PHP to create an intelligent histogram program for reports and other _php examples

Source: Internet
Author: User
Tags strlen

PHP to create intelligent bar chart programs for reports, etc.

<?php/*** * @project Bar Graph program * @license GPL * @package * @file grapbar.php * @date 2007-4-3 * @version 1.0 * @last modified * definition bar chart (column chart) class * Note, make sure the font path exists and allow access before using, and if so, check the Open_basedir in the php.ini configuration if there is no way 
Path please add, or set up in the program contains * * * Intelligent histogram program, for statements such as * ***/define ("Default_font_path", "C:/windows/fonts/simhei.ttf"); 
 Class Singlebar {private $_x; 
 Private $_y; 
 Private $_h; 
 Public $_l = 50; 
 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 (); The Public Function getsrcpoints () {return Array (array ($this->_x, $this->_y), Array ($this-&G t;_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))); 
 The 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 ge Tbelowpoints () {return Array_merge (Array_slice ($this->_points, 0, 2), Array_slice ($this->_points, 8, 4), Array_ Slice ($this->_points, 2, 2)); Lower coordinate} public function getrightsidepoints () {return Array_merge (Array_slice ($this->_points, 2, 2), array_s Lice ($this->_points, 4), Array_slice ($this->_points, 4, 2)); RightSide coordinates} public function draw ($image, $topColor, $belowColor, $sideColor, $borderColor = null, $type = ' left ') {i 
 F (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 the right side of the 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); 
 The 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 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); 
 The 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 + +; }
 The 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); 
 The 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; The Public Function Resetpaddingbottom () {$ruler _w = $this->_w-$this->_paddingleft-$this->_pa 
 Ddingright; 
 $label _w = $this->getlabletotalwidth (); 
 $lines = ceil ($label _w/$ruler _w); 
 $h = $lines + (3 * ($lines + 1)) + 30; 
 return $h; 
 The Public Function resetheight () {$padding _bottom = $this->resetpaddingbottom (); 
 if ($this->_h-$padding _bottom < 222) {return 222 + $padding _bottom; 
} return $this->_h; 
 The 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; 
 The Public Function Settitle ($title) {$this->_title = $title; 
 The 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]. 
  $ar [$k]; 
 $color 2 = Array ()}} 
 For ($i =1 $i <216; $i + + 4) {$color 2[] = $color [$i]; 
 return $color 2; 
 The 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 (m, D, M, 360), Array (' AAAAA ', ' bbbbb ', ' CCCCC ', '). 
DDDD ', ' 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.