利用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中jQuery插件autocomplate的简单使用笔记
Jun 14 PHP
php对数组排序的简单实例
Dec 25 PHP
在Yii框架中使用PHP模板引擎Twig的例子
Jun 13 PHP
ThinkPHP学习笔记(一)ThinkPHP部署
Jun 22 PHP
PHP中使用xmlreader读取xml数据示例
Dec 29 PHP
php递归法读取目录及文件的方法
Jan 30 PHP
php文件压缩之PHPZip类用法实例
Jun 18 PHP
Symfony2实现在doctrine中内置数据的方法
Feb 05 PHP
PHP实现简单的模板引擎功能示例
Sep 02 PHP
Laravel中获取路由参数Route Parameters的五种方法示例
Sep 29 PHP
Laravel find in set排序实例
Oct 09 PHP
laravel 解决后端无法获取到前端Post过来的值问题
Oct 22 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
解决phpmyadmin 乱码,支持gb2312和utf-8
2006/11/20 PHP
PHP删除非空目录的函数代码小结
2013/02/28 PHP
使用迭代器 遍历文件信息的详解
2013/06/08 PHP
mac系统下为 php 添加 pcntl 扩展
2016/08/28 PHP
php传值和传引用的区别点总结
2019/11/19 PHP
关于PhpStorm设置点击编辑文件自动定位源文件的实现方式
2020/12/30 PHP
JavaScript实用技巧(一)
2010/08/16 Javascript
jQuery队列控制方法详解queue()/dequeue()/clearQueue()
2010/12/02 Javascript
jQuery学习总结之元素的相对定位和选择器(持续更新)
2011/04/26 Javascript
Javascript实现的常用算法(如冒泡、快速、鸽巢、奇偶等)
2014/04/29 Javascript
跟我学习javascript的prototype,getPrototypeOf和__proto__
2015/11/17 Javascript
基于javascript实现tab切换特效
2016/03/29 Javascript
jQGrid动态填充select下拉框的选项值(动态填充)
2016/11/28 Javascript
JS中去掉array中重复元素的方法
2017/05/26 Javascript
vue中添加mp3音频文件的方法
2018/03/02 Javascript
一个简单的node.js界面实现方法
2018/06/01 Javascript
jQuery移动端跑马灯抽奖特效升级版(抽奖概率固定)实现方法
2019/01/18 jQuery
Vue组件简易模拟实现购物车
2020/12/21 Vue.js
[02:32]DOTA2亚洲邀请赛 C9战队出场宣传片
2015/02/07 DOTA
[22:59]VGJ.S vs VG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python socket多线程通讯实例分析(聊天室)
2016/04/06 Python
使用Python进行AES加密和解密的示例代码
2018/02/02 Python
详解pyqt5 动画在QThread线程中无法运行问题
2018/05/05 Python
解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题
2018/10/17 Python
python程序封装为win32服务的方法
2021/03/07 Python
解决Python 函数声明先后顺序出现的问题
2020/09/02 Python
HTML5中判断用户是否正在浏览页面的方法
2014/05/03 HTML / CSS
美国中西部家用医疗设备商店:Med Mart(轮椅、踏板车、升降机等)
2019/04/26 全球购物
以下的初始化有什么区别
2013/12/16 面试题
大学生收银员求职信分享
2014/01/02 职场文书
七年级地理教学反思
2014/01/26 职场文书
财务部经理岗位职责
2014/02/03 职场文书
行风评议整改报告
2014/11/06 职场文书
2015年学校保卫部工作总结
2015/05/11 职场文书
党小组考察意见
2015/06/02 职场文书
Redis的字符串是如何实现的
2021/10/24 Redis