php 3D饼图类绘制类函数

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

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