Home Backend Development PHP Tutorial php draws histogram

php draws histogram

Jul 25, 2016 am 08:43 AM

  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 bar chart ( Column chart) Class
  10. *
  11. * Note, before use, please ensure that the font path exists and allows access. If an error occurs, also check the open_basedir item in the php.ini configuration. If there is no such path, please add it, or set it 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 = 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


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles