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 简单数组排序实现代码
Aug 05 PHP
在PHP中养成7个面向对象的好习惯
Jan 28 PHP
PHP IN_ARRAY 函数使用注意事项
Jul 24 PHP
ThinkPHP CURD方法之order方法详解
Jun 18 PHP
php中实现获取随机数组列表的自定义函数
Apr 02 PHP
表单提交错误后返回内容消失问题的解决方法(PHP网站)
Oct 20 PHP
PHP使用MPDF类生成PDF的方法
Dec 08 PHP
利用Laravel事件系统如何实现登录日志的记录详解
May 20 PHP
PHP简单实现模拟登陆功能示例
Sep 15 PHP
PHP 记录访客的浏览信息方法
Jan 29 PHP
详解laravel安装使用Passport(Api认证)
Jul 27 PHP
TP5框架model常见操作示例小结【增删改查、聚合、时间戳、软删除等】
Apr 05 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设计模式 Delegation(委托模式)
2011/06/26 PHP
PHP+jQuery+Ajax实现分页效果 jPaginate插件的应用
2015/10/09 PHP
针对多用户实现头像上传功能PHP代码 适用于登陆页面制作
2016/08/17 PHP
Laravel使用scout集成elasticsearch做全文搜索的实现方法
2018/11/30 PHP
比较搞笑的js陷阱题
2010/02/07 Javascript
利用js实现遮罩以及弹出可移动登录窗口
2013/07/08 Javascript
jquery预加载图片的方法
2015/05/27 Javascript
jQuery form插件之formDdata参数校验表单及验证后提交
2016/01/23 Javascript
bootstrap datetimepicker日期插件超详细使用方法介绍
2017/02/23 Javascript
原生js仿淘宝网商品放大镜效果
2017/02/28 Javascript
微信小程序 实例开发总结
2017/04/26 Javascript
BackBone及其实例探究_动力节点Java学院整理
2017/07/14 Javascript
ES6之模版字符串的具体使用
2018/05/17 Javascript
vue实现多组关键词对应高亮显示功能
2019/07/25 Javascript
NUXT SSR初级入门笔记(小结)
2019/12/16 Javascript
node.js Promise对象的使用方法实例分析
2019/12/26 Javascript
[01:28:31]《加油DOTA》真人秀 第五期
2014/09/01 DOTA
使用python解析xml成对应的html示例分享
2014/04/02 Python
Python的ORM框架SQLAlchemy入门教程
2014/04/28 Python
Python爬虫:url中带字典列表参数的编码转换方法
2019/08/21 Python
关于pycharm中pip版本10.0无法使用的解决办法
2019/10/10 Python
python操作docx写入内容,并控制文本的字体颜色
2020/02/13 Python
详细分析Python可变对象和不可变对象
2020/07/09 Python
python 解决pycharm运行py文件只有unittest选项的问题
2020/09/01 Python
python PIL模块的基本使用
2020/09/29 Python
Jupyter notebook命令和编辑模式常用快捷键汇总
2020/11/17 Python
Python批量删除mysql中千万级大量数据的脚本分享
2020/12/03 Python
银行介绍信范文
2014/01/10 职场文书
点菜员岗位职责范本
2014/02/14 职场文书
应届大专生求职信
2014/06/26 职场文书
房地产端午节活动方案
2014/08/24 职场文书
教师党员自我剖析材料
2014/09/29 职场文书
现役军人家属慰问信
2015/03/24 职场文书
pytorch 运行一段时间后出现GPU OOM的问题
2021/06/02 Python
Java图书管理系统,课程设计必用(源码+文档)
2021/06/30 Java/Android
VUE解决跨域问题Access to XMLHttpRequest at
2022/05/06 Vue.js