php绘制饼图,php怎么绘制饼图?
php怎么繪制餅圖?
在php中,可以使用GD繪制餅圖。
GD庫是php處理圖形的擴展庫,GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片,也可以給圖片加水印。
PHP中用GD繪制餅圖,繪制的類見代碼:Class Chart{
private $image; // 定義圖像
private $title; // 定義標題
private $ydata; // 定義Y軸數據
private $xdata; // 定義X軸數據
private $color; // 定義條形圖顏色
private $bgcolor; // 定義圖片背景顏色
private $width; // 定義圖片的寬
private $height; // 定義圖片的長
/*
* 構造函數
* String title 圖片標題
* Array xdata 索引數組,X軸數據
* Array ydata 索引數組,數字數組,Y軸數據
*/
function __construct($title,$xdata,$ydata) {
$this->title = $title;
$this->xdata = $xdata;
$this->ydata = $ydata;
$this->color = array('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4');
}
/*
* 公有方法,設置條形圖的顏色
* Array color 顏色數組,元素取值為'#058DC7'這種形式
*/
function setBarColor($color){
$this->color = $color;
}
/*
* 繪制餅圖
*/
function mkPieChart() {
$sum = array_sum($this->ydata); // 獲取ydata所有元素之和
$start = 0; // 弧的開始角度
$end = 0; // 弧的結束角度
$pieWidth = 300; // 橢圓的長軸
$pieHeight = 220; // 橢圓的短軸
$space = 40; // 橢圓與小矩形的間距
$margin = 20; // 圖片的邊距
$recWidth = 20; // 小矩形的寬
$recHeight = 15; // 小矩形的高
$titleHeight = 50; // 標題區域的高
// 圖片自適應寬與高
$this->width = $pieWidth + $this->arrayLengthMax($this->xdata)*10*4/3 + $space + $recWidth +$margin;
$this->height = (($pieHeight > count($this->xdata)*25 ) ? $pieHeight : count($this->xdata)*25) + $titleHeight;
// 橢圓中心的坐標
$cx = $pieWidth/2+$margin;
$cy = $pieHeight/2+$titleHeight;
$this->image = imagecreatetruecolor($this->width ,$this->height); // 準備畫布
$this->bgcolor = imagecolorallocate($this->image,255,255,255); // 圖片的背景顏色
imagefill($this->image,0,0,$this->bgcolor); // 填充背景
// 設置條形圖的顏色
$color = array();
foreach($this->color as $col) {
$col = substr($col,1,strlen($col)-1);
$red = hexdec(substr($col,0,2));
$green = hexdec(substr($col,2,2));
$blue = hexdec(substr($col,4,2));
$color[] = imagecolorallocate($this->image ,$red, $green, $blue);
}
// 設置線段的顏色、字體的顏色、字體的路徑
$lineColor = imagecolorallocate($this->image ,0xcc,0xcc,0xcc);
$fontColor = imagecolorallocate($this->image, 0x95,0x8f,0x8f);
$fontPath = 'font/simsun.ttc';
// 繪制扇形弧
for($i = 0; $i < 10; $i++) {
foreach($this->ydata as $key => $val) {
$end += 360*$val/$sum;
imagefilledarc($this->image,$cx,$cy-$i,$pieWidth,$pieHeight, $start,$end,$color[$key%count($this->color)],IMG_ARC_PIE);
$start = $end;
}
}
// 繪制小矩形及之后文字說明
$x1 = $pieWidth+$space;
$y1 = $titleHeight ;
foreach($this->ydata as $key => $val) {
imagefilledrectangle($this->image,$x1,$y1,$x1+$recWidth,$y1+$recHeight,$color[$key%count($this->color)]);
imagettftext($this->image,10,0,$x1+$recWidth+5,$y1+$recHeight-2,$fontColor,$fontPath,$this->xdata[$key]);
$y1 += $recHeight + 10;
}
// 繪畫標題
$titleStart = ($this->width - 5.5*strlen($this->title))/2;
imagettftext($this->image,11,0,$titleStart,20,$fontColor,$fontPath,$this->title);
// 輸出圖片
header("Content-Type:image/png");
imagepng($this->image);
}
/*
* 私有方法,求數組中元素長度最大的值
* Array arr 字符串數組,必須是漢字
*/
private function arrayLengthMax($arr) {
$length = 0;
foreach($arr as $val) {
$length = strlen($val) > $length ? strlen($val) : $length;
}
return $length/3;
}
// 析構函數
function __destruct(){
imagedestroy($this->image);
}
}
測試代碼如下:$xdata = array('測試一','測試二','測試三','測試四','測試五','測試六','測試七','測試八','測試九');
$ydata = array(89,90,90,23,35,45,56,23,56);
$Img = new Chart($title,$xdata,$ydata);
$Img->mkPieChart();
效果圖如下:
GD庫的主要用途
在網站上GD庫通常用來生成縮略圖,或者用來對圖片加水印,或者用來生成漢字驗證碼,或者對網站數據生成報表等。在PHP處理圖像,可使用GD庫,而GD庫開始時是支持GIF的,但由于GIF使用了有版權爭議的LZW算法,會引起法律問題,于是從 GD 庫 1.6 版起所有的 GIF 支持都移除了,但是又在 GD 庫 2.0.28 版起又加了回來。如果使用二者之間版本的 GD 庫時 GIF 相關函數不可用。
更多相關知識,請訪問 PHP中文網!!
相關標簽:php 餅圖
本文原創發布php中文網,轉載請注明出處,感謝您的尊重!
總結
以上是生活随笔為你收集整理的php绘制饼图,php怎么绘制饼图?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IP地理地址库geoip2用法
- 下一篇: 字节跳动游戏业务收缩或因张一鸣不满业绩