PHP生成饼图很好的例子
时间:2007-09-18 来源:victorchang
找到一段PHP生成饼图很好的例子,稍加改动,就可以生成自己数据库想要的数据的分析图例...
饼图源码:(其中的$amount等变量是从数据库中取的值,你可以换成常量进行测试)
?php
/**
* 生成3d饼图的类
* 2007-6-11
*/
class Pie3d{
var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $DataArray; //每个扇形的数据
var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
var $Fize; //字体大小
//为边缘及阴影为黑色
/**
* 构造函数
*
* @param String $pa //椭圆长半轴
* @param String $pb //椭圆短半轴
* @param array $sData //每个扇形的数据
* @param array $sColor //每个扇形的颜色 要求按照十六进制书写但前面不加0x
* @param integer $fontsize //字体大小
*/
function Pie3d($x=60,$y=30,$sData="", $sColor="",$fontsize=1) {
$this->a=$x;
$this->b=$y;
$this->DataArray=split(",",$sData);
$this->ColorArray=split(",",$sColor);
$this->Fsize=$fontsize;
}
/**
* 计算圆弧角度
*
*/
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($this->deg2Arc($deg)) * $va;
$y= sin($this->deg2Arc($deg)) * $vb;
return (array($x, $y));
}
/**
* 画饼图主函数
*
*/
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)=$this->getRGB(0);
$colorBorder=imagecolorallocate($image,$R,$G,$B);
$DataNumber=count($this->DataArray);
for($i=0;$i$DataNumber;$i++) $DataTotal+=$this->DataArray[$i]; //算出数据和
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));//填充背景
/**
*
* 画每一个扇形
*/
$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)=$this->getRGB(hexdec($this->ColorArray[$i]));
$CurrentColor=imagecolorallocate($image,$R,$G,$B);
if ($R>60 and $R256) $R=$R-60;
if ($G>60 and $G256) $G=$G-60;
if ($B>60 and $B256) $B=$B-60;
$CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);
//画扇形弧
imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);
//画直线
list($ArcX, $ArcY) = $this->pie_point($StartDegrees , $this->a , $this->b);
imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);
//画直线
list($ArcX, $ArcY) = $this->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) = $this->Pie_point($MidPoint, $this->a*3/4 , $this->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);
//画阴影
if ($StartDegrees>=0 and $StartDegrees=180){
if($EndDegrees=180){
for($k = 1; $k 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);
}else{
for($k = 1; $k 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);
}
}
}
//输出生成的图片
imagepng($image,'consture.png');
imagedestroy($image);
}
}
$pie = new Pie3d;
$pie->Pie3d($pa=150,$pb=75,$sData="$amount_yp,$amount_raid,$amount_bios,$amount_zb,$amount_gq,$amount_dy,$amount_nc,$amount_qt,$amount_yu,$amount_dl,$amount_fbd,$amount_sms,$amount_bdff,$amount_qtxt", $sColor="000099,993333,66FF00,999999,00CC99,FF0000,CCFF00,330000,ddffaa,663399,996600,CC6699,33FF33,ccff00",$fontsize=2);
$pie->DrawPie();
echo '';
?>
style>
/style>
span class="STYLE1">strong>■/strong>/span>硬盘
span class="STYLE2">strong>■/strong>/span>RAID
span class="STYLE3">strong>■/strong>/span>BIOS
span class="STYLE4">strong>■/strong>/span>主板
span class="STYLE5">strong>■/strong>/span>光驱
span class="STYLE7">strong>■/strong>/span>电源
span class="STYLE8">strong>■/strong>/span>内存
span class="STYLE9">strong>■/strong>/span>其他硬件
span class="STYLE10">strong>■/strong>/span>域系统
span class="STYLE11">strong>■/strong>/span>代理系统
span class="STYLE12">strong>■/strong>/span>防病毒
span class="STYLE13">strong>■/strong>/span>SMS
span class="STYLE14">strong>■/strong>/span>补丁分发
span class="STYLE15">strong>■/strong>/span>其他软件
效果图:
饼图源码:(其中的$amount等变量是从数据库中取的值,你可以换成常量进行测试)
?php
/**
* 生成3d饼图的类
* 2007-6-11
*/
class Pie3d{
var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $DataArray; //每个扇形的数据
var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
var $Fize; //字体大小
//为边缘及阴影为黑色
/**
* 构造函数
*
* @param String $pa //椭圆长半轴
* @param String $pb //椭圆短半轴
* @param array $sData //每个扇形的数据
* @param array $sColor //每个扇形的颜色 要求按照十六进制书写但前面不加0x
* @param integer $fontsize //字体大小
*/
function Pie3d($x=60,$y=30,$sData="", $sColor="",$fontsize=1) {
$this->a=$x;
$this->b=$y;
$this->DataArray=split(",",$sData);
$this->ColorArray=split(",",$sColor);
$this->Fsize=$fontsize;
}
/**
* 计算圆弧角度
*
*/
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($this->deg2Arc($deg)) * $va;
$y= sin($this->deg2Arc($deg)) * $vb;
return (array($x, $y));
}
/**
* 画饼图主函数
*
*/
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)=$this->getRGB(0);
$colorBorder=imagecolorallocate($image,$R,$G,$B);
$DataNumber=count($this->DataArray);
for($i=0;$i$DataNumber;$i++) $DataTotal+=$this->DataArray[$i]; //算出数据和
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));//填充背景
/**
*
* 画每一个扇形
*/
$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)=$this->getRGB(hexdec($this->ColorArray[$i]));
$CurrentColor=imagecolorallocate($image,$R,$G,$B);
if ($R>60 and $R256) $R=$R-60;
if ($G>60 and $G256) $G=$G-60;
if ($B>60 and $B256) $B=$B-60;
$CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);
//画扇形弧
imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);
//画直线
list($ArcX, $ArcY) = $this->pie_point($StartDegrees , $this->a , $this->b);
imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);
//画直线
list($ArcX, $ArcY) = $this->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) = $this->Pie_point($MidPoint, $this->a*3/4 , $this->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);
//画阴影
if ($StartDegrees>=0 and $StartDegrees=180){
if($EndDegrees=180){
for($k = 1; $k 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);
}else{
for($k = 1; $k 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);
}
}
}
//输出生成的图片
imagepng($image,'consture.png');
imagedestroy($image);
}
}
$pie = new Pie3d;
$pie->Pie3d($pa=150,$pb=75,$sData="$amount_yp,$amount_raid,$amount_bios,$amount_zb,$amount_gq,$amount_dy,$amount_nc,$amount_qt,$amount_yu,$amount_dl,$amount_fbd,$amount_sms,$amount_bdff,$amount_qtxt", $sColor="000099,993333,66FF00,999999,00CC99,FF0000,CCFF00,330000,ddffaa,663399,996600,CC6699,33FF33,ccff00",$fontsize=2);
$pie->DrawPie();
echo '';
?>
style>
/style>
span class="STYLE1">strong>■/strong>/span>硬盘
span class="STYLE2">strong>■/strong>/span>RAID
span class="STYLE3">strong>■/strong>/span>BIOS
span class="STYLE4">strong>■/strong>/span>主板
span class="STYLE5">strong>■/strong>/span>光驱
span class="STYLE7">strong>■/strong>/span>电源
span class="STYLE8">strong>■/strong>/span>内存
span class="STYLE9">strong>■/strong>/span>其他硬件
span class="STYLE10">strong>■/strong>/span>域系统
span class="STYLE11">strong>■/strong>/span>代理系统
span class="STYLE12">strong>■/strong>/span>防病毒
span class="STYLE13">strong>■/strong>/span>SMS
span class="STYLE14">strong>■/strong>/span>补丁分发
span class="STYLE15">strong>■/strong>/span>其他软件
效果图:
相关阅读 更多 +
排行榜 更多 +