php 三维饼图的实现代码


Posted in PHP onSeptember 28, 2008

经过努力pie3d完成了,好东西与大家分享。不过小弟是php新手,代码可能不够精炼,希望大家指教共同来完善这个程序。记得通知我(estorm@yeah.net)
+------------------------+
| pie3dfun.php//公用函数 |
+------------------------+
define("ANGLE_STEP",5);//定义画椭圆弧时的角度步长
function chx_getdarkcolor($img,$clr){//求$clr对应的暗色
$rgb=imagecolorsforindex($img,$clr);
return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
}
function chx_getexy($a,$b,$d){//求角度$d对应的椭圆上的点坐标
$d=deg2rad($d);
return array(round($a*Cos($d)),round($b*Sin($d)));
}
function chx_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr){//椭圆弧函数
$n=ceil(($ed-$sd)/ANGLE_STEP);
$d=$sd;
list($x0,$y0)=chx_getexy($a,$b,$d);
for($i=0;$i<$n;$i++){
$d=($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
list($x,$y)=chx_getexy($a,$b,$d);
imageline($img,$x0+$ox,$y0+$oy,$x+$ox,$y+$oy,$clr);
$x0=$x;
$y0=$y;
}
}
function chx_sector($img,$ox,$oy,$a,$b,$sd,$ed,$clr){//画扇面
$n=ceil(($ed-$sd)/ANGLE_STEP);
$d=$sd;
list($x0,$y0)=chx_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)=chx_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)=chx_getexy($a/2,$b/2,($d+$sd)/2);
imagefill($img,$x+$ox,$y+$oy,$clr);
}
function chx_sector3d($img,$ox,$oy,$a,$b,$v,$sd,$ed,$clr){//3d扇面
chx_sector($img,$ox,$oy,$a,$b,$sd,$ed,$clr);
if($sd<180){
list($R,$G,$B)=chx_getdarkcolor($img,$clr);
$clr=imagecolorallocate($img,$R,$G,$B);
if($ed>180) $ed=180;
list($sx,$sy)=chx_getexy($a,$b,$sd);
$sx+=$ox;
$sy+=$oy;
list($ex,$ey)=chx_getexy($a,$b,$ed);
$ex+=$ox;
$ey+=$oy;
imageline($img,$sx,$sy,$sx,$sy+$v,$clr);
imageline($img,$ex,$ey,$ex,$ey+$v,$clr);
chx_arc($img,$ox,$oy+$v,$a,$b,$sd,$ed,$clr);
list($sx,$sy)=chx_getexy($a,$b,($sd+$ed)/2);
$sy+=$oy+$v/2;
$sx+=$ox;
imagefill($img,$sx,$sy,$clr);
}
}
function chx_getindexcolor($img,$clr){//RBG转索引色
$R=($clr>>16) & 0xff;
$G=($clr>>8)& 0xff;
$B=($clr) & 0xff;
return imagecolorallocate($img,$R,$G,$B);
}
?>
+--------------------------+
| pie3d.php //三维饼图文件 |
+--------------------------+
require("pie3dfun.php");
$a=150;//椭圆长半轴
$b=50;//椭圆段半轴
$v=20;//圆饼高度
$font=5;//字体
$ox=5+$a;
$oy=5+$b;
$fw=imagefontwidth($font);
$fh=imagefontheight($font);

$datLst=array(30,10,20,20,10,20,10,20);//数据
$labLst=array("a1","a2","a3","a4","a5","a6","a7","a8");//标签
$clrLst=array(0x99ff00,0xff6666,0x0099ff,0xff99ff,0xffff99,0x99ffff,0xff3333,0x009999);
$w=10+$a*2;
$h=10+$b*2+$v+($fh+2)*count($datLst);
$img=imagecreate($w,$h);
//转RGB为索引色
for($i=0;$i
$clrbk=imagecolorallocate($img,0xff,0xff,0xff);
$clrt=imagecolorallocate($img,0x00,0x00,0x00);
//填充背景色
imagefill($img,0,0,$clrbk);
//求和
$tot=0;
for($i=0;$i
$sd=0;
$ed=0;
$ly=10+$b*2+$v;
for($i=0;$i $sd=$ed;
$ed+=$datLst[$i]/$tot*360;
//画圆饼
chx_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);
$ly+=$fh+2;
}
//输出图形
header("Content-type:image/gif");
imagegif($img);
?>

PHP 相关文章推荐
编写漂亮的代码 - 将后台程序与前端程序分开
Apr 23 PHP
在yii中新增一个用户验证的方法详解
Jun 20 PHP
Codeigniter出现错误提示Error with CACHE directory的解决方案
Jun 12 PHP
php编写的一个E-mail验证类
Mar 25 PHP
php操作MongoDB类实例
Jun 17 PHP
实例详解PHP中html word 互转的方法
Jan 28 PHP
thinkphp下MySQL数据库读写分离代码剖析
Apr 18 PHP
PHP 实现页面静态化的几种方法
Jul 23 PHP
Laravel5.0+框架邮件发送功能实现方法图文与实例详解
Apr 23 PHP
Yii2框架视图(View)操作及Layout的使用方法分析
May 27 PHP
Thinkphp5 如何隐藏入口文件index.php(URL重写)
Oct 16 PHP
php解析非标准json、非规范json的方式实例
May 10 PHP
PHP控制网页过期时间的代码
Sep 28 #PHP
PHP集成FCK的函数代码
Sep 27 #PHP
php横向重复区域显示二法
Sep 25 #PHP
php下防止单引号,双引号在接受页面转义的设置方法
Sep 25 #PHP
PHP伪造referer实例代码
Sep 20 #PHP
PHP面向对象分析设计的经验原则
Sep 20 #PHP
php 301转向实现代码
Sep 18 #PHP
You might like
将RTF格式的文件转成HTML并在网页中显示的代码
2006/10/09 PHP
php db类库进行数据库操作
2009/03/19 PHP
PHP UTF8编码内的繁简转换类
2009/07/20 PHP
php Smarty date_format [格式化时间日期]
2010/03/15 PHP
php in_array 函数使用说明与in_array需要注意的地方说明
2010/04/13 PHP
PHP遍历二维数组的代码
2011/04/22 PHP
在php中判断一个请求是ajax请求还是普通请求的方法
2011/06/28 PHP
PHP开发制作一个简单的活动日程表Calendar
2016/06/20 PHP
Google的跟踪代码 动态加载js代码方法应用
2012/11/12 Javascript
jQuery中ajax的使用与缓存问题的解决方法
2013/12/19 Javascript
jquery点赞功能实现代码 点个赞吧!
2020/05/29 jQuery
详解Vue路由History mode模式中页面无法渲染的原因及解决
2017/09/28 Javascript
VueRouter导航守卫用法详解
2017/12/25 Javascript
如何基于vue-cli3.0构建功能完善的移动端架子
2019/04/24 Javascript
jQuery-Citys省市区三级菜单联动插件使用详解
2019/07/26 jQuery
vue.js封装switch开关组件的操作
2020/10/26 Javascript
[02:17]《辉夜杯》TRG战队巡礼
2015/10/26 DOTA
详谈Python基础之内置函数和递归
2017/06/21 Python
Python实现的圆形绘制(画圆)示例
2018/01/31 Python
Django配置文件代码说明
2019/12/04 Python
Tensorflow读取并输出已保存模型的权重数值方式
2020/01/04 Python
Python装饰器原理与基本用法分析
2020/01/07 Python
pytorch实现线性拟合方式
2020/01/15 Python
关于Tensorflow分布式并行策略
2020/02/03 Python
Django高并发负载均衡实现原理详解
2020/04/04 Python
一篇文章搞懂python的转义字符及用法
2020/09/03 Python
Python Pillow(PIL)库的用法详解
2020/09/19 Python
CSS3打造百度贴吧的3D翻牌效果示例
2017/01/04 HTML / CSS
捐资助学倡议书
2014/04/15 职场文书
小学课外阅读总结
2014/07/09 职场文书
反四风个人对照检查材料
2014/09/26 职场文书
2015年中个人总结范文
2015/03/10 职场文书
分享:关于学习的励志名言赏析
2019/08/16 职场文书
MySQL8.0.18配置多主一从
2021/06/21 MySQL
MySQL系列之开篇 MySQL关系型数据库基础概念
2021/07/02 MySQL
html中两种获取标签内的值的方法
2022/06/10 HTML / CSS