php 生成饼图 三维饼图


Posted in PHP onSeptember 28, 2009

饼图

<?php 
//+------------------------+ 
//| pie3dfun.PHP//公用函数 | 
//+------------------------+ 
define("ANGLE_STEP", 3); //定义画椭圆弧时的角度步长 
define("FONT_USED", "C:\WINDOWS\Fonts\simhei.ttf"); // 使用到的字体文件位置 
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=200,$b=90,$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; 
$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, FONT_USED, $str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)"); 
$ly += $fh+2; 
} 
//输出图形 
header("Content-type: image/png"); 
//输出生成的图片 
imagepng($img); 
} 
$datLst = array(30, 20, 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 相关文章推荐
将数组写入txt文件 var_export
Apr 21 PHP
解析php二分法查找数组是否包含某一元素
May 23 PHP
php批量上传的实现代码
Jun 09 PHP
php 过滤英文标点符号及过滤中文标点符号代码
Jun 12 PHP
一个简洁实用的PHP缓存类完整实例
Jul 26 PHP
php中strstr、strrchr、substr、stristr四个函数的区别总结
Sep 22 PHP
php几个预定义变量$_SERVER用法小结
Nov 07 PHP
Dwz与thinkphp整合下的数据导出到Excel实例
Dec 04 PHP
php递归遍历多维数组的方法
Apr 18 PHP
详解 PHP加密解密字符串函数附源码下载
Dec 18 PHP
php 截取GBK文档某个位置开始的n个字符方法
Mar 08 PHP
PHP进阶学习之反射基本概念与用法分析
Jun 18 PHP
php 不同编码下的字符串长度区分
Sep 26 #PHP
php 应用程序安全防范技术研究
Sep 25 #PHP
从Web查询数据库之PHP与MySQL篇
Sep 25 #PHP
php 服务器调试 Zend Debugger 的安装教程
Sep 25 #PHP
php 代码优化的42条建议 推荐
Sep 25 #PHP
PHP下通过系统信号量加锁方式获取递增序列ID
Sep 25 #PHP
PHP 日常开发小技巧
Sep 23 #PHP
You might like
php 无限极分类
2008/03/27 PHP
php计算程序运行时间的简单例子分享
2014/05/10 PHP
Laravel学习教程之本地化模块
2017/08/18 PHP
使用PHPExcel导出Excel表
2018/09/08 PHP
PHP使用CURL实现下载文件功能示例
2019/06/03 PHP
彪哥1.1(智能表格)提供下载
2006/09/07 Javascript
jquery 可拖拽的窗体控件实现代码
2010/03/21 Javascript
jquery 元素相对定位代码
2010/10/15 Javascript
理解Javascript_09_Function与Object
2010/10/16 Javascript
javascript页面动态显示时间变化示例代码
2013/12/18 Javascript
js函数名与form表单元素同名冲突的问题
2014/03/07 Javascript
node.js中的fs.symlink方法使用说明
2014/12/15 Javascript
javascript RegExp 使用说明
2016/05/21 Javascript
Bootstrap基本插件学习笔记之折叠(22)
2016/12/08 Javascript
Bootstrap基本插件学习笔记之标签切换(17)
2016/12/08 Javascript
jquery实现提示语淡入效果
2017/05/05 jQuery
详解require.js配置路径的用法和css的引入
2017/09/06 Javascript
深入理解Vue2.x的虚拟DOM diff原理
2017/09/27 Javascript
Vue项目分环境打包的实现步骤
2018/04/02 Javascript
ES6 Object属性新的写法实例小结
2019/06/25 Javascript
Vue实现按钮级权限方案
2019/11/21 Javascript
原生js实现html手机端城市列表索引选择城市
2020/06/24 Javascript
Python的Django框架中从url中捕捉文本的方法
2015/07/20 Python
对python中if语句的真假判断实例详解
2019/02/18 Python
python 实现GUI(图形用户界面)编程详解
2019/07/17 Python
用Python抢火车票的简单小程序实现解析
2019/08/14 Python
python实现12306登录并保存cookie的方法示例
2019/12/17 Python
python3处理word文档实例分析
2020/12/01 Python
python实现图片转字符画的完整代码
2021/02/21 Python
实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码
2014/11/05 HTML / CSS
法国最大电子商务平台:Cdiscount
2018/03/13 全球购物
Linux操作面试题
2012/05/16 面试题
课例研修方案
2014/05/31 职场文书
对外汉语教师推荐信
2015/03/27 职场文书
2016教师学习党章心得体会
2016/01/15 职场文书
维护民族团结心得体会2016
2016/01/15 职场文书