Heim  >  Artikel  >  Backend-Entwicklung  >  php GD库绘制24小时柱状图的实例代码

php GD库绘制24小时柱状图的实例代码

WBOY
WBOYOriginal
2016-07-25 08:55:34870Durchsuche
  1. /*

  2. 24小时柱状图
  3. 作者:taokey
  4. QQ:29611705
  5. * 编辑:bbs.it-home.org
  6. */
  7. function h24($str){

  8. $hour = explode(",",$str);
  9. $hmax = max($hour);
  10. $ppix = 150/$hmax;
  11. //计算柱状图高度

  12. $h0 = 190-$hour[0]*$ppix;
  13. $h1 = 190-$hour[1]*$ppix;
  14. $h2 = 190-$hour[2]*$ppix;
  15. $h3 = 190-$hour[3]*$ppix;
  16. $h4 = 190-$hour[4]*$ppix;
  17. $h5 = 190-$hour[5]*$ppix;
  18. $h6 = 190-$hour[6]*$ppix;
  19. $h7 = 190-$hour[7]*$ppix;
  20. $h8 = 190-$hour[8]*$ppix;
  21. $h9 = 190-$hour[9]*$ppix;
  22. $h10 = 190-$hour[10]*$ppix;
  23. $h11 = 190-$hour[11]*$ppix;
  24. $h12 = 190-$hour[12]*$ppix;
  25. $h13 = 190-$hour[13]*$ppix;
  26. $h14 = 190-$hour[14]*$ppix;
  27. $h15 = 190-$hour[15]*$ppix;
  28. $h16 = 190-$hour[16]*$ppix;
  29. $h17 = 190-$hour[17]*$ppix;
  30. $h18 = 190-$hour[18]*$ppix;
  31. $h19 = 190-$hour[19]*$ppix;
  32. $h20 = 190-$hour[20]*$ppix;
  33. $h21 = 190-$hour[21]*$ppix;
  34. $h22 = 190-$hour[22]*$ppix;
  35. $h23 = 190-$hour[23]*$ppix;
  36. //创建一个img

  37. $img = imagecreate(755,210);
  38. //背景
  39. $bgc = imagecolorallocate ($img, 245, 250, 254);
  40. //黑色
  41. $bc = imagecolorallocate($img,0,0,0);
  42. //画竖轴
  43. imageline($img,15,30,15,189, $bc);
  44. //画横轴
  45. imageline($img,15,190,750,190, $bc);
  46. //画竖轴点

  47. for($i=39,$j=10;$i imageline($img,13,$i,15,$i, $bc);
  48. imagestring($img,1,1,$i-4,$j."x", $bc);
  49. }
  50. //画横轴点

  51. $t = true;
  52. for($i=31,$j=29;$i if($t){
  53. $x=$i;
  54. $t=false;
  55. }else{
  56. $x=$i+1;
  57. $t=true;
  58. }
  59. imageline($img,$x,190,$x,192, $bc);
  60. }
  61. //竖轴标记
  62. $x = ceil($hmax/10);
  63. imagestring($img,2,10,15,"X=".$x,$bc);
  64. //竖轴标记
  65. //0点

  66. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  67. imagefilledrectangle($img,31,$h0,45,189,$color);
  68. imagestring($img,1,31,$h0-10,$hour[0],$color);
  69. imagechar($img,1,36,195,0,$bc);
  70. //1点

  71. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  72. imagefilledrectangle($img,61,$h1,75,189,$color);
  73. imagestring($img,1,61,$h1-10,$hour[1],$color);
  74. imagechar($img,1,66,195,1,$bc);
  75. //2点

  76. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  77. imagefilledrectangle($img,91,$h2,105,189,$color);
  78. imagestring($img,1,91,$h2-10,$hour[2],$color);
  79. imagechar($img,1,96,195,2,$bc);
  80. //3点

  81. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  82. imagefilledrectangle($img,121,$h3,135,189,$color);
  83. imagestring($img,1,121,$h3-10,$hour[3],$color);
  84. imagechar($img,1,126,195,3,$bc);
  85. //4点

  86. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  87. imagefilledrectangle($img,151,$h4,165,189,$color);
  88. imagestring($img,1,151,$h4-10,$hour[4],$color);
  89. imagechar($img,1,156,195,4,$bc);
  90. //5点

  91. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  92. imagefilledrectangle($img,181,$h5,195,189,$color);
  93. imagestring($img,1,181,$h5-10,$hour[5],$color);
  94. imagechar($img,1,186,195,5,$bc);
  95. //6点

  96. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  97. imagefilledrectangle($img,211,$h6,225,189,$color);
  98. imagestring($img,1,211,$h6-10,$hour[6],$color);
  99. imagechar($img,1,216,195,6,$bc);
  100. //7点

  101. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  102. imagefilledrectangle($img,241,$h7,255,189,$color);
  103. imagestring($img,1,241,$h7-10,$hour[7],$color);
  104. imagechar($img,1,246,195,7,$bc);
  105. //8点

  106. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  107. imagefilledrectangle($img,271,$h8,285,189,$color);
  108. imagestring($img,1,271,$h8-10,$hour[8],$color);
  109. imagechar($img,1,276,195,8,$bc);
  110. //9点

  111. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  112. imagefilledrectangle($img,301,$h9,315,189,$color);
  113. imagestring($img,1,301,$h9-10,$hour[9],$color);
  114. imagechar($img,1,306,195,9,$bc);
  115. //10点

  116. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  117. imagefilledrectangle($img,331,$h10,345,189,$color);
  118. imagestring($img,1,331,$h10-10,$hour[10],$color);
  119. imagestring($img,1,334,195,10,$bc);
  120. //11点

  121. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  122. imagefilledrectangle($img,361,$h11,375,189,$color);
  123. imagestring($img,1,361,$h11-10,$hour[11],$color);
  124. imagestring($img,1,364,195,11,$bc);
  125. //12点

  126. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  127. imagefilledrectangle($img,391,$h12,405,189,$color);
  128. imagestring($img,1,391,$h12-10,$hour[12],$color);
  129. imagestring($img,1,394,195,12,$bc);
  130. //13点

  131. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  132. imagefilledrectangle($img,421,$h13,435,189,$color);
  133. imagestring($img,1,421,$h13-10,$hour[13],$color);
  134. imagestring($img,1,424,195,13,$bc);
  135. //14点

  136. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  137. imagefilledrectangle($img,451,$h14,465,189,$color);
  138. imagestring($img,1,451,$h14-10,$hour[14],$color);
  139. imagestring($img,1,454,195,14,$bc);
  140. //15点

  141. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  142. imagefilledrectangle($img,481,$h15,495,189,$color);
  143. imagestring($img,1,481,$h15-10,$hour[15],$color);
  144. imagestring($img,1,481,195,15,$bc);
  145. //16点

  146. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  147. imagefilledrectangle($img,511,$h16,525,189,$color);
  148. imagestring($img,1,511,$h16-10,$hour[16],$color);
  149. imagestring($img,1,511,195,16,$bc);
  150. //17点

  151. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  152. imagefilledrectangle($img,541,$h17,555,189,$color);
  153. imagestring($img,1,541,$h17-10,$hour[17],$color);
  154. imagestring($img,1,544,195,17,$bc);
  155. //18点

  156. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  157. imagefilledrectangle($img,571,$h18,585,189,$color);
  158. imagestring($img,1,571,$h18-10,$hour[18],$color);
  159. imagestring($img,1,571,195,18,$bc);
  160. //19点

  161. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  162. imagefilledrectangle($img,601,$h19,615,189,$color);
  163. imagestring($img,1,601,$h19-10,$hour[19],$color);
  164. imagestring($img,1,604,195,19,$bc);
  165. //20点

  166. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  167. imagefilledrectangle($img,631,$h20,645,189,$color);
  168. imagestring($img,1,631,$h20-10,$hour[20],$color);
  169. imagestring($img,1,634,195,20,$bc);
  170. //21点

  171. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  172. imagefilledrectangle($img,661,$h21,675,189,$color);
  173. imagestring($img,1,661,$h21-10,$hour[21],$color);
  174. imagestring($img,1,664,195,21,$bc);
  175. //22点

  176. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  177. imagefilledrectangle($img,691,$h22,705,189,$color);
  178. imagestring($img,1,691,$h22-10,$hour[22],$color);
  179. imagestring($img,1,694,195,22,$bc);
  180. //23点

  181. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  182. imagefilledrectangle($img,721,$h23,735,189,$color);
  183. imagestring($img,1,721,$h23-10,$hour[23],$color);
  184. imagestring($img,1,724,195,23,$bc);
  185. //加个边框 加了之后不好看

  186. //imagerectangle($img, 0, 0, 754, 209, $bc);
  187. imagepng($img);

  188. imagedestroy($img);
  189. }
  190. $str = isset($_GET['str'])?$_GET['str']:"";
  191. if($str){
  192. h24($str);
  193. }
  194. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn