PHP中用GD绘制饼图
博客搬家了,欢迎大家关注,https://bobjin.com
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();
效果图如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEiCAIAAABY3xPlAAAR5ElEQVR4nO3dPbLkRBaGYRxWMe6sgJW0w2yAsdqcfRBtwUpYAhEYs4aJsTDHwcPKMURchFJKpZQ/3zkn3zdOdIiq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tAVbggSo65B6sLcAKOVBFh4KAtSVfYEz3gSraFwqsLfkaYzoOWtG+gGAlzAo0UPW0z7//3DLqH/+mmGBtyRcb0z5Q9TTA8p18yTEtA1VPmwnW641UH30s8r74YG3JFx7zbqDqaX3BmrCR6qNWAWtLvvyYpwNVT+t+hjV6I9VHrQXWlnwRMvUDVU8bcUnYayPVU8sc75ozOflqZG4Hp54mAWuhjVTlydckUxioetqge1iFX/evTItspGon+RJltlH/h+C1cfew2EjVbvLluvKo//B9p7qHBVgmkq/edUb9Rx0k+2DVbFN4FWBVJV/MsUf9xxuqEZeEh4P8qfy4fiPVyk3AtgDrTfIV7n3Uf4CRG3HTPVXQ83ojVcCalHzZexz1H1r8xl0SHo47bqRa/4YswOqW3AKbo/5jWa4RYNkJsPonN8LCqP8Q1g2wqCm5HQhFYQKs2cllQSjyG2BZSQ4QMJH9AMt02ES0D7ACBj0UNcAiIjcBFhG5CbCIyE2ARURuAiwichNgEZGbAIuI3ARYROQmwCIiNwEWEbkJsIjITYBFRG4CLCJyE2ARkZsAi4jcBFhE5CbAIiI3ARYRuen/VW13VGEgKBEAAAAASUVORK5CYII=" alt="" />
博客搬家了,欢迎大家关注,https://bobjin.com
PHP中用GD绘制饼图的更多相关文章
- php中用GD绘制折线图
php中用GD绘制折线图,代码如下: Class Chart{ private $image; // 定义图像 private $title; // 定义标题 private $ydata; // 定 ...
- Html5绘制饼图统计图
这里要介绍的是一个jQuery插件:jquery.easysector.js Html5提供了强大的绘图API,让我们能够使用javascript轻松绘制各种图形.本文将主要讲解使用HTML5绘制饼图 ...
- 【Highcharts】 绘制饼图和漏斗图
1.outModel类设计 设计outModel类首先研究下Highcharts中series的data数据格式,发现饼图和漏斗图都可以使用这样格式的数据 series: [{ name: 'Uniq ...
- 【带着canvas去流浪】 (3)绘制饼图
目录 一. 任务说明 二. 重点提示 三. 示例代码 四. hover高亮的实现思路 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:< ...
- Python使用Plotly绘图工具,绘制饼图
今天我们来学习一下如何使用Python的Plotly绘图工具,绘制饼图 使用Plotly绘制饼图的方法,我们需要使用graph_objs中的Pie函数 函数中最常用的两个属性values,用于赋值给需 ...
- Qt之自绘制饼图
1.说明 最近在搞绘图方面的工作,说实话C++的第三方绘图库并不算多,总之我了解的有:qtcharts.ChartDirector.qwt.kdchart和QCustomPlot.这几个库各有利弊. ...
- QT绘制饼图
QT版本:QT5.6.1 QT绘制饼图,出问题的代码如下 void DrawPieDialog::paintEvent(QPaintEvent *event) { float startAngle=0 ...
- HTML5绘制饼图示例(一)
原文地址:http://www.2cto.com/kf/201108/100251.html HTML5引入Canvas元素,用于图形的绘制,我们可以仅仅基于HTML和JavaScript就能绘制出原 ...
- R绘图 第八篇:绘制饼图(ggplot2)
geom_bar()函数不仅可以绘制条形图,还能绘制饼图,跟绘制条形图的区别是坐标系不同,绘制饼图使用的坐标系polar,并且设置theta="y": coord_polar(th ...
随机推荐
- 知方可补不足~SQL2008中的发布与订阅模式~续
回到目录 上一回介绍了如何在sql2008中建立一个数据库的发布者,今天来说一下如何建立一个订阅者,其实订阅者也是一个数据库,而这个数据库是和发布者的数据结构相同的库,它们之间通过SQL代理进行数据上 ...
- 知方可补不足~SQL为大数据引入分区表
回到目录 一些概念 分区表在oracle和sqlserver中都上存在的,当数据表的数据量过大时,上千万,上亿的数据,在进行数据查询时需要显得比较慢,性能很差,这时是时候引入分区表了,分区表顾名思义, ...
- 爱上MVC~为DisplayNameFor添加扩展,支持PagedList集合
回到目录 DisplayNameFor方法是MVC提供给我们的,它可以将模型的DisplayName特性的值显示到页面上,这对程序员来说很是方便,在进行实体设计时,可以指定它的显示名称,然后MVC引擎 ...
- js 的使用原则
1.平稳退化:确保页面在没有javaScript的情况下也能正常运行 2.向后兼容:确保老版本的浏览器不会因为javaScript而死掉 3.性能最优:确保脚本执行的性能最优 4.页面与js分离:最大 ...
- Eclipse中java获得mysql的查询结果集
不废话,先上代码,再上解释说明 package com.ningmeng; import java.sql.*; /** * 1:获取查询结果集 * @author biexiansheng * */ ...
- Javascript快速入门(下篇)
Javascript, cheer up. Ajax:其通过在Web页面与服务器之间建立一个额外的处理层,这个处理层就被称为Ajax引擎,它解释来自用户的请求,在后台以异步的方式处理服务器通信,其结构 ...
- 【工具】CodeSmith Generator 7.0.2激活步骤
学过三层的人应该认识CodeSmith Generator吧,今天我就跟大家一起探讨下CodeSmith Generator 7.0.2的激活,这最新版本破解的难度也是超越以往......具体看这篇日 ...
- OpenCascade Primitives BRep - Box
OpenCascade Primitives BRep - Box eryar@163.com Abstract. BRep is short for Boundary Representation. ...
- UNIX/Linux打包,压缩 ,解压:
1.tar打包命令 ①tar cvf tarname.tar file1 file2 $ .log $ .log $ .log $ tar cvf alllog.tar *.log a .log 0K ...
- 优秀工具推荐:超实用的 CSS 库,样板和框架
当启动一个新的项目,使用 CSS 框架或样板,可以帮助您节省大量的时间.在这篇文章中,我编译整理了我最喜欢的 CSS 样板,框架和库,帮助你在建立网站或应用程序时更加高效. 您可能感兴趣的相关文章 精 ...