各位大神 有沒有php 環形圖產生程式, 就像這樣子的
回複討論(解決方案)
你是要產生圖片還是要在網頁上顯示
如果只是為了在網頁上顯示,可以使用 jqPlot、Highcharts 這類 js 圖表外掛程式來做
如果是要產生圖片,可以考慮用 jpGraph。不知是否有支援你需要的樣式
自己寫的話也不難,就是一段段環缺計算量比較大
php運行在服務端只是提供資料,表徵圖顯示需要用到js,如上斑竹所說的庫.
你說的是PHP程式畫圓吧!我也做過跟你的差不多!給你個參考.....程式僅供參考!
//填充圖表的參數
$ChartDiameter = 140; //圖表直徑
$ChartData = array(20,80);//用於組建圖表的資料,可通過資料庫來取得來確定也可以多個不過和顏色數組對應
$msg = '56%';
//把角度轉換為弧度
function radians($degrees){
return($degrees*(pi()/180.0));
}
//取得在圓心為(0,0)圓上 x,y點的值
function circle_point($degrees,$diameter){
$x=cos(radians($degrees))*($diameter/2);
$y=sin(radians($degrees))*($diameter/2);
return array($x,$y);
}
//確定圖形的大小
$ChartWidth = $ChartDiameter + 22;
$ChartHeight = $ChartDiameter + 56;
//確定統計的總數
$ChartTotal = '';
for($index = 0;$index < count($ChartData);$index++){
$ChartTotal += $ChartData[$index];
}
$ChartCenterX = $ChartDiameter/2 + 11;
$ChartCenterY = $ChartDiameter/2 + 28;
//產生空白圖形
$image = imagecreatetruecolor($ChartWidth, $ChartHeight);
//分配顏色
$colorBody=imagecolorallocate($image,255,255,255);//白色
$colorBorder=imagecolorallocate($image,0,0,0);
$colorText=imagecolorallocate($image, 0, 0, 0);
$colorSlice[] = imagecolorallocate($image, 255,255,255);//這裡是和你上面寫的數組對應的顏色
$colorSlice[] = imagecolorallocate($image, 241,120,129);//資料完成顯示的背景色
//$colorSlice[] = imagecolorallocate($image,0,0,0);//資料完成顯示的背景色
//填充背境
imagefill($image, 0, 0, $colorBody);
//畫每一個扇形
$Degrees = 0;
for($index = 0; $index < count($ChartData); $index++){
$startDegrees = round($Degrees);
$Degrees += (($ChartData[$index]/$ChartTotal)*360);
$EndDegrees = round($Degrees);
$CurrentColor = $colorSlice[$index%(count($colorSlice))];
//畫圖F
imagearc
($image,$ChartCenterX,$ChartCenterY,$ChartDiameter,$ChartDiameter,$startDegrees,$EndDegrees,
$CurrentColor);
//畫直線
list($ArcX, $ArcY) = circle_point($startDegrees, $ChartDiameter);
imageline($image,$ChartCenterX,$ChartCenterY,floor($ChartCenterX + $ArcX),
floor($ChartCenterY + $ArcY),$CurrentColor);
//畫直線
list($ArcX, $ArcY) = circle_point($EndDegrees, $ChartDiameter);
imageline($image,$ChartCenterX,$ChartCenterY,ceil($ChartCenterX + $ArcX),
ceil($ChartCenterY + $ArcY),$CurrentColor);
//填充扇形
$tempnum = $EndDegrees - $startDegrees;
$MidPoint = round(($tempnum/2)+$startDegrees);
list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
imagefilltoborder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),
$CurrentColor,$CurrentColor);
}
$red = imagecolorallocate($image,255,0,0);//分配紅色
$white = imagecolorallocate($image,255,255,255);//分配白色
imagefilledellipse($image,81,98,120,120,$red);
$font = 'simhei.ttf';
imagettftext($image, 20,0, 63, 108, $white, $font,$msg);
//到此產生了一幅映像,把它發到瀏覽器上,重要是要將標題發給瀏覽器,讓它知道是一個GIF檔案。
header("Content-type: image/png");
imagegif($image);
imagedestroy($image);
?>
補充一下有個simhei.ttf是字型檔需要下載,可以問我要!
還有就是你說的這個圖,控制畫圖的半徑,控制每個扇形的起始點,這就需要你去想想演算法了!
問題已解決, 謝謝各位.