class chart{ var $a; //橢圓長半軸 var $b; //橢圓短半軸 var $DataArray; //每個扇形的資料 var $ColorArray; //每個扇形的顏色 要求按照十六進位書寫但前面不加0x //為邊緣及陰影為黑色 function chart($pa=100,$pb=60,$sData="100,200,300,400,500,300", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00") { $this->a=$pa; $this->b=$pb; $this->DataArray=split(",",$sData); $this->ColorArray=split(",",$sColor); } 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(){ $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;$i<$DataNumber;$i++) $DataTotal+=$this->DataArray[$i]; //算出資料和 //填充背境 imagefill($image, 0, 0, imagecolorallocate($image, 0xFF, 0xFF, 0xFF)); /* ** 畫每一個扇形 */ $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)=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,2,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); } } } /*到此指令碼已經生了一幅映像了 **現在需要的是把它發到瀏覽器上,重要的一點是要將標題發給瀏覽器,讓它知道是一個GIF檔案。不然的話你只能看到一堆奇怪的亂碼 */ //輸出產生的圖片 header("Content-type: image/gif"); imagegif($image); imagedestroy($image); }//End drawPie() }//End class //實現
$objp = new chart(); $objp->DrawPie(); |