打造超酷的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与php MySQL 之间的关系
Jul 17 PHP
利用Memcached在php下实现session机制 替换PHP的原生session支持
Aug 21 PHP
thinkphp3.0 模板中函数的使用
Nov 13 PHP
兼容PHP和Java的des加密解密代码分享
Jun 26 PHP
浅析ThinkPHP的模板输出功能
Jul 01 PHP
php可应用于面包屑导航的迭代寻找家谱树实现方法
Feb 02 PHP
检测codeigniter脚本消耗内存情况的方法
Mar 21 PHP
详解php中空字符串和0之间的关系
Oct 23 PHP
Laravel模型事件的实现原理详解
Mar 14 PHP
PHP基于cookie实现统计在线人数功能示例
Jan 16 PHP
Laravel项目中timeAgo字段语言转换的改善方法示例
Sep 16 PHP
PHP cookie与session会话基本用法实例分析
Nov 18 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
让你的网站首页自动选择语言转跳
2006/12/06 PHP
一些PHP Coding Tips(php小技巧)[2011/04/02最后更新]
2011/05/02 PHP
phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法
2011/08/18 PHP
PHP中3种生成XML文件方法的速度效率比较
2012/10/06 PHP
深入PHP异步执行的详解
2013/06/03 PHP
PHP中spl_autoload_register()和__autoload()区别分析
2014/05/10 PHP
ThinkPHP权限认证Auth实例详解
2014/07/22 PHP
PHP实现加强版加密解密类实例
2015/07/29 PHP
PHP7.0安装笔记整理
2015/08/28 PHP
基于Laravel5.4实现多字段登录功能方法示例
2017/08/11 PHP
PHP中的浅复制与深复制的实例详解
2017/10/26 PHP
Thinkphp 3.2框架使用Redis的方法详解
2019/10/24 PHP
php 多继承的几种常见实现方法示例
2019/11/18 PHP
ie 处理 gif动画 的onload 事件的一个 bug
2007/04/12 Javascript
javascript+css 网页每次加载不同样式的实现方法
2009/12/27 Javascript
js禁止小键盘输入数字功能代码
2011/08/01 Javascript
html中table数据排序的js代码
2011/08/09 Javascript
检测input每次的输入是否合法遇到汉字输入就有问题
2012/05/23 Javascript
JAVASCRIPT函数作用域和提前声明 分享
2013/08/22 Javascript
标题过长使用javascript按字节截取字符串
2014/04/24 Javascript
json中换行符的处理方法示例介绍
2014/06/10 Javascript
jquery动态分页效果堪比时光网
2014/09/25 Javascript
javascript 自定义回调函数示例代码
2014/09/26 Javascript
js+html5绘制图片到canvas的方法
2015/06/05 Javascript
举例讲解JavaScript中关于对象操作的相关知识
2015/11/16 Javascript
动态加载css方法实现和深入解析
2017/01/18 Javascript
javascript 初学教程及五子棋小程序的简单实现
2017/07/04 Javascript
react-native DatePicker日期选择组件的实现代码
2017/09/12 Javascript
Vue.js中关于侦听器(watch)的高级用法示例
2018/05/02 Javascript
解决LayUI表单获取不到data的问题
2018/08/20 Javascript
JS实现随机抽取三人
2019/11/06 Javascript
全面解析Vue中的$nextTick
2020/12/24 Vue.js
Python netmiko模块的使用
2020/02/14 Python
茱莉蔻美国官网:Jurlique美国
2020/11/24 全球购物
团队激励口号
2014/06/06 职场文书
2014年小学教师工作自我评价
2014/09/22 职场文书