首页 > 语言 > PHP > 正文

php 3D饼图类绘制类函数

2024-09-04 11:44:24
字体:大 中 小
来源:转载
供稿:网友

下面关于php 3D饼图类绘制类函数实现原理是根据//椭圆长半轴等参数绘制一个3D饼图形的代码,代码如下:

  1. class chart{ 
  2.  
  3. var $a; //椭圆长半轴  
  4. var $b; //椭圆短半轴  
  5. var $DataArray;  //每个扇形的数据  
  6. var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x  
  7. //为边缘及阴影为黑色 
  8.  
  9. function chart($pa=100,$pb=60,$sData="100,200,300,400,500,300", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00")  
  10. {  
  11.     $this->a=$pa;  
  12.     $this->b=$pb;  
  13.     $this->DataArray=split(",",$sData);  
  14.     $this->ColorArray=split(",",$sColor);  
  15. } 
  16.  
  17. function setA($v){  
  18.     $this->a=$v;  
  19. } 
  20.  
  21. function getA(){  
  22.     return $this->a;  
  23. } 
  24.  
  25. function setB($v){  
  26.     $this->b=$v;   
  27. } 
  28.  
  29. function getB(){  
  30.     return $this->b;  
  31. } 
  32.  
  33. function setDataArray($v){  
  34.     $this->DataArray=split(",",$v);  
  35. } 
  36.  
  37. function getDataArray($v){  
  38.     return $this->DataArray;  
  39. } 
  40.  
  41. function setColorArray($v){  
  42.     $this->ColorArray=split(",",$v);  
  43. } 
  44.  
  45. function getColorArray(){  
  46.     return  $this->ColorArray;  
  47. } 
  48.  
  49.    
  50. function  DrawPie(){  
  51.     $image=imagecreate($this->a*2+40,$this->b*2+40);  
  52.     $PieCenterX=$this->a+10;  
  53.     $PieCenterY=$this->b+10;  
  54.     $DoubleA=$this->a*2;  
  55.     $DoubleB=$this->b*2;  
  56.     list($R,$G,$B)=getRGB(0);  
  57.     $colorBorder=imagecolorallocate($image,$R,$G,$B);  
  58.     $DataNumber=count($this->DataArray);  
  59.       
  60.     //$DataTotal  
  61.     for($i=0;$i<$DataNumber;$i++)      $DataTotal+=$this->DataArray[$i]; //算出数据和  
  62.       
  63.     //填充背境  
  64.     imagefill($image, 0, 0, imagecolorallocate($image, 0xFF, 0xFF, 0xFF)); 
  65.  
  66.     /*  
  67.     ** 画每一个扇形  
  68.     */  
  69.     $Degrees = 0;  
  70.     for($i = 0; $i < $DataNumber; $i++){  
  71.         $StartDegrees = round($Degrees);  
  72.         $Degrees += (($this->DataArray[$i]/$DataTotal)*360);  
  73.         $EndDegrees = round($Degrees);  
  74.         $percent = number_format($this->DataArray[$i]/$DataTotal*100, 1);   
  75.         list($R,$G,$B)=getRGB(hexdec($this->ColorArray[$i]));  
  76.         $CurrentColor=imagecolorallocate($image,$R,$G,$B);  
  77.         if ($R>60 and $R<256)            $R=$R-60;  
  78.         if ($G>60 and $G<256)            $G=$G-60;  
  79.         if ($B>60 and $B<256)            $B=$B-60;  
  80.         $CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);  
  81.         //画扇形弧  
  82.         imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);  
  83.         //画直线  
  84.         list($ArcX, $ArcY) = pie_point($StartDegrees , $this->a , $this->b);  
  85.         imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);  
  86.         //画直线  
  87.         list($ArcX, $ArcY) = pie_point($EndDegrees,$this->a , $this->b);  
  88.         imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX + $ArcX),ceil($PieCenterY + $ArcY),$CurrentColor);  
  89.         //填充扇形  
  90.         $MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);  
  91.         list($ArcX, $ArcY) = Pie_point($MidPoint, $this->a*3/4 , $this->b*3/4);  
  92.           
  93.         imagefilltoborder($image,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY), $CurrentColor,$CurrentColor);  
  94.         imagestring($image,2,floor($PieCenterX + $ArcX-5),floor($PieCenterY + $ArcY-5),$percent."%",$colorBorder); 
  95.  
  96.         //画阴影  
  97.         if ($StartDegrees>=0 and $StartDegrees<=180){  
  98.            if($EndDegrees<=180){      
  99.                for($k = 1; $k < 15; $k++)  
  100.                 imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);  
  101.            }else{  
  102.                for($k = 1; $k < 15; $k++)  
  103.                 imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);  
  104.            } 
  105.  
  106.         }  
  107.    }  
  108.           
  109.     /*到此脚本已经生了一幅图像了  
  110.     **现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个GIF文件。不然的话你只能看到一堆奇怪的乱码  
  111.     */   
  112.     //输出生成的图片      
  113.     header("Content-type: image/gif");  
  114.     imagegif($image);  
  115.     imagedestroy($image);  
  116. }//End drawPie()  
  117. }//End class 
  118. //开源代码Vevb.com 
  119. //实现 
  120. $objp = new chart();  
  121. $objp->DrawPie(); 

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