Histogram implemented by GD in PHP

Source: Internet
Author: User
PHP in the use of GD implementation of the histogram, wrote a histogram of their own class, on the code.

1
 title = $title; $this->xdata = $xdata; $this->ydata = $ydata;  $this->color = Array (' #058DC7 ', ' #50B432 ', ' #ED561B ', ' #DDDF00 ', ' #24CBE5 ', ' #64E572 ', ' #FF9655 ', ' #FFF263 ', ' #6AF9C4 '); 23} 24 25/* 26 * Public method, set the color of the bar chart array color arrays, the element value is ' #058DC7 ' this form of * * * FUNCTI      On Setbarcolor ($color) {$this->color = $color; 31} 32 33/* 34 * Public method, draw bar Chart 35 */36 function Mkbarchart () {PNs $ydataNum = $this->arraynum ($this->ydata);//Get the number of data groupings $max = $th Is->arraymax ($this->ydata); Gets the maximum value of all rendered data $multi = ($max > 100)? $max/100:1; If the maximum data is greater than 100, zoom out, get the $barHeightMulti = 2.2; Percentage of bar high scale $barWidth = (16-2* ($ydataNum-1)) > 10? (16-2* ($ydataNum-1)): 10; The width of the article $barSpace = 16; The spacing between the strips is $chartLeft = (1+strlen ($max)) *12; Set the margin on the left side of the picture $barY = 250; EarlyInitialize the y coordinate of the bar 46//Set the width, height of the picture $this->width = ($ydataNum * $barWidth + $barSpace) *count ($this->xdata) +  $chartLeft;  $this->height = 300; $this->image = Imagecreatetruecolor ($this->width, $this->height); Prepare the canvas $this->bgcolor = imagecolorallocate ($this->image,255,255,255); Background color of the picture 51 52//Set the color of the bar chart $color = Array (); ($this->color as $col) {$col = substr ($col, 1,strlen ($col)-1), and $red = h Exdec (substr ($col, 0,2)); $green = Hexdec (substr ($col, 2,2)); Hexdec $blue = substr ($col, 4,2); $color [] = Imagecolorallocate ($this->image, $red, $green, $blue); 60} 61 62//Set the color of the segment, the color of the font, the path of the font $lineColor = Imagecolorallocate ($this->image, 0xc C,0XCC,0XCC); $fontColor = Imagecolorallocate ($this->image, 0x95,0x8f,0x8f); $fontPath = ' font/simsun.tTC '; Imagefill ($this->image,0,0, $this->bgcolor); Painting Background 68 69//Draw a short line with left and right edges ($i = 0; $i < 6; $i + +) {imageline ($this-& Gt;image, $chartLeft -10, $barY-$barHeightMulti * $max/5/$multi * $i, $this->width, $barY-$barHeightMulti * $max/5/$ multi* $i, $lineColor); Imagestring ($this->image,4,5, $barY-$barHeightMulti * $max/5/$multi * $i -8,floor ($max/5* $i), $fontColor); Imageline ($this->image, $chartLeft -10,30, $chartLeft -10, $barY, $lineColor); Imageline ($this->image, $this->width-1,30, $this->width-1, $barY, $lineColor); 76 77//Draw bar ($this->ydata as $key + $val) {$ydataNum = = 1                 {80//a series of data when Bayi $barX = $chartLeft + 3 + ($barWidth + $barSpace) * $KEY; 82 Imagefilledrectangle ($this->image, $barX, $barY-$barHeightMulti * $val/$multi, $barX + $barWidth, $barY, $color[$key%count ($this->color)]); }elseif ($ydataNum > 1) {84//multiple series of data $cbarSpace = $barSpace + $bar width* ($ydataNum-1); ($val as $ckey + $cval) {$barX = $chartLeft + 3 + $barWidth * $key + $ ckey* ($cbarSpace + $barWidth); Imagefilledrectangle ($this->image, $barX, $barY-$barHeightMulti * $cval/$multi, $barX + $barWidth, $barY , $color [$key%count ($this->color)]);         89} 90} 91 92} 93 94//The value of the x-coordinate of the drawing bar 95  foreach ($this->xdata as $key + $val) {$barX = $chartLeft + ($ydataNum * $barWidth + $barSpace) * $key + $ydataNum * $barWidth/3; Imagettftext ($this->image,10,-45, $barX, $barY +15, $fontColor, $fontPath, $this->xdata[$key]);          98} 99 100//Painting title 101 $titleStart = ($this->width-5.5*strlen ($this->title))/2;102Imagettftext ($this->image,11,0, $titleStart, $fontColor, $fontPath, $this->title); 103 104//Output Picture 105 Header ("Content-type:image/png"); 106 imagepng ($this->image); 107}108 109/*110 * Private method, when the array is a two-tuple array, the length of the statistic array 111 * Array arr to do the array of statistics */113 private function Arraynum ($arr) {$num = 0                 ; Is_array ($arr) {$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; 125}126 127/*128 * Private method, calculating the depth of the array 129 * Array arr arrays */131 private Function arraydepth ($arr) {$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}140}141}      142 return $num; 143}144 145/*146 * Private method, find the maximum value in a group 147 * Array arr numeric array 148 */149 Private Function ArrayMax ($arr) {$depth = $this->arraydepth ($arr); 151 $max = 0;152 if ($dep        th = = 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); 160                         }161}else{162 if ($val > $max) {163          $max = $val; 164}165} 166} 167}168 return $max; 169}170 171 function Arrayaver ($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}180         Return Array_sum ($aver)/count ($aver); 181 182}183//destructor 184 function __destruct () {185 Imagedestroy ($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
  • Related Article

    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.