The PHPGD Library dynamically generates the line chart instance code

Source: Internet
Author: User
The PHPGD Library dynamically generates the line chart instance code

  1. /**

  2. * The php GD library generates line charts, bar charts, and pie charts.
  3. * By bbs.it-home.org
  4. */
  5. $ Img_gao = 170;
  6. $ Img_kuan = 0;
  7. $ 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.
  8. $ Zuo = 20; // left blank
  9. $ You = 20; // leave it blank on the right
  10. $ Shang = 20; // leave it empty
  11. $ Xia = 20; // leave it blank
  12. $ Zuidashujuzhi = 1;
  13. $ P_x = array (); // the abscissa of a vertex.
  14. $ P_y = array (); // point ordinate
  15. $ Y_name = split (",", $ _ GET ["x_name"]);
  16. If ($ _ GET ["a"] = "") die ("error id: 0 ");
  17. $ Shuju = split (",", $ _ GET ["a"]);
  18. // Obtain the maximum value of the vertical axis.
  19. For ($ I = 0; $ I If (! Is_numeric ($ shuju [$ I]) die ("error id: 1 ");
  20. If ($ shuju [$ I]> $ zuidashujuzhi) $ zuidashujuzhi = $ shuju [$ I];
  21. }
  22. // Obtain the image width
  23. $ Img_kuan = $ zuo + $ you + count ($ shuju) * $ jiange;
  24. // Create image resources
  25. $ Image = imagecreate ($ img_kuan, $ img_gao );
  26. // Gray background
  27. $ White = imagecolorallocate ($ image, 0xEE, 0xEE, 0xEE );
  28. // The coordinate axis is displayed in black.
  29. $ Zuobiao_yanse = imagecolorallocate ($ image, 0x00, 0x00, 0x00 );
  30. // Display the line in blue
  31. $ Xian_yanse = imagecolorallocate ($ image, 0x00, 0x00, 0xFF );
  32. // Draw coordinates
  33. // Horizontal axis
  34. Imageline ($ image, $ zuo, $ img_gao-$ xia, $ img_kuan-$ you/2, $ img_gao-$ xia, $ zuobiao_yanse );
  35. // Vertical axis
  36. Imageline ($ image, $ zuo, $ shang/2, $ zuo, $ img_gao-$ xia, $ zuobiao_yanse );
  37. // Obtain the coordinates of each vertex
  38. For ($ I = 0; $ I Array_push ($ p_x, $ zuo + $ I * $ jiange );
  39. Array_push ($ p_y, $ shang + round ($ img_gao-$ shang-$ xia) * (1-$ shuju [$ I]/$ zuidashujuzhi )));
  40. }
  41. // Scale of the vertical axis
  42. Imageline ($ image, $ zuo, $ shang, $ zuo + 6, $ shang, $ zuobiao_yanse );
  43. Imagestring ($ image, 1, $ zuo/4, $ shang, $ zuidashujuzhi, $ zuobiao_yanse );
  44. Imageline ($ image, $ zuo, $ shang + ($ img_gao-$ shang-$ xia) * 1/4, $ zuo + 6, $ shang + ($ img_gao-$ shang-$ xia) * 1/4, $ zuobiao_yanse );
  45. Imagestring ($ image, 1, $ zuo/4, $ shang + ($ img_gao-$ shang-$ xia) * 1/4, $ zuidashujuzhi * 3/4, $ zuobiao_yanse );
  46. Imageline ($ image, $ zuo, $ shang + ($ img_gao-$ shang-$ xia) * 2/4, $ zuo + 6, $ shang + ($ img_gao-$ shang-$ xia) * 2/4, $ zuobiao_yanse );
  47. Imagestring ($ image, 1, $ zuo/4, $ shang + ($ img_gao-$ shang-$ xia) * 2/4, $ zuidashujuzhi * 2/4, $ zuobiao_yanse );
  48. Imageline ($ image, $ zuo, $ shang + ($ img_gao-$ shang-$ xia) * 3/4, $ zuo + 6, $ shang + ($ img_gao-$ shang-$ xia) * 3/4, $ zuobiao_yanse );
  49. Imagestring ($ image, 1, $ zuo/4, $ shang + ($ img_gao-$ shang-$ xia) * 3/4, $ zuidashujuzhi * 1/4, $ zuobiao_yanse );
  50. // Horizontal axis scale
  51. For ($ I = 0; $ I Imageline ($ image, $ zuo + $ I * $ jiange, $ img_gao-$ xia, $ zuo + $ I * $ jiange, $ img_gao-$ xia-6, $ zuobiao_yanse );
  52. Imagestring ($ image, 1, $ zuo + $ I * $ jiange-$ jiange/4, $ shang + ($ img_gao-$ shang-$ xia) + 2, $ y_name [$ I], $ zuobiao_yanse );
  53. }

  54. // Line

  55. $ Shuju_yanse_int = 0;
  56. For ($ I = 0; $ I If ($ I + 1 <> count ($ shuju )){
  57. Imageline ($ image, $ p_x [$ I], $ p_y [$ I], $ p_x [$ I + 1], $ p_y [$ I + 1], $ xian_yanse );
  58. Imagefilledrectangle ($ image, $ p_x [$ I]-1, $ p_y [$ I]-1, $ p_x [$ I] + 1, $ p_y [$ I] + 1, $ xian_yanse );
  59. }
  60. }
  61. // The last point is not shown in the previous loop. you need to append it here.
  62. 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 );

  63. // Mark the data value

  64. For ($ I = 0; $ I Imagestring ($ image, 3, $ p_x [$ I] + 4, $ p_y [$ I]-12, $ shuju [$ I], $ zuobiao_yanse );
  65. }
  66. // Set the file header
  67. Header ('content-type: image/png ');
  68. // Output image
  69. Imagepng ($ image );
  70. // Release resources
  71. Imagedestroy ($ image );
  72. ?>

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:

  1. For ($ I = 0; $ I <12; $ I ++ ){
  2. }
  3. Echo"
  4. ?>
  5. ";
    ". $ I." month

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.