免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 932 | 回复: 0
打印 上一主题 下一主题

一个3d饼图的类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-12 17:41 |只看该作者 |倒序浏览

//公用函数部分
//把角度转换为弧度
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饼图类
class Pie3d{
var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $DataArray; //每个扇形的数据
var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
var $Fize;    //字体大小
//为边缘及阴影为黑色
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;$iDataArray[$i]; //算出数据和
       //填充背景
       imagefill($image, 0, 0, imagecolorallocate($image, 0xDD, 0xDD, 0xDD));
   /*
   ** 画每一个扇形
   */
   $Degrees = 0;
   for ($i = 0; $i 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 $R60 and $G60 and $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($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
   //输出生成的图片
   imagepng($image,'vote.png');
   imagedestroy($image);
}//End drawPie()
}//End class
$exa=new Pie3d;
$exa->DrawPie();
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13004/showart_67351.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP