PhpGD: 24-hour bar chart
Last Update:2018-04-06
Source: Internet
Author: User
The h24 function is used to generate a bar chart. a string is required as a parameter. the format of this parameter is: the number of hours is connected by commas (,) from to. in the example, 80,250,430,134, 263,225,120, 59,151,677,340,221,550,300,229, 97,230,123,133, 87 A total of 24 numbers can be less than one. if there is no error, you can modify the function to judge.
The code is as follows:
/*
24-hour column chart
Author: taokey
QQ: 29611705
*/
Function h24 ($ str ){
$ Hour = explode (",", $ str );
$ Hmax = max ($ hour );
$ Ppix = 150/$ hmax;
// Calculate the bar height
$ H0 = 190-$ hour [0] * $ ppix;
$ H1 = 190-$ hour [1] * $ ppix;
$ H2 = 190-$ hour [2] * $ ppix;
$ H3 = 190-$ hour [3] * $ ppix;
$ H4 = 190-$ hour [4] * $ ppix;
$ H5 = 190-$ hour [5] * $ ppix;
$ H6 = 190-$ hour [6] * $ ppix;
$ H7 = 190-$ hour [7] * $ ppix;
$ H8 = 190-$ hour [8] * $ ppix;
$ H9 = 190-$ hour [9] * $ ppix;
$ H10 = 190-$ hour [10] * $ ppix;
$ H11 = 190-$ hour [11] * $ ppix;
$ H12 = 190-$ hour [12] * $ ppix;
$ H13 = 190-$ hour [13] * $ ppix;
$ H14 = 190-$ hour [14] * $ ppix;
$ H15= 190-$ hour [15] * $ ppix;
$ H16 = 190-$ hour [16] * $ ppix;
$ H17= 190-$ hour [17] * $ ppix;
$ H18 = 190-$ hour [18] * $ ppix;
$ H19 = 190-$ hour [19] * $ ppix;
$ H20 = 190-$ hour [20] * $ ppix;
$ H21 = 190-$ hour [21] * $ ppix;
$ H22-190-$ hour [22] * $ ppix;
$ H23 = 190-$ hour [23] * $ ppix;
// Create an img
$ Img = imagecreate (755,210 );
// Background
$ Bgc = imagecolorallocate ($ img, 245,250,254 );
// Black
$ Bc = imagecolorallocate ($ img, 0, 0 );
// Draw a vertical axis
Imageline ($ img, 15, 30, 15,189, $ bc );
// Draw the horizontal axis
Imageline ($ img, 15,190,750,190, $ bc );
// Draw a vertical axis
For ($ I = 39, $ j = 10; $ I <189; $ I = $ I + 15, $ j --){
Imageline ($ img, 13, $ I, 15, $ I, $ bc );
Imagestring ($ img, $ I-4, $ j. "x", $ bc );
}
// Draw a horizontal axis
$ T = true;
For ($ I = 31, $ j = 29; I I <750; $ I = $ j + 1, $ j = $ j + 15 ){
If ($ t ){
$ X = $ I;
$ T = false;
} Else {
$ X = $ I + 1;
$ T = true;
}
Imageline ($ img, $ x, 190, $ x, 192, $ bc );
}
// Vertical axis marker
$ X = ceil ($ hmax/10 );
Imagestring ($ img, 2,10, 15, "X =". $ x, $ bc );
// Vertical axis marker
// 0
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 31, $ h0, 45,189, $ color );
Imagestring ($ img, $ h0-10, $ hour [0], $ color );
Imagechar ($ img, 1, 36, 195, 0, $ bc );
// 1 point
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 61, $ h1, 75,189, $ color );
Imagestring ($ img, $ H1-10, $ hour [1], $ color );
Imagechar ($ img, 1, 66, 195, 1, $ bc );
// 2:
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 91, $ h2, 105,189, $ color );
Imagestring ($ img, $ h2-10, $ hour [2], $ color );
Imagechar ($ img, 1, 96, 195, 2, $ bc );
// 3 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 121, $ h3, 135,189, $ color );
Imagestring ($ img, 1,121, $ h3-10, $ hour [3], $ color );
Imagechar ($ img, 1,126,195, 3, $ bc );
// 4 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 151, $ h4, 165,189, $ color );
Imagestring ($ img, 1,151, $ h4-10, $ hour [4], $ color );
Imagechar ($ img, 1,156,195, 4, $ bc );
// 5 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 181, $ h5, 195,189, $ color );
Imagestring ($ img, 1,181, $ h5-10, $ hour [5], $ color );
Imagechar ($ img, 1,186,195, 5, $ bc );
// 6 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 211, $ h6, 225,189, $ color );
Imagestring ($ img, 1,211, $ h6-10, $ hour [6], $ color );
Imagechar ($ img, 1,216,195, 6, $ bc );
// 7 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 241, $ h7, 255,189, $ color );
Imagestring ($ img, 1,241, $ h7-10, $ hour [7], $ color );
Imagechar ($ img, 1,246,195, 7, $ bc );
// 8 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 271, $ h8, 285,189, $ color );
Imagestring ($ img, 1,271, $ h8-10, $ hour [8], $ color );
Imagechar ($ img, 1,276,195, 8, $ bc );
//
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 301, $ h9, 315,189, $ color );
Imagestring ($ img, 1,301, $ h9-10, $ hour [9], $ color );
Imagechar ($ img, 1,306,195, 9, $ bc );
// 10 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 331, $ h10, 345,189, $ color );
Imagestring ($ img, 1,331, $ h10-10, $ hour [10], $ color );
Imagestring ($ img, 1,334,195, 10, $ bc );
// 11 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 361, $ h11, 375,189, $ color );
Imagestring ($ img, 1,361, $ h11-10, $ hour [11], $ color );
Imagestring ($ img, 1,364,195, 11, $ bc );
// 12 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 391, $ h12, 405,189, $ color );
Imagestring ($ img, 1,391, $ h12-10, $ hour [12], $ color );
Imagestring ($ img, 1,394,195, 12, $ bc );
// 13 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 421, $ h13, 435,189, $ color );
Imagestring ($ img, 1,421, $ h13-10, $ hour [13], $ color );
Imagestring ($ img, 1,424,195, 13, $ bc );
// 14 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 451, $ h14, 465,189, $ color );
Imagestring ($ img, 1,451, $ h14-10, $ hour [14], $ color );
Imagestring ($ img, 1,454,195, 14, $ bc );
// 15 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 481, $ h15, 495,189, $ color );
Imagestring ($ img, 1,481, $ h15-10, $ hour [15], $ color );
Imagestring ($ img, 1,481,195, 15, $ bc );
// 16 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 511, $ h16, 525,189, $ color );
Imagestring ($ img, 1,511, $ h16-10, $ hour [16], $ color );
Imagestring ($ img, 1,511,195, 16, $ bc );
// 17 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 541, $ h17, 555,189, $ color );
Imagestring ($ img, 1,541, $ h17-10, $ hour [17], $ color );
Imagestring ($ img, 1,544,195, 17, $ bc );
// 18 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 571, $ h18, 585,189, $ color );
Imagestring ($ img, 1,571, $ h18-10, $ hour [18], $ color );
Imagestring ($ img, 1,571,195, 18, $ bc );
// 19 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 601, $ h19, 615,189, $ color );
Imagestring ($ img, 1,601, $ h19-10, $ hour [19], $ color );
Imagestring ($ img, 1,604,195, 19, $ bc );
// 20 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 631, $ h20, 645,189, $ color );
Imagestring ($ img, 1,631, $ h20-10, $ hour [20], $ color );
Imagestring ($ img, 1,634,195, 20, $ bc );
// 21: 00
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 661, $ h21, 675,189, $ color );
Imagestring ($ img, 1,661, $ h21-10, $ hour [21], $ color );
Imagestring ($ img, 1,664,195, 21, $ bc );
//
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 691, $ h22. 705,189, $ color );
Imagestring ($ img, 1,691, $ h22-10, $ hour [22], $ color );
Imagestring ($ img, 1,694,195, 22, $ bc );
// 23 o'clock
$ Color = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagefilledrectangle ($ img, 721, $ h23, 735,189, $ color );
Imagestring ($ img, 1,721, $ h23-10, $ hour [23], $ color );
Imagestring ($ img, 1,724,195, 23, $ bc );
// It is hard to see if a border is added
// Imagerectangle ($ img, 0, 0,754,209, $ bc );
Imagepng ($ img );
Imagedestroy ($ img );
}
$ Str = isset ($ _ GET ['str'])? $ _ GET ['str']: "";
If ($ str ){
H24 ($ str );
}
?>