PHP in the use of GD implementation of the histogram, PHPGD implementation of the bar chart _php tutorial

Source: Internet
Author: User

PHP in the use of GD implementation of the bar chart, PHPGD implementation of the bar chart


PHP in the use of GD implementation of the histogram, wrote a histogram of their own class, on the code.

1 
 Php2  Classchart{3     Private $image;//Defining Images4     Private $title;//Defining Headings5     Private $ydata;//Defining y-axis data6     Private $xdata;//defining X-axis data7     Private $color;//Define a bar chart color8     Private $bgcolor;//define picture background color9     Private $width;//defines the width of the pictureTen     Private $height;//defines the length of the picture One      A     /* - * Constructor Function - * String Title picture title the * Array xdata indexed arrays, x-axis data - * Array ydata indexed arrays, numeric arrays, y-axis data -      */ -     function__construct ($title,$xdata,$ydata) {         +         $this->title =$title; -         $this->xdata =$xdata; +         $this->ydata =$ydata; A         $this->color =Array(' #058DC7 ', ' #50B432 ', ' #ED561B ', ' #DDDF00 ', ' #24CBE5 ', ' #64E572 ', ' #FF9655 ', ' #FFF263 ', ' #6AF9C4 '); at     } -      -     /* - * Public method, set the color of the bar chart - * An array of color arrays, with the element value ' #058DC7 ' This form -      */ in     functionSetbarcolor ($color){ -         $this->color =$color; to     } +      -     /* the * Public method, draw bar chart *      */ $     functionMkbarchart () {Panax Notoginseng         $ydataNum=$this->arraynum ($this->ydata);//get the number of data groupings -         $max=$this->arraymax ($this->ydata);//gets the maximum value of all rendered data the         $multi= ($max> 100)?$max/100:1;//If the maximum data is greater than 100, zoom out and get +         $barHeightMulti= 2.2;//scale of high bar scaling A         $barWidth= (16-2* ($ydataNum-1)) > 10? (16-2* ($ydataNum-1)): 10;//the width of the bar the         $barSpace= 16;//spacing between the bars +         $chartLeft= (1 +strlen($max)) *12;//set margin to the left of the picture -          $         $barY= 250;//Initialize the coordinates of y of a bar chart $ //Set the width and height of the picture -         $this->width = ($ydataNum*$barWidth+$barSpace)*Count($this->xdata) +$chartLeft;  -         $this->height = 300;  the         $this->image = Imagecreatetruecolor ($this->width,$this->height);//Prepare the canvas -         $this->bgcolor = Imagecolorallocate ($this->image,255,255,255);//background color of the pictureWuyi          the //Set the color of the bar chart -         $color=Array(); Wu         foreach($this->color as $col) { -             $col=substr($col, 1,strlen($col)-1); About             $red=Hexdec(substr($col, 0,2)); $             $green=Hexdec(substr($col, 2,2)); -             $blue=Hexdec(substr($col, 4,2)); -             $color[] = Imagecolorallocate ($this->image,$red,$green,$blue); -         } A          +         //set the color of the segment, the color of the font, the path to the font the         $lineColor= Imagecolorallocate ($this->image, 0XCC,0XCC,0XCC); -         $fontColor= Imagecolorallocate ($this->image, 0x95,0x8f,0x8f); $         $fontPath= ' FONT/SIMSUN.TTC '; the          theImagefill ($this->image,0,0,$this->bgcolor);//Painting Background the          the //Draw the short and left edges of the drawing -          for($i= 0;$i< 6;$i++ ) { inImageline ($this->image,$chartLeft-10,$barY-$barHeightMulti*$max/5/$multi*$i,$this->width,$barY-$barHeightMulti*$max/5/$multi*$i,$lineColor); theImagestring ($this->image,4,5,$barY-$barHeightMulti*$max/5/$multi*$i-8, Floor($max/5*$i),$fontColor); the         }         AboutImageline ($this->image,$chartLeft-10,30,$chartLeft-10,$barY,$lineColor); theImageline ($this->image,$this->width-1,30,$this->width-1,$barY,$lineColor); the          the         //the bar of a drawing drawing +         foreach($this->ydata as $key=$val) { -             if($ydataNum= = 1) { the                 //when a series of dataBayi                 $barX=$chartLeft+ 3 + ($barWidth+$barSpace)*$key; theImagefilledrectangle ($this->image,$barX,$barY-$barHeightMulti*$val/$multi,$barX+$barWidth,$barY,$color[$key%Count($this-color)]); the}ElseIf($ydataNum> 1) { -                 //multiple series of data when -                 $cbarSpace=$barSpace+$barWidth*($ydataNum-1); the                 foreach($val  as $ckey=$cval) { the                     $barX=$chartLeft+ 3 +$barWidth*$key+$ckey*($cbarSpace+$barWidth); theImagefilledrectangle ($this->image,$barX,$barY-$barHeightMulti*$cval/$multi,$barX+$barWidth,$barY,$color[$key%Count($this-color)]); the                 } -             } the              the         } the                 94         //The value of the x-coordinate of a drawing bar chart the         foreach($this->xdata as $key=$val) { the             $barX=$chartLeft+ ($ydataNum*$barWidth+$barSpace)*$key+$ydataNum*$barWidth/3; theImagettftext ($this->image,10,-45,$barX,$barY+15,$fontColor,$fontPath,$this->xdata[$key]);98         } About          -         //Painting title101         $titleStart= ($this->width-5.5*strlen($this->title))/2;102Imagettftext ($this->image,11,0,$titleStart, 20,$fontColor,$fontPath,$this-title);103         104         //Output Picture the         Header("Content-type:image/png");106Imagepng ($this-image);107     }108     109     /* the * Private method, when the array is a two-tuple array, the length of the statistic array111 * Array arr to do the statistics of the      */113     Private functionArraynum ($arr) { the          $num= 0; the          if(Is_array($arr)) { the             $num++;117              for($i= 0;$i<Count($arr);$i++){118                 if(Is_array($arr[$i])) {119                     $num=Count($arr); -                      Break;121                 }122             }123          }124          return $num; the     }126     127     /* - * Private method, calculate the depth of the array129 * array of arr arrays the      */131     Private functionArraydepth ($arr) { the          $num= 0;133          if(Is_array($arr)) {134             $num++;135              for($i= 0;$i<Count($arr);$i++){136                 if(Is_array($arr[$i])) {137                     $num+=$this->arraydepth ($arr[$i]);138                      Break;139                 } $             }141          }142          return $num;143     }144     145     /*146 * Private method, find the maximum value in a group147 * array of arr numbers148      */149      Private functionArrayMax ($arr) { Max         $depth=$this->arraydepth ($arr);151         $max= 0; the         if($depth= = 1) {153             Rsort($arr);154             $max=$arr[0]; 155}ElseIf($depth> 1) {156             foreach($arr  as $val) {157                 if(Is_array($val)) {158                     if($this->arraymax ($val) >$max) {159                         $max=$this->arraymax ($val); the                     }161}Else{                    162                     if($val>$max){163                         $max=$val;164                     }165                 }    166             }            167         }168         return $max;169     } the     171     functionArrayaver ($arr) {172         $aver=Array();173         foreach($arr  as $val) {174             if(Is_array($val)) {175                 $aver=Array_merge($aver,$val);176}Else{177                 $aver[] =$val;178             }179         } the         return Array_sum($aver)/Count($aver);181         182     }183     // Destructors184     function__destruct () {185Imagedestroy ($this-image);186     }187  }188? >

This class can be implemented to draw a series of histogram and a series of histogram, as follows:
A column chart of a series
















Histogram of multiple series

http://www.bkjia.com/PHPjc/1063027.html www.bkjia.com true http://www.bkjia.com/PHPjc/1063027.html techarticle PHP in the use of GD implementation of the histogram, PHPGD implementation of the histogram in PHP using GD implementation of the histogram, write a histogram of their own class, on the code. 1? PHP 2 Class chart{3 private $ima ...

  • 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.