Heim >Backend-Entwicklung >PHP-Tutorial >php绘制柱状图

php绘制柱状图

WBOY
WBOYOriginal
2016-07-25 08:43:001564Durchsuche
  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 {
  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. //shuffle($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. $c2 . $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. $space = 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->_paddingRight;
  264. $label_w = $this->getLableTotalWidth();
  265. $lines = ceil($label_w / $ruler_w);
  266. $h = 12 * $lines + (3 * ($lines + 1)) + 30;
  267. return $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. $space = 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. return $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[$j] . $ar[$k];
  305. }
  306. }
  307. }
  308. $color2 = array();
  309. for ($i=1; $i {
  310. $color2[] = $color[$i];
  311. }
  312. return $color2;
  313. }
  314. public function getRGB($color)
  315. {
  316. $ar = array();
  317. $color = hexdec($color);
  318. $ar['R'] = ($color>>16) & 0xff;
  319. $ar['G'] = ($color>>8) & 0xff;
  320. $ar['B'] = ($color) & 0xff;
  321. return $ar;
  322. }
  323. }
  324. /***/
  325. $bar = new Bar(500, 300, array(600, 300, 30, 500, 400, 250, 350, 360), array('AAAAA', 'BBBBB', 'CCCCC', 'DDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGGG', 'HHHHHHHHH'));
  326. $bar->setTitle('打造完美柱状图!');
  327. $bar->stroke();
  328. /***/
  329. ?>
复制代码



柱状图, php


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