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中使用sockets:从新闻组中获取文章
Oct 09 PHP
php下防止单引号,双引号在接受页面转义的设置方法
Sep 25 PHP
php中unlink()、mkdir()、rmdir()等方法的使用介绍
Dec 21 PHP
如何利用php array_multisort函数 对数据库结果进行复杂排序
Jun 08 PHP
ThinkPHP验证码使用简明教程
Mar 05 PHP
PHP伪静态Rewrite设置之APACHE篇
Jul 30 PHP
PHP循环遍历数组的3种方法list()、each()和while总结
Nov 19 PHP
php去除头尾空格的2种方法
Mar 16 PHP
PHP调试的强悍利器之PHPDBG
Feb 22 PHP
项目中应用Redis+Php的场景
May 22 PHP
laravel实现前后台路由分离的方法
Oct 13 PHP
浅谈PHP之ThinkPHP框架使用详解
Jul 21 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的库,结果发现很多东西
2006/12/31 PHP
phpMyAdmin 安装配置方法和问题解决
2009/06/08 PHP
php使用标签替换的方式生成静态页面
2015/05/21 PHP
thinkPHP多语言切换设置方法详解
2016/11/11 PHP
php实现留言板功能
2017/03/05 PHP
PHP迭代器接口Iterator用法分析
2017/12/28 PHP
php微信公众号开发之校园图书馆
2018/10/20 PHP
ajax 缓存 问题 requestheader
2010/08/01 Javascript
javascript 基础篇4 window对象,DOM
2012/03/14 Javascript
一个获取第n个元素节点的js函数
2014/09/02 Javascript
JavaScript通过事件代理高亮显示表格行的方法
2015/05/27 Javascript
CSS图片响应式 垂直水平居中
2015/08/14 Javascript
jQuery实现简单下拉导航效果
2015/09/07 Javascript
JavaScript实现瀑布流布局
2020/06/28 Javascript
JavaScript中的prototype原型学习指南
2016/05/09 Javascript
jQuery实现的跨容器无缝拖动效果代码
2016/06/21 Javascript
详谈JavaScript的闭包及应用
2017/01/17 Javascript
Javascript中的async awai的用法
2017/05/17 Javascript
JavaScript基于扩展String实现替换字符串中index处字符的方法
2017/06/13 Javascript
微信小程序表单验证功能完整实例
2017/12/01 Javascript
vue实现局部刷新的实现示例
2019/04/16 Javascript
jQuery中event.target和this的区别详解
2020/08/13 jQuery
JS变量提升及函数提升实例解析
2020/09/03 Javascript
记录Django开发心得
2014/07/16 Python
Python多线程实现同步的四种方式
2017/05/02 Python
Python request设置HTTPS代理代码解析
2018/02/12 Python
python获取酷狗音乐top500的下载地址 MP3格式
2018/04/17 Python
Python安装lz4-0.10.1遇到的坑
2018/05/20 Python
Flask和Django框架中自定义模型类的表名、父类相关问题分析
2018/07/19 Python
Python+Pandas 获取数据库并加入DataFrame的实例
2018/07/25 Python
详解pandas.DataFrame中删除包涵特定字符串所在的行
2019/04/04 Python
Python批量修改图片分辨率的实例代码
2019/07/04 Python
Python解析微信dat文件的方法
2020/11/30 Python
船舶工程技术专业求职信
2014/08/07 职场文书
JavaScript原型链详解
2021/11/07 Javascript
Mysql排查分析慢sql之explain实战案例
2022/04/19 MySQL