首頁  >  文章  >  後端開發  >  求一個餅狀圖或長條圖php生成類別或例子

求一個餅狀圖或長條圖php生成類別或例子

高洛峰
高洛峰原創
2016-12-01 15:06:26955瀏覽

PHP代碼:---------------------------------------------- ----------------------------------
/*------------- -------------------------------------------------- ----------*/
//
// Module Name: 一個3D的圓餅圖類別
//
// Author:Avenger(avenger@php.net) Last Modify: 2002-10- 30 11:19
// Copyright (c) 2002 by Avenger
/*--------------------------------- ----------------------------------------*/

//公用函數部分

//把角度轉換為弧度
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)) ;
}

// 取得在橢圓心為(0,0)的橢圓上x,y點的值
function pie_point($deg,$va,$vb){
$x= cos(deg2Arc($deg )) * $va;
$y= sin(deg2Arc($deg)) * $vb;
return (array($x, $y));
}


//3D圓餅圖類別

class3d {

var $a; //橢圓長半軸
var $b; //橢圓短半軸
var $DataArray; //每個扇形的資料
var $ColorArray; //每個扇形的顏色要求按照十六進位書寫但前面不加0x
var $Fize; //字體大小
//為邊緣及陰影為黑色

function Pie3d($pa=60,$pb=30,$sData="100,200,300,400,$pb=30,$sData="100,200,300,400,$500", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff",$fontsize=1) {
$this->a=$pa;
$this->b=$pb;
$this->DataArsplit( ",",$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;
}
(){
return $this->b;
}

function setDataArray($v){
$this->DataArray=split(",",$v);
}

function getDataArray($v);
}

function getDataArray($v);
}

function getDataArray($v);
}

function getDataArray($v);
}

function getDataArray($v)。
return $this->DataArray;
}

function setColorArray($v){
$this->ColorArray=split(",",$v);
}

function getColorArturn) ${
>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;$iDataArray[$i]; //算出資料和

//填充背景
imagefill($image , 0, 0, imagecolorallocate($image, 255, 255, 255));

/*
** 畫每一個扇形
*/

$Degrees = 0;
for ($i = 00000; $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 $Rif ($G> 60 和 $Gif ($B>60 and $B$CurrentDarkColor=imagecolorallocate($image,$R,$G ,$B);

//畫扇形弧
imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);

//list) ArcX, $ArcY) = pie_point($StartDegrees , $this->a , $this->b);
imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);

//畫直線
list($ArcX, $ArcY) = pie_point($EndDegrees,$this->a , $this->b);
imageline($image,$PieCenterX ,$PieCenterY,ceil($PieCenterX + $ArcX),ceil($PieCenterY + $ArcY),$CurrentColor);

//填充扇形
$MidPoint = round((($EndDegrees - $StartDegrees)/2) + + $StartDegrees);
list($ArcX, $ArcY) = Pie_point($MidPoint, $this->a*3/4 , $this->b*3/4);

imagefilltoborder($image,floor($image,floor($image,floor($ PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor,$CurrentColor);
imagestring($image,$fsize,floor($PieCenterX + $ArcX-5),floor($PieCenterY + $ArcieCenterX + $ArcX-5),floor($PieCenterY + $ArcieCenterX 5),$percent."%",$colorBorder);

//畫陰影
if ($StartDegrees>=0 and $StartDegreesif($EndDegreesfor($ k = 1; $k imagedestroy($image);
}//結束drawPie()
}//結束類別
$pie = new Pie3d;
$pie>Pie3d($pa =Pie3d($pa = 300,$pb=150,$sData="100,200,300,400,500", $sColor="ee00ff,dd0000,cccccc,ccff00,0aa",$fontsize=5);
$pie->DrawPiedltaa",$fontsize=5);
$pie->DrawPie(); src="consture.png" border=0 alt="交易分析圖">';

?>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn