php GD庫繪製24小時長條圖的實例程式碼

WBOY
發布: 2016-07-25 08:55:34
原創
874 人瀏覽過
  1. /*

  2. 24小時柱狀圖
  3. 作者:taokey
  4. QQQ>24小時柱狀圖
  5. 作者:taokey
  6. QQQ:29611705
  7. * 編輯:bbs.it-home.org
  8. */
  9. function h24($str){

  10. $hour = explode(",",$str);
  11. $hmax = max($hour);
  12. $ppix = 150/$hmax;
  13. //計算長條圖高度

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

  39. $img = imagecreate(755,210);
  40. //背景
  41. $bgc = imagecolorallocate ($img, 245, 250, 254); $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. $x = ceil($hmax/10);
  62. imagestring($img,2,10,15,"X=".$x,$bc);
  63. // 垂直軸標記
  64. ; //0點

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

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

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

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

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

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

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

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

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

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

  115. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  116. imagefilledrectangle($img,331,$h10,345,189,$color);
  117. imagestring($img,1,331,$h10-10,$hour[10],$color);
  118. //11 點

  119. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  120. imagefilledrectangle($img,361,$h11,375,189,$color);
  121. imagestring($img,1,361,$h11-10,$hour[11],$color);
  122. //12點

  123. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  124. imagefilledrectangle($img,391,$h12,405,189,$color);
  125. imagestring($img,1,391,$h12-10,$hour[12],$color); strage img,1,394,195,12,$bc);
  126. //13點

  127. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  128. imagefilledrectangle($img,421,$h13,435,189,$color);
  129. imagestring($img,1,421,$h13-10,$hour[13],$color);
  130. //14點

  131. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255) , mt_rand(0, 255));
  132. imagefilledrectangle($img,451,$h14,465,189,$color);
  133. imagestring($img,1,451,$h14-10,$hour[14],$color); strage img,1,454,195,14,$bc);
  134. //15點

  135. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  136. imagefilledrectangle($img,481,$h15,495,189,$color);
  137. imagestring($img,1,481,$h15-10,$hour[15],$color);
  138. //16點

  139. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  140. imagefilledrectangle($img,511,$h16,525,189,$color);
  141. imagestring($img,1,511,$h16-10,$hour[16],$color);
  142. //17點

  143. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  144. imagefilledrectangle($img,541,$h17,555,189,$color);
  145. imagestring($img,1,541,$h17-10,$hour[17],$color);
  146. //18點

  147. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  148. imagefilledrectangle($img,571,$h18,585,189,$color);
  149. imagestring($img,1,571,$h18-10,$hour[18],$color);
  150. //19點

  151. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  152. imagefilledrectangle($img,601,$h19,615,189,$color);
  153. imagestring($img,1,601,$h19-10,$hour[19],$color);
  154. //20點

  155. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  156. imagefilledrectangle($img,631,$h20,645,189,$color);
  157. imagestring($img,1,631,$h20-10,$hour[20],$color); strage img,1,634,195,20,$bc);
  158. //21 點

  159. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  160. imagefilledrectangle($img,661,$h21,675,189,$color);
  161. imagestring($img,1,661,$h21-10,$hour[21],$color); strage img,1,664,195,21,$bc);
  162. //22 點

  163. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  164. imagefilledrectangle($img,691,$h22,705,189,$color);
  165. imagestring($img,1,691,$h22-10,$hour[22],$color); strage img,1,694,195,22,$bc);
  166. //23點

  167. $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand (0, 255));
  168. imagefilledrectangle($img,721,$h23,735,189,$color);
  169. imagestring($img,1,721,$h23-10,$hour[23],$color); strage img,1,724,195,23,$bc);
  170. //加了個眉毛之後不

  171. //imagerectangle($img, 0, 0, 754, 209, $bc);
  172. imagepng($img);

  173. imagedestroy($img);
  174. }
  175. $str = isset($_GET['str'])?$_GET['str']:"";
  176. if($str){
  177. h24($str);
  178. }
  179. ?>
  180. 複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板