打造超酷的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 提速工具eAccelerator 配置参数详解
May 16 PHP
使用GROUP BY的时候如何统计记录条数 COUNT(*) DISTINCT
Apr 23 PHP
php Hex RGB颜色值互换的使用
May 10 PHP
PHP获取毫秒级时间戳的方法
Apr 15 PHP
简单谈谈php中的unicode和utf8编码
Jun 10 PHP
CI分页类首页、尾页不显示的解决方法
Mar 28 PHP
php获取本机真实IP地址实例代码
Mar 31 PHP
浅谈PHP中的
Apr 23 PHP
PHP简单实现DES加密解密的方法
Jul 12 PHP
php使用scandir()函数扫描指定目录下所有文件示例
Jun 08 PHP
PHP+fiddler抓包采集微信文章阅读数点赞数的思路详解
Dec 20 PHP
PHP中类与对象功能、用法实例解读
Mar 27 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 file_get_contents函数轻松采集html数据
2010/04/22 PHP
php单例模式实现(对象只被创建一次)
2012/12/05 PHP
php绘图之在图片上写中文和英文的方法
2015/01/24 PHP
php实现随机显示图片方法汇总
2015/05/21 PHP
详谈phpAdmin修改密码后拒绝访问的问题
2017/04/03 PHP
innerText 使用示例
2014/01/23 Javascript
解决node-webkit 不支持html5播放mp4视频的方法
2015/03/11 Javascript
JavaScript数组去重的五种方法
2015/11/05 Javascript
JavaScript html5利用FileReader实现上传功能
2020/03/27 Javascript
BetterScroll 在移动端滚动场景的应用
2017/09/18 Javascript
JavaScript实现修改伪类样式
2017/11/27 Javascript
Angular自定义组件实现数据双向数据绑定的实例
2017/12/11 Javascript
vue 中 beforeRouteEnter 死循环的问题
2019/04/23 Javascript
简单了解微信小程序的目录结构
2019/07/01 Javascript
JavaScript闭包相关知识解析
2019/10/19 Javascript
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
2020/04/30 jQuery
vue 页面跳转的实现方式
2021/01/12 Vue.js
[04:29]【TI9采访】OG.N0tail在胜者组决赛后接受采访
2019/08/25 DOTA
Python Web框架Pylons中使用MongoDB的例子
2013/12/03 Python
python实现类似ftp传输文件的网络程序示例
2014/04/08 Python
Python简单删除列表中相同元素的方法示例
2017/06/12 Python
Python简单实现socket信息发送与监听功能示例
2018/01/03 Python
TensorFlow实现RNN循环神经网络
2018/02/28 Python
windows10下python3.5 pip3安装图文教程
2018/04/02 Python
Python网页正文转换语音文件的操作方法
2018/12/09 Python
python 函数中的参数类型
2020/02/11 Python
基于python实现查询ip地址来源
2020/06/02 Python
使用python求斐波那契数列中第n个数的值示例代码
2020/07/26 Python
HTML5自定义data-* data(obj)属性和jquery的data()方法的使用
2012/12/13 HTML / CSS
常用的HTML5列表标签
2017/06/20 HTML / CSS
美国婴儿用品店:Babies”R”Us
2017/10/12 全球购物
PHP面试题附答案
2015/11/28 面试题
高中生评语大全
2014/04/25 职场文书
软件项目实施计划书
2014/05/02 职场文书
运动会演讲稿100字
2014/08/25 职场文书
python-opencv 中值滤波{cv2.medianBlur(src, ksize)}的用法
2021/06/05 Python