利用php绘制饼状图的实现代码


Posted in PHP onJune 07, 2013

drawPieImg()函数包含8个参数,$title为饼状图的标题;$dataArr为需要显示的数据数组;$labelArr为对应数据的标签分类数组;$colorArr为对应数据的绘图颜色数组,这4个参数是必须的,对于不同的系统应用传递相应的参数即可。接下来的4个参数,负责设置要生成的饼状图的大小,如果不设置则使用系统默认值。程序按照床底数组数据的大小,从0度开始绘制,方向按照顺时针方向依次绘制对应数据占据的扇面大小。

<?php
 //变量定义,画椭圆弧时的角度大小
 define("ANGLELENGTH",3);
 /**
  * 绘制图片
  * @param $title 3D图的标题
  * @param $dataArr 显示的数据数组
  * @param $labelArr 对应数据的标签分类数组
  * @param $colorArr 对应绘图颜色的数组
  * @param $a  画布的基准宽度
  * @param $b  画布的基准高度
  * @param $v  3D柱的高度
  * @param $font 字体大小
  * @return   绘制成功的图片访问路径
  */
 function drawPieImg($title, $dataArr, $labelArr, $colorArr, $a=250, $b=120, $v=20, $font=10){
  $ox = 5+$a;
  $oy = 5+$b;
  $fw = imagefontwidth($font);
  $fh = imagefontheight($font);
  $n = count($dataArr);//计算数组长度
  $w = 10+$a*2;
  $h = 10+$b*2+$v+($fh+2)*$n;
  //创建画板
  $img = imagecreate($w, $h);
  //转RGB为索引色
  for($i=0; $i<$n; $i++)
   $colorArr[$i] = drawIndexColor($img,$colorArr[$i]);//为图像$img分配颜色
  $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);
  $clrt = imagecolorallocate($img, 0x00, 0x00, 0x00);
  //填充背景色
  imagefill($img, 0, 0, $clrbk);
  //求和
  $tot = 0;
  for($i=0; $i<$n; $i++)
   $tot += $dataArr[$i];
  //每个分类的起始角度大小
  $sd = 0;
  //每个分类所占据的角度大小
  $ed = 0;
  $ly = 10+$b*2+$v;
  for($i=0; $i<$n; $i++){
   $sd = $ed;
   $ed += $dataArr[$i]/$tot*360;
   //画3d扇面
   draw3DSector($img, $ox, $oy+20, $a, $b, $v, $sd, $ed, $colorArr[$i]);
   //画标签
   imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $colorArr[$i]);
   imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt);
   //中文转码
   $str = iconv("GB2312", "UTF-8", $labelArr[$i]);
   imagettftext($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "D:/wamp/www/source/font/simhei.ttf", $str.":".$dataArr[$i]."(".(round(10000*($dataArr[$i]/$tot))/100)."%)");
   $ly += $fh+2;
  }
  //绘制图片标题
  imagettftext($img, 15, 0, 5, 15, $clrt, "D:/wamp/www/source/font/simhei.ttf", iconv("GB2312", "UTF-8",$title));
  //输出图形
  header("Content-type: image/png");
  //输出生成的图片
  $imgFileName = "./".time().".png";
  imagepng($img,$imgFileName);
  return $imgFileName;
 }
 /**
  * 绘制3d扇面
  */
 function draw3DSector($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) {
  drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);
  if($sd<180){
   list($red, $green, $blue) = drawDarkColor($img, $clr);
   //为图像分配颜色
   $clr=imagecolorallocate($img, $red, $green, $blue);
   if($ed>180)
    $ed = 180;
   list($sx, $sy) = getExy($a,$b,$sd);
   $sx += $ox;
   $sy += $oy;
   list($ex, $ey) = getExy($a, $b, $ed);
   $ex += $ox;
   $ey += $oy;
   imageline($img, $sx, $sy, $sx, $sy+$v, $clr);
   imageline($img, $ex, $ey, $ex, $ey+$v, $clr);
   drawArc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr);
   list($sx, $sy) = getExy($a, $b, ($sd+$ed)/2);
   $sy += $oy+$v/2;
   $sx += $ox;
   imagefill($img, $sx, $sy, $clr);
  }
 }
 /**
  * 绘制椭圆弧
  */
 function drawArc($img,$ox,$oy,$a,$b,$sd,$ed,$clr){
  $n = ANGLELENGTH >0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;
  $d = $sd;
  list($x0,$y0) = getExy($a,$b,$d);
  for($i=0; $i<$n; $i++){
   $d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);
   list($x, $y) = getExy($a, $b, $d);
   imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
   $x0 = $x;
   $y0 = $y;
  }
 }
 /**
  * 绘制扇面
  */
 function drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) {
  $n = ANGLELENGTH > 0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;
  $d = $sd;
  list($x0,$y0) = getExy($a, $b, $d);
  imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
  for($i=0; $i<$n; $i++) {
   $d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);
   list($x, $y) = getExy($a, $b, $d);
   imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
   $x0 = $x;
   $y0 = $y;
  }
  imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
  list($x, $y) = getExy($a/2, $b/2, ($d+$sd)/2);
  imagefill($img, $x+$ox, $y+$oy, $clr);
 }
 /**
  * 根据$clr颜色获取对应的柱的阴影色
  * @param $img  图像
  * @param $clr  颜色
  * @return rgb颜色数组
  */
 function drawDarkColor($img,$clr){
  $rgb = imagecolorsforindex($img,$clr);
  return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
 }
 /**
  * 求角度$d对应的椭圆上的点坐标
  *
  * @param $a 横坐标
  * @param $b 纵坐标
  * @param $d 角度
  * @return 对应椭圆点坐标
  */
 function getExy($a, $b, $d){
  $d = deg2rad($d);
  return array(round($a*cos($d)), round($b*sin($d)));
 }
 /**
  * 为图像分配RGB索引色
  */
 function drawIndexColor($img, $clr){
  $red = ($clr>>16) & 0xff;
  $green = ($clr>>8)& 0xff;
  $blue = ($clr) & 0xff;
  return imagecolorallocate($img, $red, $green, $blue);
 }
//测试示例
$title = "动物园动物种类分布情况";
$dataArr = array(20, 10, 20, 20, 10, 20, 30, 10); //测试数据数组
$labelArr = array("大象", "长颈鹿", "鳄鱼", "鸵鸟", "老虎", "狮子", "猴子", "斑马");//标签
$colorArr = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); //对应颜色数组
$result = drawPieImg($title, $dataArr,$labelArr,$colorArr);
echo "<img src=".$result." mce_src=".$result.">";
?>

PHP 相关文章推荐
php 将bmp图片转为jpg等其他任意格式的图片
Jun 21 PHP
认识并使用PHP超级全局变量
Jan 26 PHP
IIS7.X配置PHP运行环境小结
Jun 09 PHP
初识php MVC
Sep 10 PHP
PHP性能分析工具XHProf安装使用教程
May 13 PHP
PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID
Nov 25 PHP
php读取XML的常见方法实例总结
Apr 25 PHP
PHP魔术方法之__call与__callStatic使用方法
Jul 23 PHP
PHP7.1实现的AES与RSA加密操作示例
Jun 15 PHP
laravel validate 设置为中文的例子(验证提示为中文)
Sep 29 PHP
PHP读取文件,解决中文乱码UTF-8的方法分析
Jan 22 PHP
PHP内存溢出优化代码详解
Feb 26 PHP
PHP自定义大小验证码的方法详解
Jun 07 #PHP
如何用php生成扭曲及旋转的验证码图片
Jun 07 #PHP
利用php获取服务器时间的实现代码
Jun 07 #PHP
探讨PHP中OO之静态关键字以及类常量的详解
Jun 07 #PHP
PHP5常用函数列表(分享)
Jun 07 #PHP
深入理解php的MySQL连接类
Jun 07 #PHP
PHP之生成GIF动画的实现方法
Jun 07 #PHP
You might like
php 应用程序安全防范技术研究
2009/09/25 PHP
php实现以只读方式打开文件的方法
2015/03/16 PHP
php 输出json及显示json中的中文汉字详解及实例
2016/11/09 PHP
php写app用的框架整理
2019/09/29 PHP
PHP array_reverse() 函数原理及实例解析
2020/07/14 PHP
javascript IFrame 强制刷新代码
2009/07/23 Javascript
js每隔5分钟执行一次ajax请求的实现方法
2013/11/27 Javascript
解决JQeury显示内容没有边距内容紧挨着浏览器边线
2013/12/20 Javascript
JavaScript面向对象编程入门教程
2014/04/16 Javascript
javascript属性访问表达式用法分析
2015/04/25 Javascript
简单的jQuery入门指引
2015/07/28 Javascript
百度地图JavascriptApi Marker平滑移动及车头指向行径方向
2017/03/13 Javascript
angularjs中回车键触发某一事件的方法
2017/04/24 Javascript
node.js中实现kindEditor图片上传功能的方法教程
2017/04/26 Javascript
nodeJs爬虫的技术点总结
2018/05/13 NodeJs
Vue项目中配置pug解析支持
2019/05/10 Javascript
Python使用CMD模块更优雅的运行脚本
2015/05/11 Python
深入浅析Python中join 和 split详解(推荐)
2016/06/30 Python
PyCharm设置SSH远程调试的方法
2018/07/17 Python
python/sympy求解矩阵方程的方法
2018/11/08 Python
django admin.py 外键,反向查询的实例
2019/07/26 Python
Python爬虫 批量爬取下载抖音视频代码实例
2019/08/16 Python
python matplotlib中的subplot函数使用详解
2020/01/19 Python
python pymysql库的常用操作
2020/10/16 Python
Python下使用Trackbar实现绘图板
2020/10/27 Python
python反扒机制的5种解决方法
2021/02/06 Python
html5 button autofocus 属性介绍及应用
2013/01/04 HTML / CSS
StubHub新加坡:购买和出售全球活动门票
2017/03/10 全球购物
个人社会实践自我鉴定
2014/03/24 职场文书
《池塘边的叫声》教学反思
2014/04/12 职场文书
数据保密承诺书
2014/06/03 职场文书
房屋买卖委托书格式范本格式
2014/10/13 职场文书
违反工作规定检讨书范文
2014/12/14 职场文书
傲慢与偏见电影观后感
2015/06/10 职场文书
采购部2015年度工作总结
2015/07/24 职场文书
win10如何快速切换窗口 win10切换窗口快捷键分享
2022/07/23 数码科技