首頁 > 後端開發 > php教程 > php繪製長條圖

php繪製長條圖

WBOY
發布: 2016-07-25 08:43:00
原創
1557 人瀏覽過
  1. /***
  2. * @project Bar Graph Program
  3. * @license GPL
  4. * @package
  5. * @file GrapBar.php
  6. * @date 2007-4-3
  7. * @version 1.00
  8. * @last modified
  9. * 定義柱狀圖(柱形圖) 類別
  10. *
  11. * 注意,使用前請確保字體路徑存在並允許訪問,如果出錯,也要檢查在php.ini配置中的open_basedir項,如果沒此路徑請添加,或在程序中設置包含
  12. *
  13. * 智能化的柱狀圖程序,用於報表等
  14. *
  15. * **/
  16. Define("DEFAULT_FONT_PATH", "c:/windows/fonts/simhei.ttf ” );
  17. class SingleBar
  18. {
  19. private $_x;
  20. 私人$_y;
  21. 私人$_h;
  22. 公用$_l = 50;
  23. 私有$_h;
  24. 公用$_l = 50;
  25. 私有$_w_w = null;
  26. private $_srcPoints = array();
  27. private $_points = array();
  28. 公用函數__construct($x, $y, $h, $l = 50, $w = null)
  29. {
  30. $this->_x = $x;
  31. $this->_y = $y;
  32. $this->_h = $h;
  33. $this->_l = $l;
  34. $this->_w = $w;
  35. $this->_srcPoints = $this->getSrcPoints();
  36. $this->_points = $this->getPoints();
  37. }
  38. public function getSrcPoints()
  39. {
  40. return array(
  41. array($this->_x , $this->_y),
  42. array(this $ ->_x $this->_l , $this->_y),
  43. 陣列($this->_x (1.35*$this->_l), $this->_y- (0.35*$這個->_l )),
  44. 陣列($這個->_x (0.35*$這個->_l), $這個->_y-(0.35*$這個->_l )),
  45. 陣列($this->_x , $this->_y $this->_h),
  46. 陣列($this->_x $this->_l , $this- >_y $this->_h),
  47. 陣列($this-> _x (1.35*$this->_l), $this->_y $this->_h-(0.35*$這->_l))
  48. );
  49. }
  50. public function getPoints ()
  51. {
  52. $points = array();
  53. foreach($this->_srcPoints as $key => $val)
  54. {
  55. $points[] = $val[0 ];
  56. $points[] = $val[1];
  57. }
  58. 返回$ 積分;
  59. }
  60. public function getTopPoints()
  61. {
  62. return array_slice($this->_points, 0, 8); //頂座標
  63. }
  64. public function getBelowPoints()
  65. {
  66. return array_merge(points_slice($this->_slice($this->_slices($thisarray_slice($this) 0, 2), array_slice($this->; _points, 8, 4), array_slice($this->_points, 2, 2)); //下方座標
  67. }
  68. public function getRightSidePoints ()
  69. {
  70. return array_merge(array_slice($this->_points, 2, 2), array_slice($this->; _points, 10, 4), array_slice($this->_points, 4, 2 )); // 重力座標
  71. }
  72. public function draw($image, $topColor, $belowColor, $sideColor, $borderColor = null, $type = 'LEFT')
  73. {
  74. if (is_null($borderColor))
  75. {
  76. $borderColor = 0xcccccc;
  77. }
  78. $top_rgb = $this->getRGB($topColleor); = $this->getRGB($belowColor);
  79. $side_rgb = $this->getRGB($sideColor);
  80. $top_color = imagecolorallocate($image, $top_rgb['R'], $top_rgb[' G'], $top_rgb['B']);
  81. $below_color = imagecolorallocate($image, $below_rgb['R'], $below_rgb['G'], $below_rgb['B']);
  82. $side_color = imagecolorallocate($image, $side_rgb['R'], $side_rgb['G'], $side_rgb['B']);
  83. imagefilledpolygon($image, $this> getTopPoints(), 4, $top_color); //畫頂面
  84. imagepolygon($image, $this->getTopPoints(), 4, $borderColor); //畫頂邊邊線
  85. imagefilledpolygongongon ($image, $this->getBelowPoints(), 4, $below_color); //畫下面
  86. imagepolygon($image, $this->getBelowPoints(), 4, $borderColor); //畫下邊線
  87. if ($type == 'LEFT')
  88. {
  89. imagefilledpolygon($image, $this->getRightSidePoints(), 4, $side_color); //畫右側面
  90. imagepolygon($image, $this->getRightSidePoints(), 4, $borderColor); //畫側邊線
  91. }
  92. }
  93. public function getRGB($color)
  94. {
  95. $ar = array();
  96. $color = hexdec($color);
  97. $ar['R'] = ($color>>16) & 0xff;
  98. $ar['G '] = ($color>>8) & 0xff;
  99. $ar['B'] = ($color) & 0xff;
  100. 回傳$ar;
  101. } }
  102. 類酒吧
  103. {
  104. 私人$_W;
  105. 私人$_H;
  106. private $_bgColor = "ffffff";
  107. private $_barHeights = array(); > private $_barTexts = array();
  108. private $_barColors = array();
  109. 公共$_title;
  110. 公共$_paddingTop = 30;
  111. 公共$_paddingBottom = 100 _paddingLeft = 45;
  112. 公共$_paddingRight = 2;
  113. 公共$_barL = 50;
  114. 公用$image;
  115. 公用函式__construct($imgW, $imgH, $imgH, $ barTexts = null, $barColors = null)
  116. {
  117. $this->_W = $imgW;
  118. $this->_H = $imgH;
  119. $this->_barHeights = $barHeights;
  120. $this->_barTexts = $barTexts;
  121. $this->_barColors = $barColors;
  122. $this->_paddingBottom = $this->resetPaddingBottom();
  123. $this->_H = $this->resetPaddingBottom();
  124. $this->_H = $this->resetPaddingBottom();
  125. $this->_H = $this->resetPaddingBottom();
  126. $this->_H = $this->resetPaddingBottom();
  127. $this->_H = $this->resetPaddingBottom(); this->resetHeight();
  128. $this->image = imagecreatetruecolor($this->_W, $this->_H);
  129. }
  130. 公用函數中風()
  131. {
  132. $this->drawBg();
  133. $this->drawBars();
  134. $this->drawTitle();
  135. $this->drawLables();
  136. ob_start() ;
  137. // header("內容類型:image/png");
  138. //imagepng($this->image);
  139. header("內容類型:" .image_type_to_mime_type(IMAGETYPE_JPEG));
  140. imagejpeg($this->image);
  141. imagedestroy($this->image);
  142. }
  143. public function drawBg()
  144. {
  145. $thisimg_w = $ ->_W;
  146. $img_h = $this->_H;
  147. $paddingTop = $this->_paddingTop;
  148. $paddingBottom = $this->_paddingBottom;
  149. $paddingLeft = $this-> _paddingLeft;
  150. $paddingRight = $this->_paddingRight;
  151. $rgb = $this->getRGB($this->_bgColor);
  152. $bg = imagecolorallocate($this->image,$rg[ 'R'], $rgb['G'], $rgb['B']);
  153. imagefilledrectangle($this->image, 0, 0, $img_w, $img_h, $bg);
  154. $side_bg = imagecolorallocatealpha($this->image, 220, 220, 220, 75);
  155. $side_bg2 = imagecolorallocate($this->image, 220, 220, 220); this->image, 190, 190, 190);
  156. $line_color = imagecolorallocate($this->image, 236, 236, 236);
  157. $dial_color = imagecolorallocate($this-1. , 131);
  158. $x1 = $paddingLeft;
  159. $y1 = $paddingTop;
  160. $x2 = $img_w - $paddingRight;
  161. $y2 = $img_h - $paddingBot; 🎜> imagerectangle($this->image, $x1, $y1, $x2, $y2, $border_color);
  162. imagefilledpolygon($this->image, array($x1-5,$y1 10, $x1 -5,$y2 10, $x1,$y2, $x1,$y1), 4, $side_bg) ;
  163. imagepolygon($this->image, array($x1-5,$y1 10, $x1 -5,$y2 10, $x1,$y2, $x1,$y1), 4, $border_color) ;
  164. imagefilledpolygon($this->image, array($x1-5,$y2 10, $x2 -5,$y2 10, $x2,$y2, $x1,$y2), 4, $side_bg) ;
  165. imagepolygon($this->image, array($x1-5,$y2 10, $x2 -5,$y2 10, $x2,$y2, $x1,$y2), 4, $border_color) ;
  166. imageline($this->image, $x1, $y2, $x2, $y2, $ side_bg2);
  167. $total_h = $img_h - $paddingTop - $paddingBottom;
  168. $every_h = $total_h/11;
  169. for($i=1; $i {
  170. imageline($this->image, $x1, $y1 ($every_h*$i), $x2 , $y1 ($every_h*$i), $line_color); //畫線
  171. }
  172. $max_h = max($this->_barHeights);
  173. for($i=1; $i {
  174. $value = $max_h - (($max_h/10)*($i-1));
  175. $value = strval($value);
  176. $str_w = strlen($value)*5;
  177. imageline ($this->image, $x1-5-3, $y1 10 ($every_h*$i), $x1-3 1, $y1 10 ($every_h*$i), $dial_color) ; //畫最線
  178. imagestring($this->image, 3, $x1-5-3-$str_w-1, $y1 10 ($every_h*$i)-5, $value, 0x000000);
  179. }
  180. }
  181. public function drawBars()
  182. {
  183. $counts = count($this->_barHeights);
  184. if (empty($this->_barColors))
  185. {
  186. $color = $this->setColor();
  187. $this->_barColors = array_slice($color, 0, $counts);
  188. //隨機播放($this-> _barColors);
  189. }
  190. $every_w = ($this->_W - $this->_paddingLeft - $this->_paddingRight)/$counts; // 每一段寬
  191. $barL = $every_w;
  192. $barL = ($barL > $this->_barL*1.35 6) ? $this->_barL : $barL/1.35 - 6;
  193. $max_h = max($this->_barHeights);
  194. $ruler_h = $this->_H - $this->_paddingTop - $this->_paddingBottom; //標尺總高度
  195. $stander_h = $ruler_h - ($ruler_h/11); //標尺10等分的高度
  196. $i = 0;
  197. foreach ($this->_barHeights as $val)
  198. {
  199. $h = ($stander_h/$max_h)*$val;
  200. $x = $this->_paddingLeft ($every_w*$i) (($every_w - ($barL*1.35))/2);;
  201. $y = $this->_H - $this->_paddingBottom 10 - $h;
  202. //$t_color = $this->_barColors[$i];
  203. $b_color = $this->_barColors[$i];
  204. //$s_color = $this->_barColors[$i];
  205. $rgb = $this->getRGB($this->_barColors[$i]);
  206. $R = $rgb['R'] * 0.7;
  207. $G = $rgb['G'] * 0.7;
  208. $B = $rgb['B'] * 0.7;
  209. $c1 = $R > 0 ? dechex($R) : '00';
  210. $c2 = $G > 0 ? dechex($G) : '00';
  211. $c3 = $B > 0 ? dechex($B) : '00';
  212. $t_color = $b_color;
  213. $s_color = $c1。 $c3;
  214. $SingleBar = new SingleBar($x, $y, $h, $barL);
  215. $SingleBar->draw($this->image, $t_color, $b_color, $s_color);
  216. $i ;
  217. }
  218. }
  219. public function drawTitle()
  220. {
  221. if (empty($this->_title))
  222. {
  223. return;
  224. }
  225. $font = 5;
  226. $font_w = imagefontwidth($font);
  227. $len = strlen($this->_title);
  228. $x = ($this->_W $this->_paddingLeft - $this->_paddingRight)/2;
  229. $x -= ($len*$font_w)/2;
  230. $y = ($this->_paddingTop - $font_w)/2 12;
  231. //imagestring($this->image, $font, $x, $y, $title, 0x000000);
  232. imagettftext($this->image, 12, 0, $x, $y, 0x000000, DEFAULT_FONT_PATH, $this->_title);
  233. }
  234. public function drawLables()
  235. {
  236. $x1 = $this->_paddingLeft - 5;
  237. $y1 = $this->_H - $this->_paddingBottom 20;
  238. $x2 = $this->_W - $this->_paddingRight;
  239. $y2 = $this->_H - 10;
  240. //imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, 0xffffff);
  241. imagerectangle($this->image, $x1, $y1, $x2, $y2, 0x000000);
  242. $空間 = 5;
  243. $x = $x1 3;
  244. $y = $y1 3;
  245. foreach ($this->_barTexts as $key => $val)
  246. {
  247. $color = $this->_barColors[$key];
  248. $rgb = $this->getRGB($color);
  249. $bg = imagecolorallocate($this->image,$rgb['R'], $rgb['G'], $rgb['B']);
  250. imagefilledrectangle($this->image, $x, $y, $x 12, $y 12, $bg); // 同等12*12的形狀
  251. imagerectangle($this->image, $x, $y, $x 12, $y 12, 0x000000); // 類似12*12的圖形框
  252. imagettftext($this->image, 12, 0, $x 12 3, $y 12, 0x000000, DEFAULT_FONT_PATH, $val);
  253. $x = 12 $space (strlen($val)*8) $space;
  254. if ($x (strlen($val)*8) >= $this->_W - $this->_paddingLeft - $this->_paddingRight)
  255. {
  256. $x = $x1 3;
  257. $y = $y 12 3;
  258. }
  259. }
  260. }
  261. public function resetPaddingBottom()
  262. {
  263. $ruler_w = $this->_W - $this->_paddingLeft - $this- >_ft -Right ;
  264. $label_w = $this->getLableTotalWidth();
  265. $lines = ceil($label_w / $ruler_w);
  266. $h = 12 * $lines (3 * ($lines 1)) 30;
  267. 回傳$h;
  268. }
  269. public function resetHeight()
  270. {
  271. $padding_bottom = $this->resetPaddingBottom();
  272. if ($this->_H - $padding_bottom {
  273. return 222 $padding_bottom;
  274. }
  275. return $this->_H;
  276. }
  277. public function getLableTotalWidth()
  278. {
  279. $counts = count($this->_barTexts);
  280. $空間 = 5;
  281. $total_len = 0;
  282. foreach ($this->_barTexts as $val)
  283. {
  284. $total_len = strlen($val);
  285. }
  286. $tx_w = ($total_len * 9) ((12 3 $space) * $counts);
  287. 回傳 $tx_w;
  288. }
  289. public function setBg($color)
  290. {
  291. $this->_bgColor = $color;
  292. }
  293. public function setTitle($title)
  294. {
  295. $this->_title = $title;
  296. }
  297. public function setColor()
  298. {
  299. $ar = array('ff', '00', '33', '66', '99', 'cc' );
  300. $color = array()
  301. for ($i=0; $i {
  302. for ($j=0; $j {
  303. for($ k=0; $k {
  304. $color[] = $ar[$i] 。 ar[$k];
  305. }
  306. }
  307. }
  308. $color2 = array();
  309. for ($i=1; $i {
  310. $color2[] = $color[$i];
  311. return $color2;
  312. }
  313. public function getRGB($顏色)
  314. {
  315. $ar = array()
  316. $color = hexdec($color);
  317. $ar['R'] = ($color>>16) & 0xff
  318. $ar['G '] = ($color>>8) & 0xff;
  319. $ar['B'] = ($color) & 0xff;
  320. 回傳$ar
  321. }
  322. }
  323. /***/
  324. $bar = new Bar(500, 300, array(600, 300, 30, 500, 400, 250, 350, 360) , array('AAAAA ', 'BBBBB', 'CCCCC 'BBBBB', 'CC ', 'DDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGGGG', 'HHHHHHHHH'));
  325. $bar->setTitle('打造完美柱狀圖!');
  326. $bar->中風()
  327. /***/
  328. ?>
複製程式碼



長條圖, php

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板