PHP GD Library to draw an example code for a 24-hour bar chart

Source: Internet
Author: User
  1. /*

  2. 24-Hour Bar chart
  3. Author: Taokey
  4. qq:29611705
  5. * Editor: Bbs.it-home.org
  6. */

  7. function H24 ($STR) {

  8. $hour = Explode (",", $str);
  9. $hmax = max ($hour);
  10. $ppix = 150/$hmax;

  11. Calculate the height of a bar chart

  12. $h 0 = 190-$hour [0]* $ppix;
  13. $h 1 = 190-$hour [1]* $ppix;
  14. $h 2 = 190-$hour [2]* $ppix;
  15. $h 3 = 190-$hour [3]* $ppix;
  16. $h 4 = 190-$hour [4]* $ppix;
  17. $h 5 = 190-$hour [5]* $ppix;
  18. $h 6 = 190-$hour [6]* $ppix;
  19. $h 7 = 190-$hour [7]* $ppix;
  20. $h 8 = 190-$hour [8]* $ppix;
  21. $h 9 = 190-$hour [9]* $ppix;
  22. $h = 190-$hour [10]* $ppix;
  23. $h one = 190-$hour [11]* $ppix;
  24. $h = 190-$hour [12]* $ppix;
  25. $h = 190-$hour [13]* $ppix;
  26. $h = 190-$hour [14]* $ppix;
  27. $h = 190-$hour [15]* $ppix;
  28. $h = 190-$hour [16]* $ppix;
  29. $h = 190-$hour [17]* $ppix;
  30. $h = 190-$hour [18]* $ppix;
  31. $h = 190-$hour [19]* $ppix;
  32. $h = 190-$hour [20]* $ppix;
  33. $h = 190-$hour [21]* $ppix;
  34. $h = 190-$hour [22]* $ppix;
  35. $h = 190-$hour [23]* $ppix;

  36. Create an img

  37. $img = Imagecreate (755,210);
  38. Background
  39. $BGC = Imagecolorallocate ($img, 245, 250, 254);
  40. Black
  41. $BC = Imagecolorallocate ($img, 0,0,0);
  42. Draw a vertical axis
  43. Imageline ($img, 15,30,15,189, $BC);
  44. Draw the horizontal axis
  45. Imageline ($img, 15,190,750,190, $BC);

  46. Draw a vertical pivot point

  47. For ($i =39, $j =10; $i <189; $i = $i +15, $j-) {
  48. Imageline ($img, $i, $i, $BC);
  49. Imagestring ($img, $i -4, $j. " X ", $BC);
  50. }

  51. Draw a horizontal point

  52. $t = true;
  53. For ($i =31, $j =29; $i <750; $i = $j +1, $j = $j +15) {
  54. if ($t) {
  55. $x = $i;
  56. $t =false;
  57. }else{
  58. $x = $i +1;
  59. $t =true;
  60. }
  61. Imageline ($img, $x, $x, 192, $BC);
  62. }
  63. Vertical axis Marking
  64. $x = Ceil ($hmax/10);
  65. Imagestring ($img, 2,10,15, "x=". $x, $BC);
  66. Vertical axis Marking

  67. 0 Pips

  68. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  69. Imagefilledrectangle ($img, $h 0,45,189, $color);
  70. Imagestring ($img, 1,31, $h 0-10, $hour [0], $color);
  71. Imagechar ($img, 1,36,195,0, $BC);

  72. 1 pips

  73. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  74. Imagefilledrectangle ($img, $h 1,75,189, $color);
  75. Imagestring ($img, 1,61, $h 1-10, $hour [1], $color);
  76. Imagechar ($img, 1,66,195,1, $BC);

  77. 2 pips

  78. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  79. Imagefilledrectangle ($img, $h 2,105,189, $color);
  80. Imagestring ($img, 1,91, $h 2-10, $hour [2], $color);
  81. Imagechar ($img, 1,96,195,2, $BC);

  82. 3 pips

  83. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  84. Imagefilledrectangle ($img, 121, $h 3,135,189, $color);
  85. Imagestring ($img, 1,121, $h 3-10, $hour [3], $color);
  86. Imagechar ($img, 1,126,195,3, $BC);

  87. 4 pips

  88. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  89. Imagefilledrectangle ($img, 151, $h 4,165,189, $color);
  90. Imagestring ($img, 1,151, $h 4-10, $hour [4], $color);
  91. Imagechar ($img, 1,156,195,4, $BC);

  92. 5 pips

  93. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  94. Imagefilledrectangle ($img, 181, $h 5,195,189, $color);
  95. Imagestring ($img, 1,181, $h 5-10, $hour [5], $color);
  96. Imagechar ($img, 1,186,195,5, $BC);

  97. 6 pips

  98. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  99. Imagefilledrectangle ($img, 211, $h 6,225,189, $color);
  100. Imagestring ($img, 1,211, $h 6-10, $hour [6], $color);
  101. Imagechar ($img, 1,216,195,6, $BC);

  102. 7 pips

  103. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  104. Imagefilledrectangle ($img, 241, $h 7,255,189, $color);
  105. Imagestring ($img, 1,241, $h 7-10, $hour [7], $color);
  106. Imagechar ($img, 1,246,195,7, $BC);

  107. 8 pips

  108. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  109. Imagefilledrectangle ($img, 271, $h 8,285,189, $color);
  110. Imagestring ($img, 1,271, $h 8-10, $hour [8], $color);
  111. Imagechar ($img, 1,276,195,8, $BC);

  112. 9 pips

  113. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  114. Imagefilledrectangle ($img, 301, $h 9,315,189, $color);
  115. Imagestring ($img, 1,301, $h 9-10, $hour [9], $color);
  116. Imagechar ($img, 1,306,195,9, $BC);

  117. 10 pips

  118. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  119. Imagefilledrectangle ($img, 331, $h 10,345,189, $color);
  120. Imagestring ($img, 1,331, $h 10-10, $hour [ten], $color);
  121. Imagestring ($img, 1,334,195,10, $BC);

  122. 11 pips

  123. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  124. Imagefilledrectangle ($img, 361, $h 11,375,189, $color);
  125. Imagestring ($img, 1,361, $h 11-10, $hour [one], $color);
  126. Imagestring ($img, 1,364,195,11, $BC);

  127. 12 pips

  128. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  129. Imagefilledrectangle ($img, 391, $h 12,405,189, $color);
  130. Imagestring ($img, 1,391, $h 12-10, $hour [n], $color);
  131. Imagestring ($img, 1,394,195,12, $BC);

  132. 13 pips

  133. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  134. Imagefilledrectangle ($img, 421, $h 13,435,189, $color);
  135. Imagestring ($img, 1,421, $h 13-10, $hour [], $color);
  136. Imagestring ($img, 1,424,195,13, $BC);

  137. 14 Pips

  138. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  139. Imagefilledrectangle ($img, 451, $h 14,465,189, $color);
  140. Imagestring ($img, 1,451, $h 14-10, $hour [+], $color);
  141. Imagestring ($img, 1,454,195,14, $BC);

  142. 15 pips

  143. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  144. Imagefilledrectangle ($img, 481, $h 15,495,189, $color);
  145. Imagestring ($img, 1,481, $h 15-10, $hour [], $color);
  146. Imagestring ($img, 1,481,195,15, $BC);

  147. 16 pips

  148. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  149. Imagefilledrectangle ($img, 511, $h 16,525,189, $color);
  150. Imagestring ($img, 1,511, $h 16-10, $hour [+], $color);
  151. Imagestring ($img, 1,511,195,16, $BC);

  152. 17 Pips

  153. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  154. Imagefilledrectangle ($img, 541, $h 17,555,189, $color);
  155. Imagestring ($img, 1,541, $h 17-10, $hour [+], $color);
  156. Imagestring ($img, 1,544,195,17, $BC);

  157. 18 Pips

  158. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  159. Imagefilledrectangle ($img, 571, $h 18,585,189, $color);
  160. Imagestring ($img, 1,571, $h 18-10, $hour [], $color);
  161. Imagestring ($img, 1,571,195,18, $BC);

  162. 19 Pips

  163. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  164. Imagefilledrectangle ($img, 601, $h 19,615,189, $color);
  165. Imagestring ($img, 1,601, $h 19-10, $hour [+], $color);
  166. Imagestring ($img, 1,604,195,19, $BC);

  167. 20 pips

  168. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  169. Imagefilledrectangle ($img, 631, $h 20,645,189, $color);
  170. Imagestring ($img, 1,631, $h 20-10, $hour [], $color);
  171. Imagestring ($img, 1,634,195,20, $BC);

  172. 21 Pips

  173. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  174. Imagefilledrectangle ($img, 661, $h 21,675,189, $color);
  175. Imagestring ($img, 1,661, $h 21-10, $hour [+], $color);
  176. Imagestring ($img, 1,664,195,21, $BC);

  177. 22 pips

  178. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  179. Imagefilledrectangle ($img, 691, $h 22,705,189, $color);
  180. Imagestring ($img, 1,691, $h 22-10, $hour [], $color);
  181. Imagestring ($img, 1,694,195,22, $BC);

  182. 23 Pips

  183. $color = Imagecolorallocate ($img, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
  184. Imagefilledrectangle ($img, 721, $h 23,735,189, $color);
  185. Imagestring ($img, 1,721, $h 23-10, $hour [], $color);
  186. Imagestring ($img, 1,724,195,23, $BC);

  187. It's not good to look after adding a border.

  188. Imagerectangle ($img, 0, 0, 754, 209, $BC);

  189. Imagepng ($IMG);

  190. Imagedestroy ($IMG);
  191. }
  192. $str = isset ($_get[' str '])? $_get[' str ']: "";
  193. if ($STR) {
  194. H24 ($STR);
  195. }
  196. ?>

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.