Example of curve Statistical Chart implemented by PHP, and php curve Statistical Chart
This example describes the curve Statistical Chart implemented by PHP. We will share this with you for your reference. The details are as follows:
<? Php/********************************* line chart generation function * youd * 090207 -01 *****************************/function line_stats_pic ($ value_y, $ width, $ high, $ strong = 1, $ fix = 0) {// y value processing function line_point_y ($ num, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity) {$ return = $ high-floor ($ num-$ min_num_add + $ y_pxdensity)/($ max_num_add-$ min_num_add)/$ high )); return $ return;} // parameter processing $ allnum = sizeof ($ value_y); $ max_num = max ($ value _ Y); // maximum value $ min_num = min ($ value_y); // minimum value $ limit_m = $ max_num-$ min_num; // very short $ max_num_add = $ max_num + $ limit_m * 0.1; // the maximum value of the Axis $ min_num_add = $ min_num-$ limit_m * 0.1; // axis minimum value $ limit = $ max_num_add-$ min_num_add; // range-coordinate axis y $ y_pxdensity = ($ max_num_add-$ min_num_add)/$ high; // y axis density $ x_pxdensity = floor ($ width/$ allnum); // returns the X axis density reset ($ value_y); // returns the array pointer to zero $ I = 0; foreach ($ value_y as $ val) {$ point_y [$ I] = line_point_y ($ val, $ width, $ high, $ max _ Num_add, $ min_num_add, $ y_pxdensity); $ I ++;} $ zero_y = line_point_y (0, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity ); // The y value of the zero point $ empty_size_x = (strlen ($ max_num)> strlen ($ min_num )? Strlen ($ max_num): strlen ($ min_num) * 5 + 3; // blank on the left // start header of the image stream ("Content-type: image/png "); $ pic = imagecreate ($ width + $ empty_size_x + 10, $ high + 13); imagecolorallocate ($ pic, 255,255,255); // background color $ color_1 = imagecolorallocate ($ pic, 30,144,255); // line color $ color_2 = imagecolorallocate ($ pic, 194,194,194, 0); // black $ color_3 = imagecolorallocate ($ pic ); // gray // draw the grid imagesetthickness ($ pic, 1); // The grid width $ y_line_width = floor ($ width/100 ); // Number of vertical gridlines $ y_line_density = $ y_line_width = 0? 0: floor ($ width/$ y_line_width); // longitudinal gridline density $ point_zero_y = $ zero_y> $ high? $ High: $ zero_y; imagestring ($ pic, 1, $ empty_size_x-1, $ high + 4, "0", $ color_2 ); // for ($ I = 1; $ I <= $ y_line_width; $ I ++) {// draw the vertical gridline imagesetthickness ($ pic, 1 ); // grid line width imageline ($ pic, $ y_line_density * $ I + $ empty_size_x, 0, $ y_line_density * $ I + $ empty_size_x, $ high, $ color_3 ); imagesetthickness ($ pic, 2); // axis point width imageline ($ pic, $ y_line_density * $ I + $ empty_size_x, $ point_zero_y-4, $ y_line_density * $ I + $ empty_size_x, $ point_z Ero_y, $ color_2); imagestring ($ pic, 1,100 * $ I + $ empty_size_x-5, $ high + 4, $ allnum/$ y_line_width * $ I, $ color_2 ); // number axis marker} $ x_line_width = floor ($ high/30); // number of horizontal gridlines $ x_line_density = $ x_line_width = 0? 0: floor ($ high/$ y_line_width); // horizontal gridline density if ($ zero_y> $ high) {// draw a horizontal gridline imagestring ($ pic, $ high-3, round ($ min_num_add, $ fix), $ color_2); // mark the zero-point numeric axis for ($ I = 1; $ I <= $ x_line_width; $ I ++) {imagesetthickness ($ pic, 1); // The grid width imageline ($ pic, 0 + $ empty_size_x, $ high-$ x_line_density * $ I, $ width + $ empty_size_x, $ high-$ x_line_density * $ I, $ color_3); imagesetthickness ($ pic, 2); // imageline ($ pic, 0 + $ empty_size_x, $ high-$ x _ Line_density * $ I, 3 + $ empty_size_x, $ high-$ x_line_density * $ I, $ color_2); imagestring ($ pic, $ high-$ x_line_density * $ I-3, round ($ limit/$ x_line_width * $ I + $ min_num_add, $ fix), $ color_2); // number axis flag} else {imagestring ($ pic, 1, $ empty_size_x-8, $ zero_y, "0", $ color_2); // for ($ I = 1; $ I <= ceil ($ x_line_width/2); $ I ++) {imagesetthickness ($ pic, 1); // The grid width. imageline ($ pic, 0 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ width + $ Empty_size_x, $ zero_y-$ x_line_density * $ I, $ color_3); if ($ zero_y + $ x_line_density * $ I <$ high) {imageline ($ pic, 0 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ width + $ fill, $ zero_y + $ x_line_density * $ I, $ color_3);} imagesetthickness ($ pic, 2); // The axis width imageline ($ pic, 0 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, 3 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ color_2); if ($ zero_y + $ x_line_density * $ I <$ high) {im Ageline ($ pic, 0 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, 3 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ color_2 );} imagestring ($ pic, $ zero_y-$ x_line_density * $ I-3, round ($ limit/$ x_line_width * $ I, $ fix), $ color_2 ); // if ($ zero_y + $ x_line_density * $ I <$ high) {imagestring ($ pic, $ zero_y + $ x_line_density * $ I-3, round (-$ limit/$ x_line_width * $ I, $ fix), $ color_2); // number axis marker }}// draw the axis imagesetthickness ($ pic, 2 ); // Axis width imageline ($ pic, 1 + $ empty_size_x, 0, 1 + $ empty_size_x, $ high, $ color_2); if ($ zero_y> $ high) {// X axis position imageline ($ pic, 0 + $ empty_size_x, $ high, $ width + $ empty_size_x, $ high, $ color_2);} else {imageline ($ pic, 0 + $ empty_size_x, $ zero_y, $ width + $ empty_size_x, $ zero_y, $ color_2);} // generate a line $ point_x = 0; $ j = 0; imagesetthickness ($ pic, $ strong); // line width while ($ j + 1 <$ allnum) {imageline ($ pic, $ point_x + 2 + $ empty_size_x, $ point_y [$ j], $ point _ X + $ x_pxdensity + 2 + $ empty_size_x, $ point_y [$ j + 1], $ color_1); $ point_x + = $ x_pxdensity; $ j ++ ;} imagepng ($ pic); imagedestroy ($ pic);}/* Function Description and example parameter description: $ value_y -------- contains the array of the line chart you want to generate, the key value must increase progressively from 0. $ Width -------- the grid width (not white) of the generated line chart $ high -------- height $ strong ------- line width (1 by default) $ fix ------- number of data retention digits (rounded by default) */for ($ I = 0; $ I <100; $ I ++) {$ value = rand (1,200); $ value_y [] = $ value ;} line_stats_pic ($ value_y, 500,100, 1, 1);?>
The running result is as follows: