一個畫餅圖的畫直方圖直接用img控制其寬高就可實現

發表于:2007-07-14來源:作者:點擊數: 標簽:
//調用方法:image src=chart. php //chart.php為本文文件名 ? /* 把角度轉換為弧度 */ function radians ($degrees) { return($degrees * (pi()/180.0)); } /* ** 取得在圓心為(0,0)圓上 x,y點的值 */ function circle_point($degrees, $diameter) { $x
//調用方法:<image src="chart.php">
//chart.php為本文文件名

<?

/*
把角度轉換為弧度
*/
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);//圖表字體的大小

   require('mysql.php3');
   $sql=new mysql_class;
   $sql->create('star');
   $sql->query("select * from serch where answer='富士通常昊 九段'");
   $record1=$sql->rows;
   $sql->query("select * from serch where answer='富士通曹薰鉉 九段'");
   $record2=$sql->rows;          

$ChartData = array( $record1,$record2);//用于生成圖表的數據,可通過數據庫來取得來確定
//$ChartLabel = array("常昊九段", "曹薰鉉九段"); //數據對應的名稱

//確定圖形的大小
$ChartWidth = $ChartDiameter + 20;
$ChartHeight = $ChartDiameter + 20 +
(($ChartFontHeight + 2) * count($ChartData));

//確定統計的總數
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, 0xe4, 0xe6, 0xd1);
$colorBorder = imagecolorallocate($image, 0x00, 0x00, 0x00);
$colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);

$colorSlice[] = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$colorSlice[] = imagecolorallocate($image, 0x00, 0x00, 0xFF);


//填充背境
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);

//填充扇形
$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);

//畫標簽
imagestring($image,
$ChartFont,
20 + $ChartFontHeight,
$LineY,
"$ChartData[$index]",
$colorText);
}

//到此腳本 已經生了一幅圖像的,現在需要的是把它發到瀏覽器上,重要的一點是要將標頭發給瀏覽器,讓它知道是一個GIF文件。不然的話你只能看到一堆奇怪的亂碼

header("Content-type: image/gif");
//輸出生成的圖片
imagegif($image);
?>

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97