Find a pie chart or histogram PHP generate class or example

Source: Internet
Author: User
Tags array php code split
Pie chart | Bar chart PHP code:--------------------------------------------------------------------------------
/*-------------------------------------------------------------------------*/
//
Module Name: A 3D pie chart class
//
Author:avenger (avenger@php.net) Last modify:2002-10-30 11:19
Copyright (c) 2002 by Avenger
/*-------------------------------------------------------------------------*/

Common functions Section

Convert Angle to radians
function Deg2arc ($degrees) {
Return ($degrees * (pi ()/180.0));
}

Rgb
function getRGB ($color) {
$R = ($color >>16) & 0xFF;
$G = ($color >>8) & 0xFF;
$B = ($color) & 0xFF;
Return (Array ($R, $G, $B));
}

Gets the value of the x,y point on the ellipse with the Ellipse Heart as (0,0)
function Pie_point ($deg, $va, $VB) {
$x = cos (DEG2ARC ($deg)) * $VA;
$y = sin (Deg2arc ($deg)) * $VB;
Return (Array ($x, $y));
}


3D Pie Chart Class

Class pie3d{

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
var $Fize; Font size
For edges and shadows for black

function Pie3d ($pa =60, $pb =30, $sData = "100,200,300,400,500", $sColor = "EE00FF,DD0000,CCCCCC,CCFF00,00CCFF", $ fontsize=1) {
$this->a= $pa;
$this->b= $PB;
$this->dataarray=split (",", $sData);
$this->colorarray=split (",", $sColor);
$this->fsize= $fontsize;
}

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 () {
$fsize = $this->fsize;
$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

Fill background
Imagefill ($image, 0, 0, imagecolorallocate ($image, 255, 255, 255));

/*
* * Draw every fan
*/

$Degrees = 0;
for ($i = 0; $i < $DataNumber; $i + +) {
$StartDegrees = round ($Degrees);
$Degrees + + ($this->dataarray[$i]/$DataTotal) *360);
$EndDegrees = round ($Degrees);
$percent = Number_format ($this->dataarray[$i]/$DataTotal *100, 1);
List ($R, $G, $B) =getrgb (Hexdec ($this->colorarray[$i));
$CurrentColor =imagecolorallocate ($image, $R, $G, $B);
if ($R >60 and $R <256) $R = $R-60;
if ($G >60 and $G <256) $G = $G-60;
if ($B >60 and $B <256) $B = $B-60;
$CurrentDarkColor =imagecolorallocate ($image, $R, $G, $B);

Draw a fan arc
Imagearc ($image, $PieCenterX, $PieCenterY, $DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentColor);

Draw a straight line
List ($ArcX, $ArcY) = Pie_point ($StartDegrees, $this->a, $this->b);
Imageline ($image, $PieCenterX, $PieCenterY, floor ($PieCenterX + $ArcX), floor ($PieCenterY + $ArcY), $CurrentColor);

Draw a straight line
List ($ArcX, $ArcY) = Pie_point ($EndDegrees, $this->a, $this->b);
Imageline ($image, $PieCenterX, $PieCenterY, ceil ($PieCenterX + $ArcX), ceil ($PieCenterY + $ArcY), $CurrentColor);

Fill sector
$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, $fsize, floor ($PieCenterX + $ArcX-5), Floor ($PieCenterY + $ArcY-5), $percent. " % ", $colorBorder);

Painting shadows
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);
}
}
}

Output generated pictures
Imagepng ($image, ' consture.png ');
Imagedestroy ($image);
}//end Drawpie ()
}//end class
$pie = new Pie3d;
$pie->pie3d ($pa =300, $pb =150, $sData = "100,200,300,400,500", $sColor = "Ee00ff,dd0000,cccccc,ccff00,ddddaa", $ FONTSIZE=5);
$pie->drawpie ();
Echo '
?>


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.