[問題] PHP的圓餅圖程式...
小女子我不才阿><
我想請問神人 PHP的圓餅圖怎樣寫
我已經有程式碼囉
也改過很多...
我主要是想要讓它直接從陣列裡面抓資料
不要再連結到資料庫囉!!
以下是書上COPY下來的程式碼 當然我已經有修改過一點
但是也不知道為什麼只抓的到查詢到的那一筆資料而已
拜託各位囉><
我只要讓它從陣列裡抓資料 而且可以畫出 多資料的圓餅圖...
*********************************以下是程式碼**********************************
include("db_func.php");
$link = mysql_connect("127.0.0.1", "root", "密碼");//資料庫連結
$db= mysql_select_db("live",$link);
mysql_query("SET NAMES 'big5'");
if(!$db){
echo "db die";
}
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 $sellData; // 銷售量陣列
var $place; // 產品序號
var $number; // 銷售總額
var $num; // 銷售標的個數
//************************************************
function pikelet($res, $length = 100) // 扇形建構子
{
$this->radius = $length; // 設定圓半徑
$this->num = db_num_rows($res);
for($i=0;$i<$this->num;$i++)
{
$row = db_fetch_array($res);
$this->sellData[$i] = $row['number']; // 產品銷售量
$this->productID[$i] = $row['place']; // 產品序號
$this->DataTotal += $row['number']; // 總銷售量
}
//******************************圓餅圖控制項開始**********************
}
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->sellData[$i] / $this->DataTotal)*360);
// 四捨五入畫弧的結束角度
$EndDegrees = round($Degrees);
// 算出每一產品所佔的百分比
$percent = number_format(($this->sellData[$i] / $this->DataTotal)*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->productID[$i] . " - " . $this->sellData[$i], $colorBorder);
ImageString($im, 2, floor($PieCenterX + $ArcX-10), floor($PieCenterY +
$ArcY-20), $percent . "%" , $colorback);
// 寫上字串 END
}
Imagepng($im); // 做出png檔
ImageDestroy($im); // 移除圖形所佔的記憶體
}
}
//******************************圓餅圖控制項結束**********************
$SQLStr = "SELECT * FROM living WHERE place='3'";
$res = mysql_query($SQLStr,$link);
if(!$res){
echo "db die";
}
if (db_num_rows($res)>0)
{
header("Content-type: image/png");
$pie = new pikelet($res); // 宣告 pie 為一個 pikelet 物件
$pie->DrawPie();
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 124.8.85.90
推
10/14 11:46, , 1F
10/14 11:46, 1F
→
10/14 12:07, , 2F
10/14 12:07, 2F
→
10/14 19:18, , 3F
10/14 19:18, 3F