打造超酷的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 相关文章推荐
Windows下的PHP5.0详解
Nov 18 PHP
php去除重复字的实现代码
Sep 16 PHP
php数据库配置文件一般做法分享
Jul 07 PHP
php empty()与isset()区别的详细介绍
Jun 17 PHP
通过curl模拟post和get方式提交的表单类
Apr 23 PHP
PHP+Mysql+Ajax+JS实现省市区三级联动
May 23 PHP
php简单socket服务器客户端代码实例
May 18 PHP
php用户注册信息验证正则表达式
Nov 12 PHP
php+ajax无刷新分页实例详解
Dec 07 PHP
WordPress中注册菜单与调用菜单的方法详解
Dec 18 PHP
PHP 获取客户端 IP 地址的方法实例代码
Nov 11 PHP
PHP pthreads v3下同步处理synchronized用法示例
Feb 21 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通过COM类调用组件的实现代码
2012/01/11 PHP
几种有用的变型 PHP中循环语句的用法介绍
2012/01/30 PHP
PHP常用数组函数介绍
2014/07/28 PHP
PHP使用socket发送HTTP请求的方法
2016/02/14 PHP
PHP中cookie知识点学习
2018/05/06 PHP
JavaScript 参数中的数组展开 [译]
2012/09/21 Javascript
用innerhtml提高页面打开速度的方法
2013/08/02 Javascript
jQuery中each()方法用法实例
2014/12/27 Javascript
JavaScript生成的动态下雨背景效果实现方法
2015/02/25 Javascript
自定义jQuery插件方式实现强制对象重绘的方法
2015/03/23 Javascript
Bootstrap自动适应PC、平板、手机的Bootstrap栅格系统
2016/05/27 Javascript
JS简单实现无缝滚动效果实例
2016/08/24 Javascript
响应式表格之固定表头的简单实现
2016/08/26 Javascript
Vue学习笔记进阶篇之vue-cli安装及介绍
2017/07/18 Javascript
解决html input验证只能输入数字,不能输入其他的问题
2017/07/21 Javascript
vue滚动轴插件better-scroll使用详解
2017/10/17 Javascript
vue 实现通过手机发送短信验证码注册功能
2018/04/19 Javascript
vue-cli基础配置及webpack配置修改的完整步骤
2019/10/20 Javascript
Javascript异步执行不按顺序解决方案
2020/04/30 Javascript
js将日期格式转换为YYYY-MM-DD HH:MM:SS
2020/09/18 Javascript
Python中DJANGO简单测试实例
2015/05/11 Python
使用PyCharm配合部署Python的Django框架的配置纪实
2015/11/19 Python
基于Python对数据shape的常见操作详解
2018/12/25 Python
Python中的异常处理try/except/finally/raise用法分析
2019/02/28 Python
Python常见读写文件操作实例总结【文本、json、csv、pdf等】
2019/04/15 Python
简单了解python的内存管理机制
2019/07/08 Python
python实现最速下降法
2020/03/24 Python
Django与AJAX实现网页动态数据显示的示例代码
2021/02/24 Python
详解HTML5中ol标签的用法
2015/09/08 HTML / CSS
药学专业个人的自我评价
2013/12/31 职场文书
个人收入证明模板
2014/09/18 职场文书
给老婆的保证书
2015/01/16 职场文书
惹女朋友生气检讨书
2015/05/06 职场文书
贷款收入证明格式
2015/06/24 职场文书
使用Python脚本对GiteePages进行一键部署的使用说明
2021/05/27 Python
Python快速实现一键抠图功能的全过程
2021/06/29 Python