用PHP制作餅圖調查表
在調查程序中,我們需要根據統計的數據來 生成各種圖表來生動的表示調查的百分比 。在PHP在這方面也是不負眾望,它中可以通過加載GD庫來實現一開始。餅狀圖表對於查看一個值占總值的百分比是一個好的方法。現在我們就用PHP來實現一個餅形圖表,給大家講述PHP在這方面的應用。它的設計思想是:首先以用imagecreate()來生成一個空白圖形,然後在空白圖形中用imageare()圓弧函數先畫圓弧,再畫兩條線連接圓心和圓弧端點(PHP圖像函數不能畫扇形),再用imagefilltoborder函數來填充扇形。其程序實現如下:
href/*
把角度轉換為弧度
*/
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));
}
// 填充圖表的參數
$ChartDiameter = 200; //圖表直徑
$ChartFont = 2; //圖表字體
$ChartFontHeight = imagefontheight($ChartFont);//圖表字體的大小
$ChartData = array( 75,45);//用於生成圖表的數據,可通過數據庫來取得來確定
//$ChartLabel = array(yes, no); //數據對應的名稱
//確定圖形的大小
$ChartWidth = $ChartDiameter + 20;
$ChartHeight = $ChartDiameter + 20 +
(($ChartFontHeight + 2) * count($ChartData));
//確定統計的總數
for($index = 0; $index
href; count($ChartData); $index++)
{
$ChartTotal += $ChartData[$index];
}
$ChartCenterX = $ChartDiameter/2 + 10;
$ChartCenterY = $ChartDiameter/2 + 10;
//生成空白圖形
$image = imagecreate($ChartWidth, $ChartHeight);
//分配顏色
$colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorBorder = imagecolorallocate($image, 0x00, 0x00, 0x00);
$colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);
$colorSlice = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$colorSlice[] = imagecolorallocate($image, 0x00, 0xFF, 0x00);
//填充背境
imagefill($image, 0, 0, $colorBody);
/*
** 畫每一個扇形
*/
$Degrees = 0;
for($index = 0; $index
href; 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);
//填充扇形
$MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
imagefilltoborder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),
$CurrentColor,$CurrentColor);
}
2頁,當前在第1 /058190945058285537_2
[ 本帖最後由 hrsenshen 於 2005-12-28 09:13 AM 編輯 ]