直播中
/*
把角度轉(zhuǎn)換為弧度
*/
function radians ($degrees)
{
return($degrees * (pi()/180.0));
}
/*
** 取得在圓心為(0,0)圓上 x,y點(diǎn)的值
*/
function circle_point($degrees, $diameter)
{
$x = cos(radians($degrees)) * ($diameter/2);
$y = sin(radians($degrees)) * ($diameter/2);
return (array($x, $y));
}
// 填充圖表的參數(shù)
$ChartDiameter = 200; //圖表直徑
$ChartFont = 2; //圖表字體
$ChartFontHeight = imagefontheight($ChartFont);//圖表字體的大小
$ChartData = array( "75","45");//用于生成圖表的數(shù)據(jù),可通過(guò)數(shù)據(jù)庫(kù)來(lái)取得來(lái)確定
//$ChartLabel = array("yes", "no"); //數(shù)據(jù)對(duì)應(yīng)的名稱
//確定圖形的大小
$ChartWidth = $ChartDiameter + 20;
$ChartHeight = $ChartDiameter + 20 +
(($ChartFontHeight + 2) * count($ChartData));
//確定統(tǒng)計(jì)的總數(shù)
for($index = 0; $index < 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);
/*
** 畫每一個(gè)扇形
*/
$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);
//填充扇形
$MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
imagefilltoborder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),
$CurrentColor,$CurrentColor);
}
//畫邊框
imagearc($image,
$ChartCenterX,
$ChartCenterY,
$ChartDiameter,
$ChartDiameter,
0,
180,
$colorBorder);
imagearc($image,
$ChartCenterX,
$ChartCenterY,
$ChartDiameter,
$ChartDiameter,
180,
360,
$colorBorder);
imagearc($image,
$ChartCenterX,
$ChartCenterY,
$ChartDiameter+7,
$ChartDiameter+7,
0,
180,
$colorBorder);
imagearc($image,
$ChartCenterX,
$ChartCenterY,
$ChartDiameter+7,
$ChartDiameter+7,
180,
360,
$colorBorder);
imagefilltoborder($image,
floor($ChartCenterX + ($ChartDiameter/2) + 2),
$ChartCenterY,
$colorBorder,
$colorBorder);
//畫圖例
for($index = 0; $index < count($ChartData); $index++)
{
$CurrentColor = $colorSlice[$index%(count($colorSlice))];
$LineY = $ChartDiameter + 20 + ($index*($ChartFontHeight+2));
//draw color box
imagerectangle($image,
10,
$LineY,
10 + $ChartFontHeight,
$LineY+$ChartFontHeight,
$colorBorder);
imagefilltoborder($image,
12,
$LineY + 2,
$colorBorder,
$CurrentColor);
//畫標(biāo)簽
imagestring($image,
$ChartFont,
20 + $ChartFontHeight,
$LineY,
"$ChartLabel[$index]: $ChartData[$index]",
$colorText);
}
//到此腳本 已經(jīng)生了一幅圖像的,現(xiàn)在需要的是把它發(fā)到瀏覽器上,重要的一點(diǎn)是要將標(biāo)頭發(fā)給瀏覽器,讓它知道是一個(gè)GIF文件。不然的話你只能看到一堆奇怪的亂碼
header("Content-type: image/gif");
//輸出生成的圖片
imagegif($image);
?>
保存為chart.php,運(yùn)行程序其結(jié)果如圖1.
但這是在服務(wù)器端生在GIF圖片,我們要在HTML文件中應(yīng)用就需要如下格式來(lái)調(diào)用它:
<?php
echo "<img src='chart.php' > "
?>
注:運(yùn)行環(huán)境為apache_1_3_12+php-4.0RC1+win98,windows平臺(tái)下. 在PHP中圖像函數(shù)都是在GD庫(kù)中完成,GD庫(kù)實(shí)際是處理GIF格式的免費(fèi)軟件。要加載GD擴(kuò)展才能使用php4的GD庫(kù)可以到www.phpuser.com下載。解壓COPY php_gd.dll文件到PHP的執(zhí)行目錄,然后編輯php.ini配置文件,找到配置文件中;extension=php_gd.dll"這行 去掉";"號(hào),如果沒(méi)有發(fā)現(xiàn)則在配置文件的'Dynamic Extensions' 后增加一行extension=php_gd.dl。最后運(yùn)行phpinfo()函數(shù),你就可以看到支持信息