PHP中用GD繪制餅圖,繪制的類見代碼:
1 Class Chart{
2 private $image; // 定義圖像
3 private $title; // 定義標題
4 private $ydata; // 定義Y軸數據
5 private $xdata; // 定義X軸數據
6 private $color; // 定義條形圖顏色
7 private $bgcolor; // 定義圖片背景顏色
8 private $width; // 定義圖片的寬
9 private $height; // 定義圖片的長
10
11 /*
12 * 構造函數
13 * String title 圖片標題
14 * Array xdata 索引數組,X軸數據
15 * Array ydata 索引數組,數字數組,Y軸數據
16 */
17 function __construct($title,$xdata,$ydata) {
18 $this->title = $title;
19 $this->xdata = $xdata;
20 $this->ydata = $ydata;
21 $this->color = array('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4');
22 }
23
24 /*
25 * 公有方法,設置條形圖的顏色
26 * Array color 顏色數組,元素取值為'#058DC7'這種形式
27 */
28 function setBarColor($color){
29 $this->color = $color;
30 }
31
32 /*
33 * 繪制餅圖
34 */
35 function mkPieChart() {
36 $sum = array_sum($this->ydata); // 獲取ydata所有元素之和
37 $start = 0; // 弧的開始角度
38 $end = 0; // 弧的結束角度
39 $pieWidth = 300; // 橢圓的長軸
40 $pieHeight = 220; // 橢圓的短軸
41 $space = 40; // 橢圓與小矩形的間距
42 $margin = 20; // 圖片的邊距
43 $recWidth = 20; // 小矩形的寬
44 $recHeight = 15; // 小矩形的高
45 $titleHeight = 50; // 標題區域的高
46 // 圖片自適應寬與高
47 $this->width = $pieWidth + $this->arrayLengthMax($this->xdata)*10*4/3 + $space + $recWidth +$margin;
48 $this->height = (($pieHeight > count($this->xdata)*25 ) ? $pieHeight : count($this->xdata)*25) + $titleHeight;
49 // 橢圓中心的坐標
50 $cx = $pieWidth/2+$margin;
51 $cy = $pieHeight/2+$titleHeight;
52
53 $this->image = imagecreatetruecolor($this->width ,$this->height); // 准備畫布
54 $this->bgcolor = imagecolorallocate($this->image,255,255,255); // 圖片的背景顏色
55 imagefill($this->image,0,0,$this->bgcolor); // 填充背景
56
57 // 設置條形圖的顏色
58 $color = array();
59 foreach($this->color as $col) {
60 $col = substr($col,1,strlen($col)-1);
61 $red = hexdec(substr($col,0,2));
62 $green = hexdec(substr($col,2,2));
63 $blue = hexdec(substr($col,4,2));
64 $color[] = imagecolorallocate($this->image ,$red, $green, $blue);
65 }
66
67 // 設置線段的顏色、字體的顏色、字體的路徑
68 $lineColor = imagecolorallocate($this->image ,0xcc,0xcc,0xcc);
69 $fontColor = imagecolorallocate($this->image, 0x95,0x8f,0x8f);
70 $fontPath = 'font/simsun.ttc';
71
72 // 繪制扇形弧
73 for($i = 0; $i < 10; $i++) {
74 foreach($this->ydata as $key => $val) {
75 $end += 360*$val/$sum;
76 imagefilledarc($this->image,$cx,$cy-$i,$pieWidth,$pieHeight, $start,$end,$color[$key%count($this->color)],IMG_ARC_PIE);
77 $start = $end;
78 }
79 }
80
81 // 繪制小矩形及之後文字說明
82 $x1 = $pieWidth+$space;
83 $y1 = $titleHeight ;
84 foreach($this->ydata as $key => $val) {
85 imagefilledrectangle($this->image,$x1,$y1,$x1+$recWidth,$y1+$recHeight,$color[$key%count($this->color)]);
86 imagettftext($this->image,10,0,$x1+$recWidth+5,$y1+$recHeight-2,$fontColor,$fontPath,$this->xdata[$key]);
87 $y1 += $recHeight + 10;
88 }
89
90 // 繪畫標題
91 $titleStart = ($this->width - 5.5*strlen($this->title))/2;
92 imagettftext($this->image,11,0,$titleStart,20,$fontColor,$fontPath,$this->title);
93
94 // 輸出圖片
95 header("Content-Type:image/png");
96 imagepng($this->image);
97 }
98
99 /*
100 * 私有方法,求數組中元素長度最大的值
101 * Array arr 字符串數組,必須是漢字
102 */
103 private function arrayLengthMax($arr) {
104 $length = 0;
105 foreach($arr as $val) {
106 $length = strlen($val) > $length ? strlen($val) : $length;
107 }
108 return $length/3;
109 }
110
111 // 析構函數
112 function __destruct(){
113 imagedestroy($this->image);
114 }
115 }
測試代碼如下:
1 $xdata = array('測試一','測試二','測試三','測試四','測試五','測試六','測試七','測試八','測試九');
2 $ydata = array(89,90,90,23,35,45,56,23,56);
3 $Img = new Chart($title,$xdata,$ydata);
4 $Img->mkPieChart();
效果圖如下: