打造超酷的PHP数据饼图效果实现代码


Posted in PHP onNovember 23, 2011

效果图:
打造超酷的PHP数据饼图效果实现代码
源代码:
[code]
<?
//+------------------------+
//| pie3dfun.PHP//公用函数 |
//+------------------------+
define("ANGLE_STEP", 5); //定义画椭圆弧时的角度步长
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=250,$b=120,$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; 333
$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, "./simsun.ttf", $str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)");
$ly += $fh+2;
}
//输出图形
header("Content-type: image/png");
//输出生成的图片
$imgFileName = "../temp/".time().".png";
imagepng($img,$imgFileName);
echo '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
$datLst = array(30, 10, 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 批量替换html标签的实例代码
Nov 26 PHP
php实现读取超大文件的方法
Jul 28 PHP
php单态设计模式(单例模式)实例
Nov 18 PHP
Yii的CDbCriteria查询条件用法实例
Dec 04 PHP
ecshop后台编辑器替换成ueditor编辑器
Mar 03 PHP
PHP MPDF中文乱码的解决方式
Dec 08 PHP
PHP实现适用于自定义的验证码类
Jun 15 PHP
Yii2-GridView 中让关联字段带搜索和排序功能示例
Jan 21 PHP
PHP中error_reporting函数用法详细介绍
Jun 11 PHP
PHP设计模式之原型模式定义与用法详解
Apr 03 PHP
ThinkPHP5.1表单令牌Token失效问题的解决
Mar 22 PHP
PHP copy函数使用案例代码解析
Sep 01 PHP
DISCUZ在win2003环境下 Unable to access ./include/common.inc.php in... 的问题终极解决方案
Nov 21 #PHP
一个PHP的QRcode类与大家分享
Nov 13 #PHP
PHP提取字符串中的图片地址[正则表达式]
Nov 12 #PHP
PHP学习散记_编码(json_encode 中文不显示)
Nov 10 #PHP
PHP字符串函数系列之nl2br(),在字符串中的每个新行 (\n) 之前插入 HTML 换行符br
Nov 10 #PHP
php数组函数序列之array_intersect() 返回两个或多个数组的交集数组
Nov 10 #PHP
php中一个完整表单处理实现代码
Nov 10 #PHP
You might like
PHP json_encode中文乱码问题的解决办法
2013/09/09 PHP
PHP获取浏览器信息类和客户端地理位置的2个方法
2014/04/24 PHP
PHP实现动态压缩js与css文件的方法
2018/05/02 PHP
laravel 模型查询按照whereIn排序的示例
2019/10/16 PHP
JavaScript实际应用:innerHTMl和确认提示的使用
2006/06/22 Javascript
关于JavaScript中string 的replace
2013/04/12 Javascript
js动态设置鼠标事件示例代码
2013/10/30 Javascript
JS模拟并美化的表单控件完整实例
2015/08/19 Javascript
jquery实现ajax提交表单信息的简单方法(推荐)
2016/08/24 Javascript
AngularJs Understanding the Controller Component
2016/09/02 Javascript
js实现的在线调色板功能完整实例
2016/12/21 Javascript
javascript中replace使用方法总结
2017/03/01 Javascript
详解vue项目首页加载速度优化
2017/10/18 Javascript
Vue.js中的computed工作原理
2018/03/22 Javascript
axios向后台传递数组作为参数的方法
2018/08/11 Javascript
解决VUE项目localhost端口服务器拒绝连接,只能用127.0.0.1的问题
2020/08/14 Javascript
vue a标签点击实现赋值方式
2020/09/07 Javascript
[58:23]LGD vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
利用soaplib搭建webservice详细步骤和实例代码
2013/11/20 Python
python3.3实现乘法表示例
2014/02/07 Python
Python基于递归算法实现的走迷宫问题
2017/08/04 Python
Ubuntu安装Jupyter Notebook教程
2017/10/18 Python
python调用自定义函数的实例操作
2019/06/26 Python
使用Pyhton 分析酒店针孔摄像头
2020/03/04 Python
Python实现的北京积分落户数据分析示例
2020/03/27 Python
Django Channel实时推送与聊天的示例代码
2020/04/30 Python
微软俄罗斯官方网站:Microsoft俄罗斯
2016/09/18 全球购物
编辑个人求职信范文
2013/09/21 职场文书
元旦联欢会感言
2014/03/04 职场文书
马云北大演讲完整版:真心话,什么才是阿里的核心竞争力?
2014/04/04 职场文书
办理护照工作证明
2014/10/10 职场文书
优秀党支部申报材料
2014/12/24 职场文书
社区党支部承诺书
2015/04/29 职场文书
三方合作意向书范本
2015/05/09 职场文书
Java 在生活中的 10 大应用
2021/11/02 Java/Android
《现实主义勇者的王国再建记》第三弹OST全曲试听片段公开
2022/04/04 日漫