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 相关文章推荐
php5.3 不支持 session_register() 此函数已启用的解决方法
Nov 12 PHP
yii实现级联下拉菜单的方法
Jul 31 PHP
PHP将HTML转换成文本的实现代码
Jan 21 PHP
详解PHP中instanceof关键字及instanceof关键字有什么作用
Nov 05 PHP
PHP中类的继承和用法实例分析
May 24 PHP
php处理复杂xml数据示例
Jul 11 PHP
php获取ip及网址的简单方法(必看)
Apr 01 PHP
php+resumablejs实现的分块上传 断点续传功能示例
Apr 18 PHP
Yii 2.0自带的验证码使用经验分享
Jun 19 PHP
PHP封装curl的调用接口及常用函数详解
May 31 PHP
Yii框架日志操作图文与实例详解
Sep 09 PHP
如何在Laravel5.8中正确地应用Repository设计模式
Nov 26 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
phpMyAdmin链接MySql错误 个人解决方案
2009/12/28 PHP
php获取指定日期之间的各个周和月的起止时间
2014/11/24 PHP
ThinkPHP提交表单时默认自动转义的解决方法
2014/11/25 PHP
php实现监控varnish缓存服务器的状态
2014/12/30 PHP
php实现搜索一维数组元素并删除二维数组对应元素的方法
2015/07/06 PHP
thinkPHP微信分享接口JSSDK用法实例
2017/07/07 PHP
一些经常会用到的Javascript检测函数
2010/05/31 Javascript
javascript 星级评分效果(手写)
2012/12/24 Javascript
JS比较两个时间大小的简单示例代码
2013/12/20 Javascript
js操纵dom生成下拉列表框的方法
2014/02/24 Javascript
jquery、js操作checkbox全选反选
2014/03/12 Javascript
JQuery 使用attr方法实现下拉列表选中
2014/10/13 Javascript
JS+Canvas实现的俄罗斯方块游戏完整实例
2016/12/12 Javascript
深入浅出webpack教程系列_安装与基本打包用法和命令参数详解
2017/09/10 Javascript
基于复选框demo(分享)
2017/09/27 Javascript
利用HBuilder打包前端开发webapp为apk的方法
2017/11/13 Javascript
js提取中文拼音首字母的封装工具类
2018/03/12 Javascript
关于Mac下安装nodejs、npm和cnpm的教程
2018/04/11 NodeJs
配置eslint规范项目代码风格
2019/03/11 Javascript
python字典值排序并取出前n个key值的方法
2018/10/17 Python
在Python中实现shuffle给列表洗牌
2018/11/08 Python
在python中按照特定顺序访问字典的方法详解
2018/12/14 Python
python把1变成01的步骤总结
2019/02/27 Python
django实现web接口 python3模拟Post请求方式
2019/11/19 Python
python列表的逆序遍历实现
2020/04/20 Python
django序列化时使用外键的真实值操作
2020/07/15 Python
5分钟弄清楚html5的drag and drop(小结)
2019/04/10 HTML / CSS
美国按摩椅批发网站:Titan Chair
2018/12/27 全球购物
采购主管工作职责
2013/12/12 职场文书
学习型班组申报材料
2014/05/31 职场文书
房地产资料员岗位职责
2014/07/02 职场文书
写字楼租赁意向书
2014/07/30 职场文书
党员个人自我剖析材料
2014/10/08 职场文书
幼儿教师辞职信范文
2015/03/02 职场文书
婚宴来宾致辞
2015/07/28 职场文书
vue+echarts实现多条折线图
2022/03/21 Vue.js