首页 > 开发 > PHP > 正文

一个3D的饼图类

2024-05-04 22:58:51
字体:
来源:转载
供稿:网友

<?
/*-------------------------------------------------------------------------*/
//
//   module name: 一个3d的饼图类
//
//   author:avenger([email protected]) 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饼图类
class pie3d{
 var $a; //椭圆长半轴
 var $b; //椭圆短半轴
 var $dataarray; //每个扇形的数据
 var $colorarray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
 var $fize;    //字体大小
 var $fontcolor; //字体的颜色
 //为边缘及阴影为黑色

 function pie3d($pa=60,$pb=30,$sdata="100,200,300,400,500", $scolor="ee00ff,dd0000,cccccc,ccff00,00ccff",$fontsize=1,$fontcolor="000000") {
  $this->a=$pa;
  $this->b=$pb;
  $this->dataarray=split(",",$sdata);
  $this->colorarray=split(",",$scolor);
  $this->fsize=$fontsize;
  $this->fontcolor=$fontcolor;
  $this->bodercolor=$bordercolor;
 }
 
 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(hexdec($this->bodercolor));
     $colorborder=imagecolorallocate($image,$r,$g,$b);
     list($r,$g,$b)=getrgb(hexdec($this->fontcolor));
     $strcolor =imagecolorallocate($image,$r,$g,$b);
     $datanumber=count($this->dataarray);
     //$datatotal
     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, 2);
         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);
         //画扇形弧
         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."%",$strcolor);
         //画阴影
      
         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,'http://www.pushad.com/info/consture.png');
     imagedestroy($image);
 }//end drawpie()
}//end class


$pie = new pie3d;
$pie->pie3d($pa=80,$pb=70,$sdata="100,200,300,400,500,600,700", $scolor="ee00ff,dd0000,cccccc,ccff00,ddddaa,666666,000000",$fontsize=3,$fontcloor="ff0000");
$pie->drawpie();
echo '<img src="http://www.pushad.com/info/consture.png" border=0 alt="交易分析图">';
?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表