php绘制饼图,php怎么绘制饼图?

本文介绍如何使用PHP的GD库绘制饼图,包括创建饼图类、设置颜色、绘制饼图等步骤,并提供完整示例代码。

3f2c1e85da4397be5a8ecffa922f5ecb.png

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();

效果图如下:

f9d322bce299fabba34e62f3c1f5ddd6.png

GD库的主要用途

在网站上GD库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站数据生成报表等。在PHP处理图像,可使用GD库,而GD库开始时是支持GIF的,但由于GIF使用了有版权争议的LZW算法,会引起法律问题,于是从 GD 库 1.6 版起所有的 GIF 支持都移除了,但是又在 GD 库 2.0.28 版起又加了回来。如果使用二者之间版本的 GD 库时 GIF 相关函数不可用。

更多相关知识,请访问 PHP中文网!!

相关标签:php 饼图

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值