PHP Drawing Bar Chart

Source: Internet
Author: User
  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. * Define Column chart (column chart) class
  10. *
  11. * Note that before using, make sure that the font path exists and allow access, if there is an error, also check the Open_basedir entry in the php.ini configuration, if this path is not added, or set in the program contains
  12. *
  13. * Intelligent Bar Chart Program for reports, etc.
  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 = a, $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); Top coordinates
  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)); Bottom coordinate
  65. }
  66. Public Function getrightsidepoints ()
  67. {
  68. Return Array_merge (Array_slice ($this->_points, 2, 2), Array_slice ($this->_points, ten, 4), Array_slice ($this- >_points, 4, 2)); Right coordinate
  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); Draw the top face
  83. Imagepolygon ($image, $this->gettoppoints (), 4, $borderColor); Draw Top Edge
  84. Imagefilledpolygon ($image, $this->getbelowpoints (), 4, $below _color); Draw below
  85. Imagepolygon ($image, $this->getbelowpoints (), 4, $borderColor); Draw the bottom edge
  86. if ($type = = ' Left ')
  87. {
  88. Imagefilledpolygon ($image, $this->getrightsidepoints (), 4, $side _color); Draw right Side
  89. Imagepolygon ($image, $this->getrightsidepoints (), 4, $borderColor); Draw side edges
  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. $x 1 = $paddingLeft;
  158. $y 1 = $paddingTop;
  159. $x 2 = $img _w-$paddingRight;
  160. $y 2 = $img _h-$paddingBottom;
  161. Imagerectangle ($this->image, $x 1, $y 1, $x 2, $y 2, $border _color);
  162. Imagefilledpolygon ($this->image, Array ($x 1-5, $y 1+10, $x 1-5, $y 2+10, $x 1, $y 2, $x 1, $y 1), 4, $side _BG);
  163. Imagepolygon ($this->image, Array ($x 1-5, $y 1+10, $x 1-5, $y 2+10, $x 1, $y 2, $x 1, $y 1), 4, $border _color);
  164. Imagefilledpolygon ($this->image, Array ($x 1-5, $y 2+10, $x 2-5, $y 2+10, $x 2, $y 2, $x 1, $y 2), 4, $side _BG);
  165. Imagepolygon ($this->image, Array ($x 1-5, $y 2+10, $x 2-5, $y 2+10, $x 2, $y 2, $x 1, $y 2), 4, $border _color);
  166. Imageline ($this->image, $x 1, $y 2, $x 2, $y 2, $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, $x 1, $y 1+ ($every _h* $i), $x 2, $y 1+ ($every _h* $i), $line _color); Draw Network Cable
  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);
  179. Imageline ($this->image, $x 1-5-3, $y 1+10+ ($every _h* $i), $x 1-3+1, $y 1+10+ ($every _h* $i), $dial _color); Draw a tick mark
  180. Imagestring ($this->image, 3, $x 1-5-3-$str _w-1, $y 1+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; Each section is wide
  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; Ruler Total Height
  197. $stander _h = $ruler _h-($ruler _H/11); Height of ruler 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. $c 1 = $R > 0? Dechex ($R): ' 00 ';
  212. $c 2 = $G > 0? Dechex ($G): ' 00 ';
  213. $c 3 = $B > 0? Dechex ($B): ' 00 ';
  214. $t _color = $b _color;
  215. $s _color = $c 1. $c 2. $c 3;
  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, 0, $x, $y, 0x000000, Default_font_path, $this->_title);
  235. }
  236. Public Function Drawlables ()
  237. {
  238. $x 1 = $this->_paddingleft-5;
  239. $y 1 = $this->_h-$this->_paddingbottom + 20;
  240. $x 2 = $this->_w-$this->_paddingright;
  241. $y 2 = $this->_h-10;
  242. Imagefilledrectangle ($this->image, $x 1, $y 1, $x 2, $y 2, 0XFFFFFF);
  243. Imagerectangle ($this->image, $x 1, $y 1, $x 2, $y 2, 0x000000);
  244. $space = 5;
  245. $x = $x 1 + 3;
  246. $y = $y 1 + 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); Draw a 12*12 Rectangle
  253. Imagerectangle ($this->image, $x, $y, $x +12, $y +12, 0x000000); Draw a rectangular box of 12*12
  254. Imagettftext ($this->image, 0, $x +12+3, $y +12, 0x000000, Default_font_path, $val);
  255. $x + = + $space + (strlen ($val) *8) + $space;
  256. if ($x + (strlen ($val) *8) >= $this->_w-$this->_paddingleft-$this->_paddingright)
  257. {
  258. $x = $x 1 + 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 = * $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) + ((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 ', ' xx ', ' + ', ' n ', ' A ', ' 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. $color 2 = array ();
  315. for ($i =1; $i <216; $i + = 4)
  316. {
  317. $color 2[] = $color [$i];
  318. }
  319. return $color 2;
  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 (+, (), array (+, +,--), Array (' AAAAA ', ' bbbbb ', ' ccccc ', ' ddddd ', ' eeeeee ', ' FFFFFF ', ' ggggggg ', ' hhhhhhhhh ');
  333. $bar->settitle (' Create the perfect histogram! ');
  334. $bar->stroke ();
  335. /***/
  336. ?>
Copy Code


Bar chart, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.