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 相关文章推荐
PHP网上调查系统
Oct 09 PHP
PHP session常见问题集锦及解决办法总结
Mar 18 PHP
php学习笔记 [预定义数组(超全局数组)]
Jun 09 PHP
php后台如何避免用户直接进入方法实例
Oct 15 PHP
php实现水仙花数的4个示例分享
Apr 08 PHP
ThinkPHP实现多数据库连接的解决方法
Jul 01 PHP
PHP基于CURL进行POST数据上传实例
Nov 10 PHP
PHP的Laravel框架中使用消息队列queue及异步队列的方法
Mar 21 PHP
PHP扩展框架之Yaf框架的安装与使用
May 18 PHP
Laravel手动分页实现方法详解
Oct 09 PHP
微信公众平台开发-微信服务器IP接口实例(含源码)
Mar 05 PHP
PHP实现十进制数字与二十六进制字母串相互转换操作示例
Aug 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
PHP小程序自动提交到自助友情连接
2009/11/24 PHP
PHP中strtotime函数使用方法分享
2012/01/10 PHP
php一些错误处理的方法与技巧总结
2013/08/10 PHP
juqery 学习之四 筛选查找
2010/11/30 Javascript
JS获取URL中参数值(QueryString)的4种方法分享
2014/04/12 Javascript
node.js中的fs.createWriteStream方法使用说明
2014/12/17 Javascript
javascript数组去重的方法汇总
2015/04/14 Javascript
javascript动态创建表格及添加数据实例详解
2015/05/13 Javascript
JavaScript小技巧整理篇(非常全)
2016/01/26 Javascript
在页面中输出当前客户端时间javascript实例代码
2016/03/02 Javascript
JS获取元素多层嵌套思路详解
2016/05/16 Javascript
详解JavaScript中数组的reduce方法
2016/12/02 Javascript
bootstrap按钮插件(Button)使用方法解析
2017/01/13 Javascript
AngularJS框架中的双向数据绑定机制详解【减少需要重复的开发代码量】
2017/01/19 Javascript
bootstrap table操作技巧分享
2017/02/15 Javascript
BootStrap Validator 根据条件在JS中添加或移除校验操作
2017/10/12 Javascript
vue-cli 自定义指令directive 添加验证滑块示例
2017/10/19 Javascript
node.js基于express使用websocket的方法
2017/11/09 Javascript
JavaScript实现写入文件到本地的方法【基于FileSaver.js插件】
2018/03/15 Javascript
Vue2.0 实现单选互斥的方法
2018/04/13 Javascript
JavaScript变量基本使用方法实例分析
2019/11/15 Javascript
Python实现从百度API获取天气的方法
2015/03/11 Python
Python生成器(Generator)详解
2015/04/13 Python
使用Python 统计高频字数的方法
2019/01/31 Python
Python Pandas数据结构简单介绍
2019/07/03 Python
详解Anaconda安装tensorflow报错问题解决方法
2020/11/01 Python
中国首家奢侈品O2O网购平台:第五大道奢侈品网
2017/12/14 全球购物
Bath & Body Works阿联酋:在线购买沐浴和身体用品
2021/02/27 全球购物
学校司机岗位职责
2013/11/14 职场文书
文明村镇申报材料
2014/05/06 职场文书
2014年销售经理工作总结
2014/12/01 职场文书
行政处罚告知书
2015/07/01 职场文书
2016党校学习心得体会范文
2016/01/07 职场文书
《中国机长》观后感:敬畏生命,敬畏职责
2019/11/12 职场文书
Python入门之使用pandas分析excel数据
2021/05/12 Python
MySQL中LAG()函数和LEAD()函数的使用
2022/08/14 MySQL