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仿discuz分页效果代码
Oct 02 PHP
PHP json格式和js json格式 js跨域调用实现代码
Sep 08 PHP
有关PHP性能优化的介绍
Jun 20 PHP
JoshChen_web格式编码UTF8-无BOM的小细节分析
Aug 16 PHP
php中base_convert()进制数字转换函数实例
Nov 20 PHP
新浪SAE搭建PHP项目教程
Jan 28 PHP
PHP实现多维数组转字符串和多维数组转一维数组的方法
Aug 08 PHP
ThinkPHP的常用配置选项汇总
Mar 24 PHP
php版微信公众平台开发之验证步骤实例详解
Sep 23 PHP
PHP使用递归算法无限遍历数组示例
Jan 13 PHP
在TP5数据库中四个字段实现无限分类的示例
Oct 18 PHP
laravel 框架执行流程与原理简单分析
Feb 01 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的webservice的wsdl的XML无法显示问题的解决方法
2014/03/11 PHP
PHP命名空间namespace用法实例分析
2016/09/27 PHP
js跟随滚动条滚动浮动代码
2009/12/31 Javascript
Javascript读取cookie函数代码
2010/10/16 Javascript
Javascript实现动态菜单添加的实例代码
2013/07/05 Javascript
页面元素绑定jquery toggle后元素隐藏的解决方法
2014/03/27 Javascript
js淡入淡出的图片轮播效果代码分享
2015/08/24 Javascript
JavaScript判断FileUpload控件上传文件类型
2015/09/28 Javascript
JS+CSS实现的竖向简洁折叠菜单效果代码
2015/10/22 Javascript
js实现简单计算器
2015/11/22 Javascript
html5+javascript实现简单上传的注意细节
2016/04/18 Javascript
jQuery获取file控件中图片的宽高与大小
2016/08/04 Javascript
jsp 自动编译机制详细介绍
2016/12/01 Javascript
Bootstrap基本插件学习笔记之轮播幻灯片(23)
2016/12/08 Javascript
scroll事件实现监控滚动条并分页显示(zepto.js)
2016/12/18 Javascript
[53:52]OG vs EG 2018国际邀请赛淘汰赛BO3 第二场 8.23
2018/08/24 DOTA
详细介绍Python语言中的按位运算符
2013/11/26 Python
Python实现向服务器请求压缩数据及解压缩数据的方法示例
2017/06/09 Python
django js实现部分页面刷新的示例代码
2018/05/28 Python
pyQt4实现俄罗斯方块游戏
2018/06/26 Python
深入了解Python枚举类型的相关知识
2019/07/09 Python
pytorch AvgPool2d函数使用详解
2020/01/03 Python
python实现密度聚类(模板代码+sklearn代码)
2020/04/27 Python
解决Python 函数声明先后顺序出现的问题
2020/09/02 Python
拉斯维加斯城市观光通行证:Las Vegas Pass
2019/05/21 全球购物
精美的手工家居和生活用品:Nkuku
2019/11/01 全球购物
CheapTickets泰国:廉价航班,查看促销价格并预订机票
2019/12/28 全球购物
私有程序集与共享程序集有什么区别
2013/04/05 面试题
Tomcat的缺省是多少,怎么修改
2014/04/09 面试题
应届行政管理专业个人自我评价
2013/12/28 职场文书
运动会拉拉队口号
2014/06/09 职场文书
上课玩手机的检讨书
2014/10/01 职场文书
摩登时代观后感
2015/06/03 职场文书
厉行节约工作总结
2015/08/12 职场文书
500字作文之难忘的同学
2019/12/20 职场文书
《帝国时代4》赛季预告 新增内容编译器可创造地图
2022/04/03 其他游戏