当前位置:Gxlcms > PHP教程 > php绘制柱状图

php绘制柱状图

时间:2021-07-01 10:21:17 帮助过:24人阅读

  1. /***
  2. * @project Bar Graph Program
  3. * @license GPL
  4. * @package
  5. * @file GrapBar.php
  6. * @date 2007-4-3
  7. * @version 1.0
  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. private $_y;
  21. private $_h;
  22. public $_l = 50;
  23. private $_w = null;
  24. private $_srcPoints = array();
  25. private $_points = array();
  26. public function __construct($x, $y, $h, $l = 50, $w = null)
  27. {
  28. $this->_x = $x;
  29. $this->_y = $y;
  30. $this->_h = $h;
  31. $this->_l = $l;
  32. $this->_w = $w;
  33. $this->_srcPoints = $this->getSrcPoints();
  34. $this->_points = $this->getPoints();
  35. }
  36. public function getSrcPoints()
  37. {
  38. return array(
  39. array($this->_x , $this->_y),
  40. array($this->_x+$this->_l , $this->_y),
  41. array($this->_x+(1.35*$this->_l), $this->_y-(0.35*$this->_l)),
  42. array($this->_x+(0.35*$this->_l), $this->_y-(0.35*$this->_l)),
  43. array($this->_x , $this->_y+$this->_h),
  44. array($this->_x+$this->_l , $this->_y+$this->_h),
  45. array($this->_x+(1.35*$this->_l), $this->_y+$this->_h-(0.35*$this->_l))
  46. );
  47. }
  48. public function getPoints()
  49. {
  50. $points = array();
  51. foreach($this->_srcPoints as $key => $val)
  52. {
  53. $points[] = $val[0];
  54. $points[] = $val[1];
  55. }
  56. return $points;
  57. }
  58. public function getTopPoints()
  59. {
  60. return array_slice($this->_points, 0, 8); //顶坐标
  61. }
  62. public function getBelowPoints()
  63. {
  64. return array_merge(array_slice($this->_points, 0, 2), array_slice($this->_points, 8, 4), array_slice($this->_points, 2, 2)); //下坐标
  65. }
  66. public function getRightSidePoints()
  67. {
  68. return array_merge(array_slice($this->_points, 2, 2), array_slice($this->_points, 10, 4), array_slice($this->_points, 4, 2)); //右侧坐标
  69. }
  70. public function draw($image, $topColor, $belowColor, $sideColor, $borderColor = null, $type = 'LEFT')
  71. {
  72. if (is_null($borderColor))
  73. {
  74. $borderColor = 0xcccccc;
  75. }
  76. $top_rgb = $this->getRGB($topColor);
  77. $below_rgb = $this->getRGB($belowColor);
  78. $side_rgb = $this->getRGB($sideColor);
  79. $top_color = imagecolorallocate($image, $top_rgb['R'], $top_rgb['G'], $top_rgb['B']);
  80. $below_color = imagecolorallocate($image, $below_rgb['R'], $below_rgb['G'], $below_rgb['B']);
  81. $side_color = imagecolorallocate($image, $side_rgb['R'], $side_rgb['G'], $side_rgb['B']);
  82. imagefilledpolygon($image, $this->getTopPoints(), 4, $top_color); //画顶面
  83. imagepolygon($image, $this->getTopPoints(), 4, $borderColor); //画顶面边线
  84. imagefilledpolygon($image, $this->getBelowPoints(), 4, $below_color); //画下面
  85. imagepolygon($image, $this->getBelowPoints(), 4, $borderColor); //画下面边线
  86. if ($type == 'LEFT')
  87. {
  88. imagefilledpolygon($image, $this->getRightSidePoints(), 4, $side_color); //画右侧面
  89. imagepolygon($image, $this->getRightSidePoints(), 4, $borderColor); //画侧面边线
  90. }
  91. }
  92. public function getRGB($color)
  93. {
  94. $ar = array();
  95. $color = hexdec($color);
  96. $ar['R'] = ($color>>16) & 0xff;
  97. $ar['G'] = ($color>>8) & 0xff;
  98. $ar['B'] = ($color) & 0xff;
  99. return $ar;
  100. }
  101. }
  102. class Bar
  103. {
  104. private $_W;
  105. private $_H;
  106. private $_bgColor = "ffffff";
  107. private $_barHeights = array();
  108. private $_barTexts = array();
  109. private $_barColors = array();
  110. public $_title;
  111. public $_paddingTop = 30;
  112. public $_paddingBottom = 100;
  113. public $_paddingLeft = 45;
  114. public $_paddingRight = 2;
  115. public $_barL = 50;
  116. public $image;
  117. public function __construct($imgW, $imgH, $barHeights, $barTexts = null, $barColors = null)
  118. {
  119. $this->_W = $imgW;
  120. $this->_H = $imgH;
  121. $this->_barHeights = $barHeights;
  122. $this->_barTexts = $barTexts;
  123. $this->_barColors = $barColors;
  124. $this->_paddingBottom = $this->resetPaddingBottom();
  125. $this->_H = $this->resetHeight();
  126. $this->image = imagecreatetruecolor($this->_W, $this->_H);
  127. }
  128. public function stroke()
  129. {
  130. $this->drawBg();
  131. $this->drawBars();
  132. $this->drawTitle();
  133. $this->drawLables();
  134. ob_start();
  135. //header("Content-type: image/png");
  136. //imagepng($this->image);
  137. header("Content-type: " . image_type_to_mime_type(IMAGETYPE_JPEG));
  138. imagejpeg($this->image);
  139. imagedestroy($this->image);
  140. }
  141. public function drawBg()
  142. {
  143. $img_w = $this->_W;
  144. $img_h = $this->_H;
  145. $paddingTop = $this->_paddingTop;
  146. $paddingBottom = $this->_paddingBottom;
  147. $paddingLeft = $this->_paddingLeft;
  148. $paddingRight = $this->_paddingRight;
  149. $rgb = $this->getRGB($this->_bgColor);
  150. $bg = imagecolorallocate($this->image,$rgb['R'], $rgb['G'], $rgb['B']);
  151. imagefilledrectangle($this->image, 0, 0, $img_w, $img_h, $bg);
  152. $side_bg = imagecolorallocatealpha($this->image, 220, 220, 220, 75);
  153. $side_bg2 = imagecolorallocate($this->image, 220, 220, 220);
  154. $border_color = imagecolorallocate($this->image, 190, 190, 190);
  155. $line_color = imagecolorallocate($this->image, 236, 236, 236);
  156. $dial_color = imagecolorallocate($this->image, 131, 131, 131);
  157. $x1 = $paddingLeft;
  158. $y1 = $paddingTop;
  159. $x2 = $img_w - $paddingRight;
  160. $y2 = $img_h - $paddingBottom;
  161. 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<=10; $i++)
  170. {
  171. imageline($this->image, $x1, $y1+($every_h*$i), $x2, $y1+($every_h*$i), $line_color); //画网线
  172. }
  173. $max_h = max($this->_barHeights);
  174. for($i=1; $i<=10; $i++)
  175. {
  176. $value = $max_h - (($max_h/10)*($i-1));
  177. $value = strval($value);
  178. $str_w = strlen($value)*5;
  179. imageline($this->image, $x1-5-3, $y1+10+($every_h*$i), $x1-3+1, $y1+10+($every_h*$i), $dial_color); //画刻度线
  180. imagestring($this->image, 3, $x1-5-3-$str_w-1, $y1+10+($every_h*$i)-5, $value, 0x000000);
  181. }
  182. }
  183. public function drawBars()
  184. {
  185. $counts = count($this->_barHeights);
  186. if (empty($this->_barColors))
  187. {
  188. $color = $this->setColor();
  189. $this->_barColors = array_slice($color, 0, $counts);
  190. //shuffle($this->_barColors);
  191. }
  192. $every_w = ($this->_W - $this->_paddingLeft - $this->_paddingRight)/$counts; //每一段宽
  193. $barL = $every_w;
  194. $barL = ($barL > $this->_barL*1.35+6) ? $this->_barL : $barL/1.35 - 6;
  195. $max_h = max($this->_barHeights);
  196. $ruler_h = $this->_H - $this->_paddingTop - $this->_paddingBottom; //标尺总高度
  197. $stander_h = $ruler_h - ($ruler_h/11); //标尺10等分的高度
  198. $i = 0;
  199. foreach ($this->_barHeights as $val)
  200. {
  201. $h = ($stander_h/$max_h)*$val;
  202. $x = $this->_paddingLeft + ($every_w*$i) + (($every_w - ($barL*1.35))/2);;
  203. $y = $this->_H - $this->_paddingBottom + 10 - $h;
  204. //$t_color = $this->_barColors[$i];
  205. $b_color = $this->_barColors[$i];
  206. //$s_color = $this->_barColors[$i];
  207. $rgb = $this->getRGB($this->_barColors[$i]);
  208. $R = $rgb['R'] * 0.7;
  209. $G = $rgb['G'] * 0.7;
  210. $B = $rgb['B'] * 0.7;
  211. $c1 = $R > 0 ? dechex($R) : '00';
  212. $c2 = $G > 0 ? dechex($G) : '00';
  213. $c3 = $B > 0 ? dechex($B) : '00';
  214. $t_color = $b_color;
  215. $s_color = $c1. $c2 . $c3;
  216. $SingleBar = new SingleBar($x, $y, $h, $barL);
  217. $SingleBar->draw($this->image, $t_color, $b_color, $s_color);
  218. $i++;
  219. }
  220. }
  221. public function drawTitle()
  222. {
  223. if (empty($this->_title))
  224. {
  225. return;
  226. }
  227. $font = 5;
  228. $font_w = imagefontwidth($font);
  229. $len = strlen($this->_title);
  230. $x = ($this->_W + $this->_paddingLeft - $this->_paddingRight)/2;
  231. $x -= ($len*$font_w)/2;
  232. $y = ($this->_paddingTop - $font_w)/2 + 12;
  233. //imagestring($this->image, $font, $x, $y, $title, 0x000000);
  234. imagettftext($this->image, 12, 0, $x, $y, 0x000000, DEFAULT_FONT_PATH, $this->_title);
  235. }
  236. public function drawLables()
  237. {
  238. $x1 = $this->_paddingLeft - 5;
  239. $y1 = $this->_H - $this->_paddingBottom + 20;
  240. $x2 = $this->_W - $this->_paddingRight;
  241. $y2 = $this->_H - 10;
  242. //imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, 0xffffff);
  243. imagerectangle($this->image, $x1, $y1, $x2, $y2, 0x000000);
  244. $space = 5;
  245. $x = $x1 + 3;
  246. $y = $y1 + 3;
  247. foreach ($this->_barTexts as $key => $val)
  248. {
  249. $color = $this->_barColors[$key];
  250. $rgb = $this->getRGB($color);
  251. $bg = imagecolorallocate($this->image,$rgb['R'], $rgb['G'], $rgb['B']);
  252. imagefilledrectangle($this->image, $x, $y, $x+12, $y+12, $bg); //绘12*12的矩形
  253. imagerectangle($this->image, $x, $y, $x+12, $y+12, 0x000000); //绘12*12的矩形框
  254. imagettftext($this->image, 12, 0, $x+12+3, $y+12, 0x000000, DEFAULT_FONT_PATH, $val);
  255. $x += 12 + $space + (strlen($val)*8) + $space;
  256. if ($x + (strlen($val)*8) >= $this->_W - $this->_paddingLeft - $this->_paddingRight)
  257. {
  258. $x = $x1 + 3;
  259. $y = $y + 12 + 3;
  260. }
  261. }
  262. }
  263. public function resetPaddingBottom()
  264. {
  265. $ruler_w = $this->_W - $this->_paddingLeft - $this->_paddingRight;
  266. $label_w = $this->getLableTotalWidth();
  267. $lines = ceil($label_w / $ruler_w);
  268. $h = 12 * $lines + (3 * ($lines + 1)) + 30;
  269. return $h;
  270. }
  271. public function resetHeight()
  272. {
  273. $padding_bottom = $this->resetPaddingBottom();
  274. if ($this->_H - $padding_bottom < 222)
  275. {
  276. return 222 + $padding_bottom;
  277. }
  278. return $this->_H;
  279. }
  280. public function getLableTotalWidth()
  281. {
  282. $counts = count($this->_barTexts);
  283. $space = 5;
  284. $total_len = 0;
  285. foreach ($this->_barTexts as $val)
  286. {
  287. $total_len += strlen($val);
  288. }
  289. $tx_w = ($total_len * 9) + ((12 + 3 + $space) * $counts);
  290. return $tx_w;
  291. }
  292. public function setBg($color)
  293. {
  294. $this->_bgColor = $color;
  295. }
  296. public function setTitle($title)
  297. {
  298. $this->_title = $title;
  299. }
  300. public function setColor()
  301. {
  302. $ar = array('ff', '00', '33', '66', '99', 'cc');
  303. $color = array();
  304. for ($i=0; $i<6; $i++)
  305. {
  306. for ($j=0; $j<6; $j++)
  307. {
  308. for($k=0; $k<6; $k++)
  309. {
  310. $color[] = $ar[$i] . $ar[$j] . $ar[$k];
  311. }
  312. }
  313. }
  314. $color2 = array();
  315. for ($i=1; $i<216; $i += 4)
  316. {
  317. $color2[] = $color[$i];
  318. }
  319. return $color2;
  320. }
  321. public function getRGB($color)
  322. {
  323. $ar = array();
  324. $color = hexdec($color);
  325. $ar['R'] = ($color>>16) & 0xff;
  326. $ar['G'] = ($color>>8) & 0xff;
  327. $ar['B'] = ($color) & 0xff;
  328. return $ar;
  329. }
  330. }
  331. /***/
  332. $bar = new Bar(500, 300, array(600, 300, 30, 500, 400, 250, 350, 360), array('AAAAA', 'BBBBB', 'CCCCC', 'DDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGGG', 'HHHHHHHHH'));
  333. $bar->setTitle('打造完美柱状图!');
  334. $bar->stroke();
  335. /***/
  336. ?>



柱状图, php

人气教程排行