PHP 板


LINE

› 看板 PHP  RSS
小女子我不才阿>< 我想請問神人 PHP的圓餅圖怎樣寫 我已經有程式碼囉 也改過很多... 我主要是想要讓它直接從陣列裡面抓資料 不要再連結到資料庫囉!! 以下是書上COPY下來的程式碼 當然我已經有修改過一點 但是也不知道為什麼只能抓的到查詢到的那一筆資料而已 拜託各位囉>< 我只要讓它從陣列裡抓資料 而且可以畫出 多資料的圓餅圖... 陣列內容大概就是 桃園縣 10 台北縣 15 台北市 5 //*********************以下是程式碼************************* <?php 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
1F:推 yukang :如果沒有機秘資料的話,可以丟到 google chart api 10/14 11:21
2F:→ claire0810 :現場沒有網路 可能沒辦法這樣匯耶... 10/14 11:47
3F:→ claire0810 :而且陣列資料會改成變數因為每次的數據可能會不同 10/14 11:49
4F:→ tka :再推 http://teethgrinder.co.uk/open-flash-chart/ 10/14 12:54
5F:→ tka :自己用GD畫實在太累了 我是懶人 :p 10/14 12:54







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:iOS 或 站內搜尋

TOP