打造超酷的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 相关文章推荐
一个简单实现多条件查询的例子
Oct 09 PHP
php中文字符截取防乱码
Mar 28 PHP
mysql+php分页类(已测)
Mar 31 PHP
细谈php中SQL注入攻击与XSS攻击
Jun 10 PHP
Function eregi is deprecated (解决方法)
Jun 21 PHP
163的邮件用phpmailer发送(实例详解)
Jun 24 PHP
IIS下PHP的三种配置方式对比
Nov 20 PHP
php导入大量数据到mysql性能优化技巧
Dec 29 PHP
浅谈PHP的反射机制
Dec 15 PHP
php简单随机字符串生成方法示例
Apr 19 PHP
PHP观察者模式实例分析【对比JS观察者模式】
May 22 PHP
浅谈php常用的7大框架的优缺点
Jul 20 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
ThinkPHP学习笔记(一)ThinkPHP部署
2014/06/22 PHP
PHP使用memcache缓存技术提高响应速度的方法
2014/12/26 PHP
PHP 用session与gd库实现简单验证码生成与验证的类方法
2016/11/15 PHP
Prototype使用指南之base.js
2007/01/10 Javascript
对JavaScript的eval()中使用函数的进一步讨论
2008/07/26 Javascript
JS中 用户登录系统的解决办法
2013/04/15 Javascript
JavaScript获取XML数据附示例截图
2014/03/05 Javascript
js确认删除对话框适用于a标签及submit
2014/07/10 Javascript
详解JavaScript中数组的reduce方法
2016/12/02 Javascript
Bootstrap 网格系统布局详解
2017/03/19 Javascript
BootStrap TreeView使用实例详解
2017/11/01 Javascript
JS实现的文字间歇循环滚动效果完整示例
2018/02/13 Javascript
详解vue填坑之解决部分浏览器不支持pushState方法
2018/07/12 Javascript
mpvue跳转页面及注意事项
2018/08/03 Javascript
总结javascript三元运算符知识点
2018/09/28 Javascript
TypeScript基础入门教程之三重斜线指令详解
2018/10/22 Javascript
如何使用puppet替换文件中的string
2018/12/06 Javascript
react实现移动端下拉菜单的示例代码
2020/01/16 Javascript
记一次用ts+vuecli4重构项目的实现
2020/05/21 Javascript
angular中的post请求处理示例详解
2020/06/30 Javascript
[05:59]2018DOTA2国际邀请赛寻真——只为胜利的Secret
2018/08/13 DOTA
Python最长公共子串算法实例
2015/03/07 Python
在windows下Python打印彩色字体的方法
2018/05/15 Python
Python实现图片识别加翻译功能
2019/12/26 Python
Python基础之字符串常见操作经典实例详解
2020/02/26 Python
keras的load_model实现加载含有参数的自定义模型
2020/06/22 Python
浅谈keras中的后端backend及其相关函数(K.prod,K.cast)
2020/06/29 Python
一文读懂Python 枚举
2020/08/25 Python
简历中自我评价范文3则
2013/12/14 职场文书
公司成立感言
2014/01/11 职场文书
师说教学反思
2014/02/07 职场文书
党员学习中共十八大思想报告
2014/09/12 职场文书
人事文员岗位职责
2015/02/04 职场文书
个人工作年终总结
2015/03/09 职场文书
2016年师德先进个人事迹材料
2016/02/29 职场文书
2016年“我们的节日·中秋节”活动总结
2016/04/05 职场文书