打造超酷的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缓存函数的使用说明
May 10 PHP
php 获取本地IP代码
Jun 23 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十六)
Jun 30 PHP
ThinkPHP 3.2 数据分页代码分享
Oct 14 PHP
PHP中让curl支持sock5的代码实例
Jan 21 PHP
使用php+swoole对client数据实时更新(一)
Jan 07 PHP
Yii2 批量插入、更新数据实例
Mar 15 PHP
PHP中类的自动加载的方法
Mar 17 PHP
PHP对象相关知识总结
Apr 09 PHP
PHP实现一维数组与二维数组去重功能示例
May 24 PHP
PHP单元测试配置与使用方法详解
Dec 27 PHP
PHP+ajax实现上传、删除、修改单张图片及后台处理逻辑操作详解
Feb 12 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生成xml时添加CDATA标签的方法
2014/10/17 PHP
php实现从上传文件创建缩略图的方法
2015/04/02 PHP
WampServer搭建php环境时遇到的问题汇总
2015/07/23 PHP
PHP连接MSSQL方法汇总
2016/02/05 PHP
Symfony模板的快捷变量用法实例
2016/03/17 PHP
php 浮点数比较方法详解
2017/05/05 PHP
extjs form textfield的隐藏方法
2008/12/29 Javascript
js二维数组定义和初始化的三种方法总结
2014/03/03 Javascript
Javascript实现简单二级下拉菜单实例
2014/06/15 Javascript
jquery获取radio值(单选组radio)
2014/10/16 Javascript
jQuery实现平滑滚动到指定锚点的方法
2015/03/20 Javascript
QQ登录背景闪动效果附效果演示源码下载
2015/09/22 Javascript
jQuery页面加载初始化的3种方法(推荐)
2016/06/02 Javascript
js获取浏览器高度 窗口高度 元素尺寸 偏移属性的方法
2016/11/21 Javascript
Ajax异步获取html数据中包含js方法无效的解决方法
2017/02/20 Javascript
vue微信分享的实现(在当前页面分享其他页面)
2019/04/16 Javascript
[08:38]DOTA2-DPC中国联赛 正赛 VG vs Elephant 选手采访
2021/03/11 DOTA
Python多线程编程(五):死锁的形成
2015/04/05 Python
Django中使用Celery的教程详解
2018/08/24 Python
Python3解释器知识点总结
2019/02/19 Python
详解Python可视化神器Yellowbrick使用
2019/11/11 Python
python使用html2text库实现从HTML转markdown的方法详解
2020/02/21 Python
matplotlib 曲线图 和 折线图 plt.plot()实例
2020/04/17 Python
eBay加拿大站:eBay.ca
2019/06/20 全球购物
C#面试问题
2016/07/29 面试题
公务员年总结的自我评价
2013/10/25 职场文书
英语系本科生求职信范文
2013/12/18 职场文书
测控技术与仪器个人求职信范文
2013/12/30 职场文书
开办大学饮食联盟创业计划书
2014/01/29 职场文书
善意的谎言事例
2014/02/15 职场文书
《菜园里》教学反思
2014/04/17 职场文书
助人为乐模范事迹材料
2014/06/02 职场文书
环保口号大全
2014/06/12 职场文书
个人纪律作风整改措施思想汇报
2014/10/12 职场文书
学校师德师风整改方案
2014/10/28 职场文书
2014年教师工作总结
2014/11/10 职场文书