作者claire0810 (Claire)
看板PHP
標題[請益] (使用函式畫的)圓餅圖該如何顯示中文?
時間Fri Dec 5 16:31:24 2008
之前感謝這裡的高手 幫我改成可以使用正列顯示圓餅圖
但是因為我們是要使用中文介面顯示
所以現在我想要問...該如何使用中文 來顯示 圓餅圖裡的資料...
還有一個問題是 當資料量太小 EX:1 時 圓餅圖的角度會太小
資料會被蓋到 有沒有其他方法可以改善...
以下為程式碼
/*********************************************************************
function Deg2Arc($degrees)
{
return($degrees * (pi()/180.0));
}
// 定義一把角度轉換為弳度的函式 END
// 定義一取得圓弧上之 x,y 點的函式 BEGIN
function Center2Pie($deg,$length)
{
$x= cos(Deg2Arc($deg)) * $length;
$y= sin(Deg2Arc($deg)) * $length;
return (array($x, $y));
}
// 定義一取得圓弧上之 x,y 點的函式 END
class pikelet // 宣告一個 pikelet 的類別
{
var $radius; // 圓半徑
var $DataArray; // 每個扇形所代表的資料
var $NumberData;
var $place; // 序號
var $Number; // 總數
var $num; // 各數
function pikelet($res, $length = 100) // 扇形建構子
{
$this->radius = $length; // 設定圓半徑
$this->num = count($res);
$this->NumberData = array_values($res); // 產品銷售量
$this->place = array_keys($res); // 產品序號
$this->Number = array_sum($this->NumberData); // 總銷售量
}
//******************************圓餅圖控制項開始**********************
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->NumberData[$i] / $this->Number)*360);
// 四捨五入畫弧的結束角度
$EndDegrees = round($Degrees);
// 算出每一產品所佔的百分比
$percent = number_format(($this->NumberData[$i] / $this->Number)*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->place[$i] . " - " . $this->NumberData[$i], $colorBorder);
ImageString($im, 2, floor($PieCenterX + $ArcX-10), floor($PieCenterY +
$ArcY-20), $percent . "%" , $colorback);
// 寫上字串 END
}
Imagepng($im); // 做出png檔
ImageDestroy($im); // 移除圖形所佔的記憶體
}
}
//******************************圓餅圖控制項結束**********************
$res = array("90分" => 1,"90~80分" => 5,"80~70分" => 10, "70~60分" => 10,"60以
下" => 17) ;
mysql_close($link);
header("Content-type: image/png");
$pie = new pikelet($res); // 宣告 pie 為一個 pikelet 物件
$pie->DrawPie();
/*******************************程式碼結束********************************
這就是全部的程式碼
主要問題就是前面所說的那幾項囉...
1. 在陣列中 "分" 無法顯示 會變成亂碼顯示在圓餅圖中
2. 90分的人數只有1 在圓餅圖中的弧度太小 無法全部顯示 有無方法可以讓他全部顯示
主要就是這兩個問題了>< 拜託可以幫我想想辦法嗎
先謝謝各位了 ><
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 124.8.83.161
※ 編輯: claire0810 來自: 124.8.83.161 (12/05 16:32)
※ 編輯: claire0810 來自: 124.8.83.161 (12/05 16:35)
※ 編輯: claire0810 來自: 124.8.83.161 (12/05 16:35)
→ cttlee :ImageString換掉 12/05 18:17
推 cttlee :imagettftext 12/05 18:19
改了之後程式會錯誤喔...
※ 編輯: claire0810 來自: 124.8.83.161 (12/05 18:59)
→ cttlee :引數不太一樣自己對應一下 12/05 19:14
→ crazybad :imagettftext只接受utf-8記得中文要先用iconv()去轉 12/06 21:28
→ claire0810 :那這樣我要在哪裡改編碼方式呢? 12/07 17:38
→ claire0810 :因為我現在連錯誤訊息都沒有了 12/07 17:38