php 生成饼图 三维饼图


Posted in PHP onSeptember 28, 2009

饼图

<?php 
//+------------------------+ 
//| pie3dfun.PHP//公用函数 | 
//+------------------------+ 
define("ANGLE_STEP", 3); //定义画椭圆弧时的角度步长 
define("FONT_USED", "C:\WINDOWS\Fonts\simhei.ttf"); // 使用到的字体文件位置 
function draw_getdarkcolor($img,$clr) //求$clr对应的暗色 
{ 
$rgb = imagecolorsforindex($img,$clr); 
return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2); 
} 
function draw_getexy($a, $b, $d) //求角度$d对应的椭圆上的点坐标 
{ 
$d = deg2rad($d); 
return array(round($a*Cos($d)), round($b*Sin($d))); 
} 
function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr) //椭圆弧函数 
{ 
$n = ceil(($ed-$sd)/ANGLE_STEP); 
$d = $sd; 
list($x0,$y0) = draw_getexy($a,$b,$d); 
for($i=0; $i<$n; $i++) 
{ 
$d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP); 
list($x, $y) = draw_getexy($a, $b, $d); 
imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr); 
$x0 = $x; 
$y0 = $y; 
} 
} 
function draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) //画扇面 
{ 
$n = ceil(($ed-$sd)/ANGLE_STEP); 
$d = $sd; 
list($x0,$y0) = draw_getexy($a, $b, $d); 
imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr); 
for($i=0; $i<$n; $i++) 
{ 
$d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP); 
list($x, $y) = draw_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) = draw_getexy($a/2, $b/2, ($d+$sd)/2); 
imagefill($img, $x+$ox, $y+$oy, $clr); 
} 
function draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) //3d扇面 
{ 
draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr); 
if($sd<180) 
{ 
list($R, $G, $B) = draw_getdarkcolor($img, $clr); 
$clr=imagecolorallocate($img, $R, $G, $B); 
if($ed>180) $ed = 180; 
list($sx, $sy) = draw_getexy($a,$b,$sd); 
$sx += $ox; 
$sy += $oy; 
list($ex, $ey) = draw_getexy($a, $b, $ed); 
$ex += $ox; 
$ey += $oy; 
imageline($img, $sx, $sy, $sx, $sy+$v, $clr); 
imageline($img, $ex, $ey, $ex, $ey+$v, $clr); 
draw_arc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr); 
list($sx, $sy) = draw_getexy($a, $b, ($sd+$ed)/2); 
$sy += $oy+$v/2; 
$sx += $ox; 
imagefill($img, $sx, $sy, $clr); 
} 
} 
function draw_getindexcolor($img, $clr) //RBG转索引色 
{ 
$R = ($clr>>16) & 0xff; 
$G = ($clr>>8)& 0xff; 
$B = ($clr) & 0xff; 
return imagecolorallocate($img, $R, $G, $B); 
} 
// 绘图主函数,并输出图片 
// $datLst 为数据数组, $datLst 为标签数组, $datLst 为颜色数组 
// 以上三个数组的维数应该相等 
function draw_img($datLst,$labLst,$clrLst,$a=200,$b=90,$v=20,$font=10) 
{ 
$ox = 5+$a; 
$oy = 5+$b; 
$fw = imagefontwidth($font); 
$fh = imagefontheight($font); 
$n = count($datLst);//数据项个数 
$w = 10+$a*2; 
$h = 10+$b*2+$v+($fh+2)*$n; 
$img = imagecreate($w, $h); 
//转RGB为索引色 
for($i=0; $i<$n; $i++) 
$clrLst[$i] = draw_getindexcolor($img,$clrLst[$i]); 
$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 += $datLst[$i]; 
$sd = 0; 
$ed = 0; 
$ly = 10+$b*2+$v; 
for($i=0; $i<$n; $i++) 
{ 
$sd = $ed; 
$ed += $datLst[$i]/$tot*360; 
//画圆饼 
draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clrLst[$i]); //$sd,$ed,$clrLst[$i]); 
//画标签 
imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrLst[$i]); 
imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt); 
//imagestring($img, $font, 5+2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)", $clrt); 
$str = iconv("GB2312", "UTF-8", $labLst[$i]); 
ImageTTFText($img, $font, 0, 5+2*$fw, $ly+13, $clrt, FONT_USED, $str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)"); 
$ly += $fh+2; 
} 
//输出图形 
header("Content-type: image/png"); 
//输出生成的图片 
imagepng($img); 
} 
$datLst = array(30, 20, 20, 20, 10, 20, 10, 20); //数据 
$labLst = array("浙江省", "广东省", "上海市", "北京市", "福建省", "江苏省", "湖北省", "安徽省"); //标签 
$clrLst = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); 
//画图 
draw_img($datLst,$labLst,$clrLst); 
?>

php 生成饼图 三维饼图
PHP 相关文章推荐
解析php中的fopen()函数用打开文件模式说明
Jun 20 PHP
解析func_num_args与func_get_args函数的使用
Jun 24 PHP
PHP动态生成javascript文件的2个例子
Apr 11 PHP
ThinkPHP查询中的魔术方法简述
Jun 25 PHP
PHP连接操作access数据库实例
Mar 30 PHP
网页的分页下标生成代码(PHP后端方法)
Feb 03 PHP
PHP简单实现文本计数器的方法
Apr 28 PHP
PHP中单例模式与工厂模式详解
Feb 17 PHP
详谈PHP中public,private,protected,abstract等关键字的用法
Dec 31 PHP
详解PHP发送邮件知识点
May 06 PHP
php5.5使用PHPMailer-5.2发送邮件的完整步骤
Oct 14 PHP
Yii 框架使用Forms操作详解
May 18 PHP
php 不同编码下的字符串长度区分
Sep 26 #PHP
php 应用程序安全防范技术研究
Sep 25 #PHP
从Web查询数据库之PHP与MySQL篇
Sep 25 #PHP
php 服务器调试 Zend Debugger 的安装教程
Sep 25 #PHP
php 代码优化的42条建议 推荐
Sep 25 #PHP
PHP下通过系统信号量加锁方式获取递增序列ID
Sep 25 #PHP
PHP 日常开发小技巧
Sep 23 #PHP
You might like
PHP创建/删除/复制文件夹、文件
2016/05/03 PHP
[原创]PHPCMS遭遇会员投稿审核无效的解决方法
2017/01/11 PHP
CSS中简写属性要注意TRouBLe的顺序问题(避免踩坑)
2021/03/09 HTML / CSS
限制复选框的最大可选数
2006/07/01 Javascript
使用新的消息弹出框blackbirdjs
2008/10/16 Javascript
jQuery的强大选择器小结
2009/12/27 Javascript
使用滤镜设置透明导致 IE 6/7/8/9 解析异常的解决方法
2011/04/07 Javascript
JQuery实现简单时尚快捷的气泡提示插件
2012/12/20 Javascript
JS加jquery简单实现标签元素的显示或隐藏
2013/09/23 Javascript
js取得html iframe中的元素和变量值
2014/06/30 Javascript
简单谈谈javascript代码复用模式
2015/01/28 Javascript
JS+CSS实现可拖拽的漂亮圆角特效弹出层完整实例
2015/02/13 Javascript
Javascript中的call()方法介绍
2015/03/15 Javascript
JS实现的网页背景闪电闪烁效果代码
2015/10/17 Javascript
jQuery Validation Plugin验证插件手动验证
2016/01/26 Javascript
浅析JavaScript 箭头函数 generator Date JSON
2016/05/23 Javascript
如何清除IE10+ input X 文本框的叉叉和密码输入框的眼睛图标
2016/12/21 Javascript
JavaScript实现图像模糊化的方法实例
2017/01/15 Javascript
JS+HTML实现的圆形可点击区域示例【3种方法】
2018/08/01 Javascript
小程序实现图片预览裁剪插件
2019/11/22 Javascript
Nodejs文件上传、监听上传进度的代码
2020/03/27 NodeJs
记录一次websocket封装的过程
2020/11/23 Javascript
详解Django缓存处理中Vary头部的使用
2015/07/24 Python
利用Python中SocketServer 实现客户端与服务器间非阻塞通信
2016/12/15 Python
python中Pycharm 输出中文或打印中文乱码现象的解决办法
2017/06/16 Python
python通过zabbix api获取主机
2018/09/17 Python
使用python Telnet远程登录执行程序的方法
2019/01/26 Python
Python爬虫动态ip代理防止被封的方法
2019/07/07 Python
django settings.py 配置文件及介绍
2019/07/15 Python
python 调用Google翻译接口的方法
2020/12/09 Python
C#中的验证控件有几种
2014/03/08 面试题
法制宣传月活动方案
2014/05/11 职场文书
《中国梦我的梦》中学生演讲稿
2014/08/20 职场文书
毕业典礼邀请函
2015/01/31 职场文书
校园安全学习心得体会
2016/01/18 职场文书
HTML常用标签超详细整理
2022/03/19 HTML / CSS