Let's take a look at the final result: The following is the PHP code: /******************************** line chart generation function ***** * **********************/functionline_stats_pic ($ value_y, $ width, $ high, $ strong = 1, $ fix = 0
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,100 * $ I + $ empty_size_x-5, $ high + 4, $ allnum/$ y_line_width * $ I, $ color_2); // 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 grid line density
- If ($ zero_y> $ high) {// draw a grid line
- Imagestring ($ pic, $ high-3, round ($ min_num_add, $ fix), $ color_2); // zero axis mark
- For ($ I = 1; $ I <= $ x_line_width; $ I ++ ){
- Imagesetthickness ($ pic, 1); // mesh 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); // axis width
- Imageline ($ pic, 0 + $ empty_size_x, $ high-$ x_line_density * $ I, 3 + $ empty_size_x, $ high-$ x_line_density * $ I, $ color_2 );
- Imagestring ($ pic, 1, 0, $ high-$ x_line_density * $ I-3, round ($ limit/$ x_line_width * $ I + $ min_num_add, $ fix), $ color_2 ); // number axis mark
- }
- } Else {
- Imagestring ($ pic, 1, $ empty_size_x-8, $ zero_y, "0", $ color_2); // zero axis marker
- For ($ I = 1; $ I <= ceil ($ x_line_width/2); $ I ++ ){
- Imagesetthickness ($ pic, 1); // mesh 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 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ color_3 );
- }
- Imagesetthickness ($ pic, 2); // 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 ){
- Imageline ($ 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); // Number of axis markers
- 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 of axis markers
- }
- }
- }
- // 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 -------- grid width of the generated line chart (not a white edge)
- $ High -------- Height
- $ Strong ------- line width (1 by default)
- $ Fix ------- Number of digits retained (the default value is integer)
- */
-
- For ($ I = 0; I I <100; $ I ++ ){
- $ Value = revert (1,200 );
- $ Value_y [] = $ value;
- }
- Line_stats_pic ($ value_y, 500,100, 1, 1 );