/**
- * The php GD library generates line charts, bar charts, and pie charts.
- * By bbs.it-home.org
- */
- $ Img_gao = 170;
- $ Img_kuan = 0;
- $ Jiange = 30; // The interval between the x-coordinate point and the point. The width of the generated image automatically varies according to the amount of input data.
- $ Zuo = 20; // left blank
- $ You = 20; // leave it blank on the right
- $ Shang = 20; // leave it empty
- $ Xia = 20; // leave it blank
- $ Zuidashujuzhi = 1;
- $ P_x = array (); // the abscissa of a vertex.
- $ P_y = array (); // point ordinate
- $ Y_name = split (",", $ _ GET ["x_name"]);
- If ($ _ GET ["a"] = "") die ("error id: 0 ");
- $ Shuju = split (",", $ _ GET ["a"]);
- // Obtain the maximum value of the vertical axis.
- For ($ I = 0; $ I If (! Is_numeric ($ shuju [$ I]) die ("error id: 1 ");
- If ($ shuju [$ I]> $ zuidashujuzhi) $ zuidashujuzhi = $ shuju [$ I];
- }
- // Obtain the image width
- $ Img_kuan = $ zuo + $ you + count ($ shuju) * $ jiange;
- // Create image resources
- $ Image = imagecreate ($ img_kuan, $ img_gao );
- // Gray background
- $ White = imagecolorallocate ($ image, 0xEE, 0xEE, 0xEE );
- // The coordinate axis is displayed in black.
- $ Zuobiao_yanse = imagecolorallocate ($ image, 0x00, 0x00, 0x00 );
- // Display the line in blue
- $ Xian_yanse = imagecolorallocate ($ image, 0x00, 0x00, 0xFF );
- // Draw coordinates
- // Horizontal axis
- Imageline ($ image, $ zuo, $ img_gao-$ xia, $ img_kuan-$ you/2, $ img_gao-$ xia, $ zuobiao_yanse );
- // Vertical axis
- Imageline ($ image, $ zuo, $ shang/2, $ zuo, $ img_gao-$ xia, $ zuobiao_yanse );
- // Obtain the coordinates of each vertex
- For ($ I = 0; $ I Array_push ($ p_x, $ zuo + $ I * $ jiange );
- Array_push ($ p_y, $ shang + round ($ img_gao-$ shang-$ xia) * (1-$ shuju [$ I]/$ zuidashujuzhi )));
- }
- // Scale of the vertical axis
- Imageline ($ image, $ zuo, $ shang, $ zuo + 6, $ shang, $ zuobiao_yanse );
- Imagestring ($ image, 1, $ zuo/4, $ shang, $ zuidashujuzhi, $ zuobiao_yanse );
- Imageline ($ image, $ zuo, $ shang + ($ img_gao-$ shang-$ xia) * 1/4, $ zuo + 6, $ shang + ($ img_gao-$ shang-$ xia) * 1/4, $ zuobiao_yanse );
- Imagestring ($ image, 1, $ zuo/4, $ shang + ($ img_gao-$ shang-$ xia) * 1/4, $ zuidashujuzhi * 3/4, $ zuobiao_yanse );
- Imageline ($ image, $ zuo, $ shang + ($ img_gao-$ shang-$ xia) * 2/4, $ zuo + 6, $ shang + ($ img_gao-$ shang-$ xia) * 2/4, $ zuobiao_yanse );
- Imagestring ($ image, 1, $ zuo/4, $ shang + ($ img_gao-$ shang-$ xia) * 2/4, $ zuidashujuzhi * 2/4, $ zuobiao_yanse );
- Imageline ($ image, $ zuo, $ shang + ($ img_gao-$ shang-$ xia) * 3/4, $ zuo + 6, $ shang + ($ img_gao-$ shang-$ xia) * 3/4, $ zuobiao_yanse );
- Imagestring ($ image, 1, $ zuo/4, $ shang + ($ img_gao-$ shang-$ xia) * 3/4, $ zuidashujuzhi * 1/4, $ zuobiao_yanse );
- // Horizontal axis scale
- For ($ I = 0; $ I Imageline ($ image, $ zuo + $ I * $ jiange, $ img_gao-$ xia, $ zuo + $ I * $ jiange, $ img_gao-$ xia-6, $ zuobiao_yanse );
- Imagestring ($ image, 1, $ zuo + $ I * $ jiange-$ jiange/4, $ shang + ($ img_gao-$ shang-$ xia) + 2, $ y_name [$ I], $ zuobiao_yanse );
- }
// Line
- $ Shuju_yanse_int = 0;
- For ($ I = 0; $ I If ($ I + 1 <> count ($ shuju )){
- Imageline ($ image, $ p_x [$ I], $ p_y [$ I], $ p_x [$ I + 1], $ p_y [$ I + 1], $ xian_yanse );
- Imagefilledrectangle ($ image, $ p_x [$ I]-1, $ p_y [$ I]-1, $ p_x [$ I] + 1, $ p_y [$ I] + 1, $ xian_yanse );
- }
- }
- // The last point is not shown in the previous loop. you need to append it here.
- Imagefilledrectangle ($ image, $ p_x [count ($ shuju)-1]-1, $ p_y [count ($ shuju)-1]-1, $ p_x [count ($ shuju)-1] + 1, $ p_y [count ($ shuju)-1] + 1, $ xian_yanse );
// Mark the data value
- For ($ I = 0; $ I Imagestring ($ image, 3, $ p_x [$ I] + 4, $ p_y [$ I]-12, $ shuju [$ I], $ zuobiao_yanse );
- }
- // Set the file header
- Header ('content-type: image/png ');
- // Output image
- Imagepng ($ image );
- // Release resources
- Imagedestroy ($ image );
- ?>
2. Insert the following code at the position where the image needs to be displayed:
Note: the value of a is calculated by yourself. The text format of a is a string of several data connected by ",", which is passed in by get. Because more PHP environment configurations are required to write Chinese characters to the graph, an html solution is provided here: dynamically generate a table based on the number of data, and place the horizontal axis coordinate scale name. Example:
- For ($ I = 0; $ I <12; $ I ++ ){
- }
- Echo"
- ?>
|