Let's take a look at the final effect:
The following is the PHP code:
- /******************************
- * Line chart generation function
- ******************************/
- Function line_stats_pic ($ value_y, $ width, $ high, $ strong = 1, $ fix = 0 ){
- // Y value processing function
- 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 );
- $ Maxmax_num = max ($ value_y); // maximum value
- $ Minmin_num = min ($ value_y); // minimum value
- $ Limit_m = $ max_num-$ min_num; // very short
- $ Max_num_add = $ max_num + $ limit_m * 0.1; // maximum value of the Axis
- $ Min_num_add = $ min_num-$ limit_m * 0.1; // minimum value of the Axis
- $ 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); // 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; // left blank
-
- // Starts the image stream
- Header ("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, 0, 0); // black
- $ Color_3 = imagecolorallocate ($ pic, 194,194,194); // gray
- // Draw a grid
- Imagesetthickness ($ pic, 1); // mesh width
- $ Y_line_width = floor ($ width/100); // number of vertical gridlines
- $ Y_line_density = $ y_line_width = 0? 0: floor ($ width/$ y_line_width); // The vertical gridline density.
- $ Point_zero_y = $ zero_y> $ high? $ High: $ zero_y;
- Imagestring ($ pic, 1, $ empty_size_x-1, $ high + 4, "0", $ color_2); // zero axis marker
- For ($ I = 1; $ I <= $ y_line_width; $ I ++) {// draw vertical gridlines
- Imagesetthickness ($ pic, 1); // mesh 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 width
- Imageline ($ pic, $ y_line_density * $ I + $ empty_size_x, $ point_zero_y-4, $ y_line_density * $ I + $ empty_size_x, $ point_zero_y, $ color_2 );
- Imagestring ($ pic, 1, 10