radius = $length; // 設定圓半徑 $this->num = db_num_rows($res); for($i=0;$i<$this->num;$i++) { $row = db_fetch_array($res); $this->sellData[$i] = $row['p_sell']; // 產品銷售量 $this->productID[$i] = $row['p_id']; // 產品序號 $this->DataTotal += $row['p_sell']; // 總銷售量 } } function DrawPie() // 畫圓餅圖 { $im = ImageCreate($this->radius*2+40,$this->radius*2+40); $PieCenterX=$this->radius+10; // 圓心 X座標 $PieCenterY=$this->radius+10; // 圓心 Y座標 $radius=$this->radius*2; // 圓半徑 // 定義所使欲使用的顏色 BEGIN $colorBorder = ImageColorAllocate($im, 0, 0, 0); // 黑 $colorback = ImageColorAllocate($im, 255, 255, 255); // 白 $colors[0] = ImageColorAllocate($im, 255, 128, 128); // 粉紅 $colors[1] = ImageColorAllocate($im, 100, 149, 237); // 粉藍 $colors[2] = ImageColorAllocate($im, 50, 205, 50); // 亮綠 $colors[3] = ImageColorAllocate($im, 244, 20, 190); // 紫 $colors[4] = ImageColorAllocate($im, 255, 215, 0); // 米黃 $colors[5] = ImageColorAllocate($im, 144, 238, 144); // 淺綠 $colors[6] = ImageColorAllocate($im, 70, 130, 180); // 海軍藍 $colors[7] = ImageColorAllocate($im, 244, 164, 96); // 棕 $colors[8] = ImageColorAllocate($im, 139, 121, 94); // 土 $colors[9] = ImageColorAllocate($im, 190, 190, 190); // 灰 // 定義所使欲使用的顏色 END ImageFill($im, 0, 0, $colorback); // 填充背景 // 開始畫每一個扇形 for($i=0;$i<$this->num;$i++) { // 四捨五入畫弧的起始角度 $StartDegrees = round($Degrees); // 累積角度 $Degrees += (($this->sellData[$i] / $this->DataTotal)*360); // 四捨五入畫弧的結束角度 $EndDegrees = round($Degrees); // 算出每一產品所佔的百分比 $percent = number_format(($this->sellData[$i] / $this->DataTotal)*100, 1); // 分配顏色 BEGIN $tmp_color = $i%10; // 取10的餘數 $CurrentColor = $colors[$tmp_color]; // 指定顏色 // 分配顏色 END // 畫扇形的弧 ImageArc($im, $PieCenterX, $PieCenterY, $radius, $radius, $StartDegrees, $EndDegrees, $CurrentColor); // 畫扇形的一邊直線 BEGIN list($ArcX, $ArcY) = Center2Pie($StartDegrees, $this->radius, $this->radius); ImageLine($im, $PieCenterX, $PieCenterY, floor($PieCenterX + $ArcX), floor($PieCenterY + $ArcY), $CurrentColor); // 畫扇形的一邊直線 END // 畫畫扇形的另一邊直線 BEGIN list($ArcX, $ArcY) = Center2Pie($EndDegrees, $this->radius, $this->radius); ImageLine($im, $PieCenterX, $PieCenterY, ceil($PieCenterX + $ArcX), ceil($PieCenterY + $ArcY), $CurrentColor); // 畫畫扇形的另一邊直線 END // 填充扇形 BEGIN $MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees); list($ArcX, $ArcY) = Center2Pie($MidPoint, $this->radius*3/4, $this->radius*3/4); ImageFilltoBorder($im, floor($PieCenterX + $ArcX), floor($PieCenterY + $ArcY), $CurrentColor, $CurrentColor); // 填充扇形 END // 寫上字串 ImageString($im, 2, floor($PieCenterX + $ArcX-20), floor($PieCenterY + $ArcY-2),$this->productID[$i] . " - " . $this->sellData[$i], $colorBorder); ImageString($im, 2, floor($PieCenterX + $ArcX-10), floor($PieCenterY + $ArcY-20), $percent . "%" , $colorback); // 寫上字串 END } Imagepng($im); // 做出png檔 ImageDestroy($im); // 移除圖形所佔的記憶體 } } // 取出類別為電腦書籍的所有產品 BEGIN $SQLStr = "SELECT * FROM product WHERE c_id='1'"; $res = db_query($SQLStr); // 取出類別為電腦書籍的所有產品 END if (db_num_rows($res)>0) { header("Content-type: image/png"); $pie = new pikelet($res); // 宣告 pie 為一個 pikelet 物件 $pie->DrawPie(); } ?>