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 之入门篇
Dec 04 PHP
用来给图片加水印的PHP类
Apr 09 PHP
php校验表单检测字段是否为空的方法
Mar 20 PHP
PHP中list()函数用法实例简析
Jan 08 PHP
PHP基于单例模式实现的数据库操作基类
Jan 15 PHP
php基于curl重写file_get_contents函数实例
Nov 08 PHP
浅析PHP类的反射来实现依赖注入过程
Feb 06 PHP
详解PHP版本兼容之openssl调用参数
Jul 25 PHP
PHP获取数据库表中的数据插入新的表再原删除数据方法
Oct 12 PHP
PHP PDOStatement::nextRowset讲解
Feb 01 PHP
PHP+RabbitMQ实现消息队列的完整代码
Mar 20 PHP
Laravel框架中缓存的使用方法分析
Sep 06 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
Terran兵种介绍
2020/03/14 星际争霸
PHP备份数据库生成SQL文件并下载的函数代码
2012/02/05 PHP
再推荐十款免费的php开发工具
2015/11/09 PHP
PHP数组实例详解
2016/06/26 PHP
PHP里的$_GET数组介绍
2019/03/22 PHP
JavaScript 学习笔记(十五)
2010/01/28 Javascript
DIV菜单层实现代码
2010/11/19 Javascript
MultiSelect左右选择控件的设计与实现介绍
2013/06/08 Javascript
网页中表单按回车就自动提交的问题的解决方案
2014/11/03 Javascript
jquery zTree异步加载、模糊搜索简单实例分享
2016/03/24 Javascript
JavaScript中的操作符类型转换示例总结
2016/05/30 Javascript
浅析jQuery操作select控件的取值和设值
2016/12/07 Javascript
ES6中的箭头函数实例详解
2017/04/06 Javascript
基于Vue实例对象的数据选项
2017/08/09 Javascript
Node.js API详解之 timer模块用法实例分析
2020/05/07 Javascript
Vue——前端生成二维码的示例
2020/12/19 Vue.js
python操作redis的方法
2015/07/07 Python
Python 使用requests模块发送GET和POST请求的实现代码
2016/09/21 Python
python Flask实现restful api service
2017/12/04 Python
神经网络理论基础及Python实现详解
2017/12/15 Python
python数据处理 根据颜色对图片进行分类的方法
2018/12/08 Python
Python使用Selenium爬取淘宝异步加载的数据方法
2018/12/17 Python
基于Python+Appium实现京东双十一自动领金币功能
2019/10/31 Python
python进行OpenCV实战之画图(直线、矩形、圆形)
2020/08/27 Python
学会迭代器设计模式,帮你大幅提升python性能
2021/01/03 Python
使用CSS实现阅读进度条
2017/02/27 HTML / CSS
中专毕业自我鉴定
2013/10/16 职场文书
农业资源与环境专业自荐信范文
2013/12/30 职场文书
简历中自我评价怎么写
2014/02/12 职场文书
产品质量承诺范本
2014/03/31 职场文书
大学生社会实践活动总结
2014/07/03 职场文书
夫妻忠诚协议书范本
2014/11/17 职场文书
3.15消费者权益日活动总结
2015/02/09 职场文书
考勤制度通知
2015/04/25 职场文书
导游词之四川武侯祠
2019/10/21 职场文书
解决python存数据库速度太慢的问题
2021/04/23 Python