PHP line chart generation functions and examples

Source: Internet
Author: User
PHP line chart generation functions and examples

  1. /*
  2. * Generate a line chart in php
  3. * By bbs.it-home.org
  4. */
  5. Function line_stats_pic ($ value_y, $ width, $ high, $ strong = 1, $ fix = 0 ){
  6. // Y value processing function
  7. Function line_point_y ($ num, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity ){
  8. $ Return = $ high-floor ($ num-$ min_num_add + $ y_pxdensity)/($ max_num_add-$ min_num_add)/$ high ));
  9. Return $ return;
  10. }
  11. // Parameter processing
  12. $ Allnum = sizeof ($ value_y );
  13. $ Max_num = max ($ value_y); // maximum value
  14. $ Min_num = min ($ value_y); // minimum value
  15. $ Limit_m = $ max_num-$ min_num; // very short
  16. $ Max_num_add = $ max_num + $ limit_m * 0.1; // maximum value of the axis
  17. $ Min_num_add = $ min_num-$ limit_m * 0.1; // minimum value of the axis
  18. $ Limit = $ max_num_add-$ min_num_add; // range-coordinate axis y
  19. $ Y_pxdensity = ($ max_num_add-$ min_num_add)/$ high; // Y axis density
  20. $ X_pxdensity = floor ($ width/$ allnum); // x axis density
  21. Reset ($ value_y); // returns the array pointer to zero.
  22. $ I = 0;
  23. Foreach ($ value_y as $ val ){
  24. $ Point_y [$ I] = line_point_y ($ val, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity );
  25. $ I ++;
  26. }
  27. $ Zero_y = line_point_y (0, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity); // the y value of the zero point
  28. $ Empty_size_x = (strlen ($ max_num)> strlen ($ min_num )? Strlen ($ max_num): strlen ($ min_num) * 5 + 3; // left blank
  29. // Starts the image stream
  30. Header ("Content-type: image/png ");
  31. $ Pic = imagecreate ($ width + $ empty_size_x + 10, $ high + 13 );
  32. Imagecolorallocate ($ pic, 255,255,255); // background color
  33. $ Color_1 = imagecolorallocate ($ pic, 30,144,255); // Line color
  34. $ Color_2 = imagecolorallocate ($ pic, 0, 0); // Black
  35. $ Color_3 = imagecolorallocate ($ pic, 194,194,194); // Gray
  36. // Draw a grid
  37. Imagesetthickness ($ pic, 1); // mesh width
  38. $ Y_line_width = floor ($ width/100); // number of vertical gridlines
  39. $ Y_line_density = $ y_line_width = 0? 0: floor ($ width/$ y_line_width); // The vertical gridline density.
  40. $ Point_zero_y = $ zero_y> $ high? $ High: $ zero_y;
  41. Imagestring ($ pic, 1, $ empty_size_x-1, $ high + 4, "0", $ color_2); // zero axis marker
  42. For ($ I = 1; $ I <= $ y_line_width; $ I ++) {// draw vertical gridlines
  43. Imagesetthickness ($ pic, 1); // mesh width
  44. Imageline ($ pic, $ y_line_density * $ I + $ empty_size_x, 0, $ y_line_density * $ I + $ empty_size_x, $ high, $ color_3 );
  45. Imagesetthickness ($ pic, 2); // axis width
  46. 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 );
  47. Imagestring ($ pic, 1,100 * $ I + $ empty_size_x-5, $ high + 4, $ allnum/$ y_line_width * $ I, $ color_2); // axis marker
  48. }
  49. $ X_line_width = floor ($ high/30); // Number of Horizontal gridlines
  50. $ X_line_density = $ x_line_width = 0? 0: floor ($ high/$ y_line_width); // horizontal grid line density
  51. If ($ zero_y> $ high) {// draw a grid line
  52. Imagestring ($ pic, $ high-3, round ($ min_num_add, $ fix), $ color_2); // zero axis mark
  53. For ($ I = 1; $ I <= $ x_line_width; $ I ++ ){
  54. Imagesetthickness ($ pic, 1); // mesh width
  55. Imageline ($ pic, 0 + $ empty_size_x, $ high-$ x_line_density * $ I, $ width + $ empty_size_x, $ high-$ x_line_density * $ I, $ color_3 );
  56. Imagesetthickness ($ pic, 2); // axis width
  57. Imageline ($ pic, 0 + $ empty_size_x, $ high-$ x_line_density * $ I, 3 + $ empty_size_x, $ high-$ x_line_density * $ I, $ color_2 );
  58. 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
  59. }
  60. } Else {
  61. Imagestring ($ pic, 1, $ empty_size_x-8, $ zero_y, "0", $ color_2); // zero axis marker
  62. For ($ I = 1; $ I <= ceil ($ x_line_width/2); $ I ++ ){
  63. Imagesetthickness ($ pic, 1); // mesh width
  64. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ width + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ color_3 );
  65. If ($ zero_y + $ x_line_density * $ I <$ high ){
  66. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ width + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ color_3 );
  67. }
  68. Imagesetthickness ($ pic, 2); // axis width
  69. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, 3 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ color_2 );
  70. If ($ zero_y + $ x_line_density * $ I <$ high ){
  71. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, 3 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ color_2 );
  72. }
  73. Imagestring ($ pic, $ zero_y-$ x_line_density * $ I-3, round ($ limit/$ x_line_width * $ I, $ fix), $ color_2); // Number of axis markers
  74. If ($ zero_y + $ x_line_density * $ I <$ high ){
  75. Imagestring ($ pic, $ zero_y + $ x_line_density * $ I-3, round (-$ limit/$ x_line_width * $ I, $ fix), $ color_2); // Number of axis markers
  76. }
  77. }
  78. }
  79. // Draw the axis
  80. Imagesetthickness ($ pic, 2); // axis width
  81. Imageline ($ pic, 1 + $ empty_size_x, 0, 1 + $ empty_size_x, $ high, $ color_2 );
  82. If ($ zero_y> $ high) {// x axis position
  83. Imageline ($ pic, 0 + $ empty_size_x, $ high, $ width + $ empty_size_x, $ high, $ color_2 );
  84. } Else {
  85. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y, $ width + $ empty_size_x, $ zero_y, $ color_2 );
  86. }
  87. // Generate a line
  88. $ Point_x = 0;
  89. $ J = 0;
  90. Imagesetthickness ($ pic, $ strong); // line width
  91. While ($ j + 1 <$ allnum ){
  92. 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 );
  93. $ Point_x + = $ x_pxdensity;
  94. $ J ++;
  95. }
  96. Imagepng ($ pic );
  97. Imagedestroy ($ pic );
  98. }
  99. For ($ I = 0; I I <100; $ I ++ ){
  100. $ Value = revert (1,200 );
  101. $ Value_y [] = $ value;
  102. }
  103. Line_stats_pic ($ value_y, 500,100, 1, 1 );
  104. ?>

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.