PHP to use GD to draw pie chart, GD draw Pie _php Tutorial

Source: Internet
Author: User

PHP in the use of GD to draw pie chart, GD draw pie


PHP to use GD to draw a pie chart, the class to draw see code:

1 Classchart{2     Private $image;//Defining Images3     Private $title;//Defining Headings4     Private $ydata;//Defining y-axis data5     Private $xdata;//defining X-axis data6     Private $color;//Define a bar chart color7     Private $bgcolor;//define picture background color8     Private $width;//defines the width of the picture9     Private $height;//defines the length of the pictureTen      One     /* A * Constructor Function - * String Title picture title - * Array xdata indexed arrays, x-axis data the * Array ydata indexed arrays, numeric arrays, y-axis data -      */ -     function__construct ($title,$xdata,$ydata) {         -         $this->title =$title; +         $this->xdata =$xdata; -         $this->ydata =$ydata; +         $this->color =Array(' #058DC7 ', ' #50B432 ', ' #ED561B ', ' #DDDF00 ', ' #24CBE5 ', ' #64E572 ', ' #FF9655 ', ' #FFF263 ', ' #6AF9C4 '); A     } at      -     /* - * Public method, set the color of the bar chart - * An array of color arrays, with the element value ' #058DC7 ' This form -      */ -     functionSetbarcolor ($color){ in         $this->color =$color; -     } to      +     /* - * Draw pie chart the      */ *     functionMkpiechart () { $         $sum=Array_sum($this->ydata);//get the sum of all ydata elementsPanax Notoginseng         $start= 0;//Start angle of Arc -         $end= 0;//End angle of Arc the         $pieWidth= 300;//The long axis of an ellipse +         $pieHeight= 220;//the short axis of an ellipse A         $space= 40;//the spacing between the ellipse and the small rectangle the         $margin= 20;//the margins of the picture +         $recWidth= 20;//the width of the small rectangle -         $recHeight= 15;//the height of the small rectangle $         $titleHeight= 50;//the height of the title area $ //Image adaptive width and height -         $this->width =$pieWidth+$this->arraylengthmax ($this->xdata) *10*4/3 +$space+$recWidth+$margin; -         $this->height = (($pieHeight>Count($this->xdata) *25)?$pieHeight:Count($this->xdata) *25) +$titleHeight; the         //coordinates of the ellipse center -         $cx=$pieWidth/2+$margin;Wuyi         $cy=$pieHeight/2+$titleHeight; the          -         $this->image = Imagecreatetruecolor ($this->width,$this->height);//Prepare the canvas Wu         $this->bgcolor = Imagecolorallocate ($this->image,255,255,255);//background color of the picture -Imagefill ($this->image,0,0,$this->bgcolor);//Fill Background About          $ //Set the color of the bar chart -         $color=Array(); -         foreach($this->color as $col) { -             $col=substr($col, 1,strlen($col)-1); A             $red=Hexdec(substr($col, 0,2)); +             $green=Hexdec(substr($col, 2,2)); the             $blue=Hexdec(substr($col, 4,2)); -             $color[] = Imagecolorallocate ($this->image,$red,$green,$blue); $         } the          the         //set the color of the segment, the color of the font, the path to the font the         $lineColor= Imagecolorallocate ($this->image, 0XCC,0XCC,0XCC); the         $fontColor= Imagecolorallocate ($this->image, 0x95,0x8f,0x8f); -         $fontPath= ' FONT/SIMSUN.TTC '; in          the         //Draw Sector arcs the          for($i= 0;$i< 10;$i++) { About             foreach($this->ydata as $key=$val) { the                 $end+ = 360*$val/$sum; theImagefilledarc ($this->image,$cx,$cy-$i,$pieWidth,$pieHeight,$start,$end,$color[$key%Count($this->color)],Img_arc_pie);  the                 $start=$end;  +             } -         } the         Bayi         //draw small rectangles and later text descriptions the         $x 1=$pieWidth+$space; the         $y 1=$titleHeight ; -         foreach($this->ydata as $key=$val) { -Imagefilledrectangle ($this->image,$x 1,$y 1,$x 1+$recWidth,$y 1+$recHeight,$color[$key%Count($this-color)]);  theImagettftext ($this->image,10,0,$x 1+$recWidth+5,$y 1+$recHeight-2,$fontColor,$fontPath,$this->xdata[$key]); the             $y 1+=$recHeight+ 10;  the         } the          -         //Painting title the         $titleStart= ($this->width-5.5*strlen($this->title))/2; theImagettftext ($this->image,11,0,$titleStart, 20,$fontColor,$fontPath,$this-title); the         94         //Output Picture the         Header("Content-type:image/png"); theImagepng ($this-image); the     } 98      About     /* - * Private method to find the maximum length of the element in the array101 * Array of arr strings, must be kanji102      */103     Private functionArraylengthmax ($arr) {104         $length= 0; the         foreach($arr  as $val) {106             $length=strlen($val) >$length?strlen($val) :$length;107         }108         return $length/3;109     }  the     111     // Destructors the     function__destruct () {113Imagedestroy ($this-image); the     } the}

The test code is as follows:

1 $xdata Array (' Test one ', ' Test two ', ' Test three ', ' Test Four ', ' Test Five ', ' Test Six ', ' Test seven ', ' Test Eight ', ' Test Nine '); 2 $ydata Array (89,90,90,23,35,45,56,23,56); 3 $Img New Chart ($title,$xdata,$ydata); 4 $Img->mkpiechart ();

As follows:

http://www.bkjia.com/PHPjc/1063518.html www.bkjia.com true http://www.bkjia.com/PHPjc/1063518.html techarticle PHP using GD to draw a pie chart, GD draw pie in PHP with GD to draw pie chart, draw the class see code: 1 class chart{2 private $image;//definition Image 3 private $title;//define Title 4 Priv ...

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