PHP to create an intelligent bar chart program, for reporting, etc.

Source: Internet
Author: User
Tags foreach strlen

This article mainly introduces PHP to create intelligent histogram program, for the report and other relevant information, the need for friends can refer to the

PHP to create an intelligent bar chart program, for reporting, etc.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 30 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359-360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 <?php/*** * @project Bar Graph program * @license GPL * @package * @file grapbar.php * @date 2007-4-3 * @version 1.0 * @last Modified   * Define bar Chart (column chart) class * * Note that before using, make sure that the font path exists and allow access, and if so, check the Open_basedir entry in the php.ini configuration if you do not have this path to add, or set up in the program containing * * Intelligent histogram procedure for reporting, etc. * ***/  define ("Default_font_path", "C:/windows/fonts/simhei.ttf"); Class Singlebar {Private $_x, private $_y, private $_h, public $_l =, private $_w = null; private $_srcpoints = Array ( ); Private $_points = Array ();   Public Function __construct ($x, $y, $h, $l =, $w = null) {$this->_x = $x; $this->_y = $y; $this->_h = $h; $this->_l = $l; $this->_w = $w; $this->_srcpoints = $this->getsrcpoints (); $this->_points = $this->getpoints ();   Public Function getsrcpoints () {return Array (array ($this->_x, $this->_y), Array ($this->_x+ $this-> _l, $this->_y), Array ($this->_x+ (1.35* $this->_l), $this->_y-(0.35* $this->_l)), Array ($this->_x+ ( 0.35* $this->_l), $this->_y-(0.35* $this->_l)), Array ($this->_x, $this->_y+ $this->_h), Array ($this->_x+ $this->_ L, $this->_y+ $this->_h), Array ($this->_x+ (1.35* $this->_l), $this->_y+ $this->_h-(0.35* $this-> _l))); }   Public Function getpoints () {$points = array (); foreach ($this->_srcpoints as $key => $val) {$points [] = $ VAL[0]; $points [] = $val [1]; return $points; }   Public Function gettoppoints () {return array_slice ($this->_points, 0, 8);//Top coordinate}   public Function Getb Elowpoints () {return Array_merge (Array_slice ($this->_points, 0, 2), Array_slice ($this->_points, 8, 4), Array_ Slice ($this->_points, 2, 2)); Lower coordinates}   Public Function getrightsidepoints () {return Array_merge (Array_slice ($this->_points, 2, 2), Array_slic E ($this->_points, 4), Array_slice ($this->_points, 4, 2)); Right coordinate}   Public function Draw ($image, $topColor, $belowColor, $sideColor, $borderColor = null, $type = ' left ') {if (Is_null ($bOrdercolor) {$borderColor = 0XCCCCCC;}   $top _rgb = $this->getrgb ($topColor); $below _rgb = $this->getrgb ($belowColor); $side _rgb = $this->getrgb ($sideColor); $top _color = imagecolorallocate ($image, $top _rgb[' R '], $top _rgb[' G '], $top _rgb[' B ']); $below _color = imagecolorallocate ($image, $below _rgb[' R '], $below _rgb[' G '], $below _rgb[' B ']); $side _color = imagecolorallocate ($image, $side _rgb[' R '], $side _rgb[' G '], $side _rgb[' B ']);   Imagefilledpolygon ($image, $this->gettoppoints (), 4, $top _color); Draw Top Imagepolygon ($image, $this->gettoppoints (), 4, $borderColor); Draw Top Edge   Imagefilledpolygon ($image, $this->getbelowpoints (), 4, $below _color); Draw below Imagepolygon ($image, $this->getbelowpoints (), 4, $borderColor); Draw the following sideline   if ($type = = ' Left ') {Imagefilledpolygon ($image, $this->getrightsidepoints (), 4, $side _color);/Draw Right side I Magepolygon ($image, $this->getrightsidepoints (), 4, $borderColor); Draw Side Edge}}   Public function getRGB ($color) {$ar = Array (); $color = Hexdec ($color); $ar [' R '] = ($color >>16) & 0xFF; $ar [' G '] = ($color >>8) & 0xFF; $ar [' B '] = ($color) & 0xFF; return $ar; }   class Bar {private $_w; private $_h; Private $_bgcolor = "ffffff"; Private $_barheights = Array (); private $_b artexts = Array (); Private $_barcolors = Array (); Public $_title; Public $_paddingtop = 30; Public $_paddingbottom = 100; Public $_paddingleft = 45; Public $_paddingright = 2; Public $_barl = 50; Public $image;   Public Function __construct ($imgW, $imgH, $barHeights, $barTexts = null, $barColors = null) {$this->_w = $imgW; $this->_h = $imgH; $this->_barheights = $barHeights; $this->_bartexts = $barTexts; $this->_barcolors = $barColors; $this->_paddingbottom = $this->resetpaddingbottom (); $this->_h = $this->resetheight (); $this->image = Imagecreatetruecolor ($this->_w, $this->_h); }   Public Function stroke () {$this->drawbg (); $this->drawbars (); $this->drawtitle (); $this->drawlables (); Ob_start (); Header ("Content-type:image/png"); Imagepng ($this->image); Header ("Content-type:"). Image_type_to_mime_type (Imagetype_jpeg)); Imagejpeg ($this->image); Imagedestroy ($this->image); }   Public Function drawbg () {$img _w = $this->_w; $img _h = $this->_h; $paddingTop = $this->_paddingtop; $pa Ddingbottom = $this->_paddingbottom; $paddingLeft = $this->_paddingleft; $paddingRight = $this->_paddingright; $rgb = $this->getrgb ($this->_bgcolor); $BG = Imagecolorallocate ($this->image, $rgb [' R '], $rgb [' G '], $rgb [' B ']); Imagefilledrectangle ($this->image, 0, 0, $img _w, $img _h, $BG); $side _BG = Imagecolorallocatealpha ($this->image, 220, 220, 220, 75); $side _bg2 = imagecolorallocate ($this->image, 220, 220, 220); $border _color = imagecolorallocate ($this->image, 190, 190, 190); $line _color = imagecolorallocate ($this->image, 236, 236, 236); $dial _color = imagecolorallocate ($this->image, 131, 131, 131);   $x 1 = $paddingLeft; $y 1 = $paddingTop; $x 2 = $img _w-$paddingRight; $y 2 = $img _h-$paddingBottom; Imagerectangle ($this->image, $x 1, $y 1, $x 2, $y 2, $border _color); 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); 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); 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); 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); Imageline ($this->image, $x 1, $y 2, $x 2, $y 2, $side _bg2);   $total _h = $img _h-$paddingTop-$paddingBottom; $every _h = $total _H/11; For ($i =1 $i <=10; $i + +) {imageline ($this->image, $x 1, $y 1+ ($every _h* $i), $x 2, $y 1+ ($every _h* $i), $line _color); Draw Cable}   $max _h = max ($this->_barheights); For ($i =1 $i <=10; $i + +) {$value = $max _h-(($max _H/10) * ($i-1)); $value = Strval ($value); $stR_w = strlen ($value) *5; 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 tick-Mark line imagestring ($this->image, 3, $x 1-5-3-$str _w-1, $y 1+10+ ($every _h* $i) -5, $value, 0x000000); }     Public function drawbars () {$counts = count ($this->_barheights); if (Empty ($this->_barcolors)) { $color = $this->setcolor (); $this->_barcolors = array_slice ($color, 0, $counts); Shuffle ($this->_barcolors); $every _w = ($this->_w-$this->_paddingleft-$this->_paddingright)/$counts; Each section of width $barL = $every _w; $barL = ($barL > $this->_barl*1.35+6)? $this->_barl: $barL/1.35-6; $max _h = max ($this->_barheights); $ruler _h = $this->_h-$this->_paddingtop-$this->_paddingbottom; Ruler total height $stander _h = $ruler _h-($ruler _H/11); Ruler 10 equal height $i = 0; foreach ($this->_barheights as $val) {$h = ($stander _h/$max _h) * $val $x = $this->_paddingleft + ($every _w* $i) + (($ Every_w-($barL *1.35))/2);; $y = $this->_h-$this->_paddingbottom + 10-$h; $t _color = $this->_barcolors[$i]; $b _color = $this->_barcolors[$i]; $s _color = $this->_barcolors[$i];     $RGB = $this->getrgb ($this->_barcolors[$i]); $R = $rgb [' R '] * 0.7; $G = $rgb [' G '] * 0.7; $B = $rgb [' B '] * 0.7;   $c 1 = $R > 0? Dechex ($R): ' 00 '; $c 2 = $G > 0? Dechex ($G): ' 00 '; $c 3 = $B > 0? Dechex ($B): ' 00 ';   $t _color = $b _color; $s _color = $c 1. $c 2. $c 3;   $SingleBar = new Singlebar ($x, $y, $h, $barL); $SingleBar->draw ($this->image, $t _color, $b _color, $s _color); $i + +; }   Public Function Drawtitle () {if (Empty ($this->_title)) {return;} $font = 5; $font _w = Imagefontwidth ($fon T); $len = strlen ($this->_title); $x = ($this->_w + $this->_paddingleft-$this->_paddingright)/2; $x-= ($len * $font _w)/2; $y = ($this->_paddingtop-$font _w)/2 + 12; Imagestring ($this->image, $font, $x, $y, $title, 0x000000); Imagettftext ($this->image, 12, 0, $x, $y, 0x000000, Default_font_path, $this->_title); }   Public Function Drawlables () {$x 1 = $this->_paddingleft-5; $y 1 = $this->_h-$this->_paddingbottom + 20; $x 2 = $this->_w-$this->_paddingright; $y 2 = $this->_h-10; Imagefilledrectangle ($this->image, $x 1, $y 1, $x 2, $y 2, 0XFFFFFF); Imagerectangle ($this->image, $x 1, $y 1, $x 2, $y 2, 0x000000); $space = 5; $x = $x 1 + 3; $y = $y 1 + 3; foreach ($this->_bartexts as $key => $val) {$color = $this->_barcolors[$key]; $rgb = $this->getrgb ($color); $ BG = imagecolorallocate ($this->image, $rgb [' R '], $rgb [' G '], $rgb [' B ']); Imagefilledrectangle ($this->image, $x, $y, $x +12, $y +12, $BG); Painted 12*12 rectangular imagerectangle ($this->image, $x, $y, $x +12, $y +12, 0x000000); Painted 12*12 Rectangular frame imagettftext ($this->image, 0, $x +12+3, $y +12, 0x000000, Default_font_path, $val); $x + + $space + (strlen ($val) *8) + $space; if ($x + (strlen ($val) *8) >= $this->_w-$this->_paddingleft-$this->_paddingright) {$x = $x 1 + 3; $y = $y + 12 + 3;}} }   Public Function Resetpaddingbottom () {$ruler _w = $this->_w-$this->_paddingleft-$this->_paddingright ; $label _w = $this->getlabletotalwidth (); $lines = ceil ($label _w/$ruler _w); $h = $lines + (3 * ($lines + 1)) + 30; return $h; }   Public Function resetheight () {$padding _bottom = $this->resetpaddingbottom (); if ($this->_h-$padding _bot Tom < 222) {return 222 + $padding _bottom} return $this->_h; }     Public function getlabletotalwidth () {$counts = count ($this->_bartexts); $space = 5; $total _len = 0; F Oreach ($this->_bartexts as $val) {$total _len + = strlen ($val);}   $tx _w = ($total _len * 9) + ((+ 3 + $space) * $counts); return $TX _w; }   Public Function SETBG ($color) {$this->_bgcolor = $color;}   Public Function Settitle ($title) {$this-&G T;_title = $title;   Public Function SetColor () {$ar = array (' FF ', ', ', ', ', ', ', ', ', ', ' cc '); $color = Array (); For ($i =0 $i <6; $i + +) {for ($j =0; $j <6; $j + +) {for ($k =0; $k <6; $k + +) {$color [] = $ar [$i]. $ar [$j]; }}   $color 2 = array (); For ($i =1 $i <216; $i + + 4) {$color 2[] = $color [$i];}   return $color 2; }   Public Function getRGB ($color) {$ar = array (); $color = Hexdec ($color); $ar [' R '] = ($color >>16) & 0xFF ; $ar [' G '] = ($color >>8) & 0xFF; $ar [' B '] = ($color) & 0xFF; return $ar; }  /***/$bar = new Bar (+, +, 360), Array (' AAAAA ', ' bbbbb ', ' CCCCC ', '), ' Ddddd ', ' eeeeee ', ' FFFFFF ', ' ggggggg ', ' hhhhhhhhh '); $bar->settitle (' Create a perfect bar chart! '); $bar->stroke (); /***/?>

The above is the entire contents of this article, I hope you can enjoy.

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.