PHP Draw 3D pie chart class function

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags based class code data function graphics image list

The following about the PHP tutorial 3D pie chart Class drawing class function implementation principle is based on//ellipse long axis and other parameters to draw a 3D pie graphics code.

Class chart{

var $a; Oval Long Half axis
var $b; Elliptic Short Half axis
var $DataArray; Each sector of the data
var $ColorArray; The color of each pie is required to be written in hexadecimal but not preceded by 0x
For edges and shadows for black

Function chart ($pa =100, $pb =60, $sData = "100,200,300,400,500,300", $sColor = "EE00FF,DD0000,CCCCCC,CCFF00,00CCFF, Ccff00 ")
{
$this->a= $pa;
$this->b= $PB;
$this->dataarray=split (",", $sData);
$this->colorarray=split (",", $sColor);
}

function SetA ($v) {
$this->a= $v;
}

function Geta () {
return $this->a;
}

function Setb ($v) {
$this->b= $v;
}

function Getb () {
return $this->b;
}

function Setdataarray ($v) {
$this->dataarray=split (",", $v);
}

function Getdataarray ($v) {
return $this->dataarray;
}

function Setcolorarray ($v) {
$this->colorarray=split (",", $v);
}

function Getcolorarray () {
return $this->colorarray;
}


function Drawpie () {
$image =imagecreate ($this->a*2+40, $this->b*2+40);
$PieCenterX = $this->a+10;
$PieCenterY = $this->b+10;
$DoubleA = $this->a*2;
$DoubleB = $this->b*2;
List ($R, $G, $B) =getrgb (0);
$colorBorder =imagecolorallocate ($image, $R, $G, $B);
$DataNumber =count ($this->dataarray);

$DataTotal
for ($i =0; $i < $DataNumber; $i + +) $DataTotal = = = $this->dataarray[$i]; Figure out the data and

Filling back
Imagefill ($image, 0, 0, imagecolorallocate ($image, 0xFF, 0xFF, 0xFF));

    /*


* * Draw every fan


    */


$Degrees = 0;


for ($i = 0; $i &lt; $DataNumber; $i + +) {


$StartDegrees = round ($Degrees);


$Degrees + + ($this-&gt;dataarray[$i]/$DataTotal) *360);


$EndDegrees = round ($Degrees);


$percent = Number_format ($this-&gt;dataarray[$i]/$DataTotal *100, 1);


list ($R, $G, $B) =getrgb (Hexdec ($this-&gt;colorarray[$i));


$CurrentColor =imagecolorallocate ($image, $R, $G, $B);


if ($R &gt;60 and $R &lt;256) $R = $R-60;


if ($G &gt;60 and $G &lt;256) $G = $G-60;


if ($B &gt;60 and $B &lt;256) $B = $B-60;


$CurrentDarkColor =imagecolorallocate ($image, $R, $G, $B);


//Draw fan arc


Imagearc ($image, $PieCenterX, $PieCenterY, $DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentColor);


//Draw a straight line


list ($ArcX, $ArcY) = Pie_point ($StartDegrees, $this-&gt;a, $this-&gt;b);


Imageline ($image, $PieCenterX, $PieCenterY, floor ($PieCenterX + $ArcX), floor ($PieCenterY + $ArcY), $CurrentColor);


//Draw a straight line


list ($ArcX, $ArcY) = Pie_point ($EndDegrees, $this-&gt;a, $this-&gt;b);


imageline ($image, $PieCenterX, $PieCenterY, ceil ($PieCenterX + $ArcX), ceil ($PieCenterY + $ArcY), $CurrentColor);


//Filled fan


$MidPoint = Round (($EndDegrees-$StartDegrees)/2) + $StartDegrees);


list ($ArcX, $ArcY) = Pie_point ($MidPoint, $this-&GT;A*3/4, $this-&GT;B*3/4);


        


Imagefilltoborder ($image, floor ($PieCenterX + $ArcX), floor ($PieCenterY + $ArcY), $CurrentColor, $CurrentColor);


imagestring ($image, 2,floor ($PieCenterX + $ArcX-5), Floor ($PieCenterY + $ArcY-5), $percent. " % ", $colorBorder);

       /Draw Shadow
        if ($ Startdegrees>=0 and $StartDegrees <=180) {
           if ($EndDegrees <=180) {    
                for ($k = 1; $k < $k + +)
                 Imagearc ($image, $PieCenterX, $PieCenterY + $k, $DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $ Currentdarkcolor);
          }else{
                for ($k = 1; $k < $k + +)
                 Imagearc ($image, $PieCenterX, $PieCenterY + $k, $ Doublea, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);
          }

}
}

* * To this script has already had a picture
* * Now need to send it to the browser, it is important to the hair to the browser, let it know is a GIF file. Otherwise, you can only see a bunch of weird gibberish.
*/
Output generated pictures
Header ("Content-type:image/gif");
Imagegif ($image);
Imagedestroy ($image);
}//end Drawpie ()
}//end class


Achieve

$OBJP = new chart ();
$OBJP->drawpie ();

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.