PHP Graphing Column Chart

Source: Internet
Author: User
PHP Drawing Bar Chart

Effect:


 _x = $x;  $this->_y = $y;  $this->_h = $h;  $this->_l = $l;  $this->_w = $w;  $this->_srcpoints = $this->getsrcpoints (); $this->_points = $this->getpoints (); The Public Function getsrcpoints () {return Array (array ($this->_x, $this->_y), Array ($this-&gt ; _x+ $this->_l, $this->_y), Array ($this->_x+ (1.35* $this->_l), $this->_y-(0.35* $this->_l), arr Ay ($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-&G t;_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 Coordinates} public FUnction getbelowpoints () {return Array_merge (Array_slice ($this->_points, 0, 2), Array_slice ($this->_points, 8, 4 ), Array_slice ($this->_points, 2, 2)); The following coordinates} public function getrightsidepoints () {return Array_merge (Array_slice ($this->_points, 2, 2), Array_slice ($thi S->_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); DrawTop Face 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 edge if ($type = = ' Left ') {Imagefilledpolygon ($image, $this->getrightsidepoints (), 4, $side _color);//Draw Right Side Imagepolygon ($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 $_bartexts = 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;  $paddingBottom = $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 Network 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); 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 lines 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 wide $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 The height of the equal $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 = $C1. $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 ($font);  $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, 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 box 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-&Gt;_h-$padding _bottom < 222) {return 222 + $padding _bottom; } return $this->_h;  Public Function Getlabletotalwidth () {$counts = count ($this->_bartexts);  $space = 5;  $total _len = 0;  foreach ($this->_bartexts as $val) {$total _len + = strlen ($val);  } $tx _w = ($total _len * 9) + ((3 + + $space) * $counts); return $TX _w;  The Public Function SETBG ($color) {$this->_bgcolor = $color;}  Public Function Settitle ($title) {$this->_title = $title;}  Public Function SetColor () {$ar = array (' FF ', ' xx ', ' + ', ' $ '), ' A ', ' 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].    $ar [$k];  }}} $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 (+, (+, +, +, +, +), array (' AAAAA ', ' bbbbb ', ' ccccc ', ' ddddd ', ' E Eeeee ', ' FFFFFF ', ' ggggggg ', ' hhhhhhhhh '); $bar->settitle (' Create the perfect histogram! '); $bar->stroke ();/***/?>


1 Floor u0100288694 hours ago
the landlord summed up very detailed, study. NN welcome attention, Mutual Exchange: http://blog.csdn.net/u010028869
  • 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.