PHP Line chart generation functions and examples

Source: Internet
Author: User
  1. /*
  2. * PHP generates a line chart
  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 handling
  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 poor
  16. $max _num_add= $max _num+ $limit _m*0.1; Axis Maximum Value
  17. $min _num_add= $min _num-$limit _m*0.1; Minimum axis value
  18. $limit = $max _num_add-$min _num_add; Extreme difference-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); Zeroing 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); Y value of 0 points
  28. $empty _size_x= (strlen ($max _num) > strlen ($min _num)? strlen ($max _num): strlen ($min _num)) *5+3; Left blank
  29. Picture Stream Start
  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,0); Black
  35. $color _3=imagecolorallocate ($pic, 194,194,194);//Grey
  36. Draw Grid
  37. Imagesetthickness ($pic, 1); Grid line width
  38. $y _line_width=floor ($width/100); Number of vertical grid lines
  39. $y _line_density= $y _line_width==0? 0:floor ($width/$y _line_width); Vertical Grid line Density
  40. $point _zero_y= $zero _y > $high? $high: $zero _y;
  41. Imagestring ($pic, 1, $empty _size_x-1, $high +4, "0", $color _2); 0-Point Axis mark
  42. for ($i =1; $i <= $y _line_width; $i + +) {//Draw vertical grid lines
  43. Imagesetthickness ($pic, 1); Grid line 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 Point line 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 Mark
  48. }
  49. $x _line_width=floor ($high/30); Number of horizontal grid lines
  50. $x _line_density= $x _line_width==0? 0:floor ($high/$y _line_width); Horizontal Grid line Density
  51. if ($zero _y > $high) {//Draw horizontal grid lines
  52. Imagestring ($pic, 1,0, $high -3,round ($min _num_add, $fix), $color _2); 0-Point Axis mark
  53. for ($i =1; $i <= $x _line_width; $i + +) {
  54. Imagesetthickness ($pic, 1); Grid line 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 Point line width
  57. Imageline ($pic, 0+ $empty _size_x, $high-$x _line_density* $i, $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); Axis Mark
  59. }
  60. }else{
  61. Imagestring ($pic, 1, $empty _size_x-8, $zero _y, "0", $color _2); 0-Point Axis mark
  62. for ($i =1; $i <= ceil ($x _line_width/2); $i + +) {
  63. Imagesetthickness ($pic, 1); Grid line 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 Point line width
  69. Imageline ($pic, 0+ $empty _size_x, $zero _y-$x _line_density* $i, $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, $empty _size_x, $zero _y+ $x _line_density* $i, $color _2) ;
  72. }
  73. Imagestring ($pic, 1,0, $zero _y-$x _line_density* $i -3,round ($limit/$x _line_width* $i, $fix), $color _2); Axis Mark
  74. if ($zero _y+ $x _line_density* $i < $high) {
  75. Imagestring ($pic, 1,0, $zero _y+ $x _line_density* $i -3,round (-$limit/$x _line_width* $i, $fix), $color _2); Axis Mark
  76. }
  77. }
  78. }
  79. Drawing axes
  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. Create a polyline
  88. $point _x=0;
  89. $j = 0;
  90. Imagesetthickness ($pic, $strong); Line thickness
  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 <100; $i + +) {
  100. $value = rand (1,200);
  101. $value _y[]= $value;
  102. }
  103. Line_stats_pic ($value _y,500,100,1,1);
  104. ?>
Copy Code
  • 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.