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 相关文章推荐
PHP 文件上传进度条的两种实现方法的代码
Nov 25 PHP
php 购物车实例(申精)
May 11 PHP
php通用防注入程序 推荐
Feb 26 PHP
php递归函数中使用return的注意事项
Jan 17 PHP
神盾加密解密教程(二)PHP 神盾解密
Jun 08 PHP
PHP命名空间(Namespace)简明教程
Jun 11 PHP
Ubuntu下安装PHP的mongodb扩展操作命令
Jul 04 PHP
WampServer搭建php环境时遇到的问题汇总
Jul 23 PHP
2017年最新PHP经典面试题目汇总(上篇)
Mar 17 PHP
Laravel 中创建 Zip 压缩文件并提供下载的实现方法
Apr 02 PHP
Laravel 自动生成验证的实例讲解:login / logout
Oct 14 PHP
Laravel服务容器绑定的几种方法总结
Jun 14 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笔记之常用文件操作
2010/10/12 PHP
PHP统一页面编码避免乱码问题
2015/04/09 PHP
Yii2实现log输出到file及database的方法
2016/11/12 PHP
php基于session锁防止阻塞请求的方法分析
2017/08/07 PHP
php 中phar包的使用教程详解
2018/10/26 PHP
jquery实现背景墙聚光灯效果示例分享
2014/03/02 Javascript
如何让浏览器支持jquery ajax load 前进、后退功能
2014/06/12 Javascript
VUEJS实战之构建基础并渲染出列表(1)
2016/06/13 Javascript
js方法数据验证的简单实例
2016/09/17 Javascript
JS实现获取来自百度,Google,soso,sogou关键词的方法
2016/12/21 Javascript
jQuery的中 is(':visible') 解析及用法(必看)
2017/02/12 Javascript
支持移动端原生js轮播图
2017/02/16 Javascript
详解Vue学习笔记进阶篇之列表过渡及其他
2017/07/17 Javascript
在页面中引入js的两种方法(推荐)
2017/08/29 Javascript
angular.js4使用 RxJS 处理多个 Http 请求
2017/09/23 Javascript
微信小程序canvas实现刮刮乐效果
2018/07/09 Javascript
Vue.js 利用v-for中的index值实现隔行变色
2018/08/01 Javascript
python 字典(dict)按键和值排序
2016/06/28 Python
基于python中的TCP及UDP(详解)
2017/11/06 Python
Python调用C语言的方法【基于ctypes模块】
2018/01/22 Python
Python实现爬取马云的微博功能示例
2019/02/16 Python
Python字符串内置函数功能与用法总结
2019/04/16 Python
在Python中合并字典模块ChainMap的隐藏坑【推荐】
2019/06/27 Python
Python 简单计算要求形状面积的实例
2020/01/18 Python
PyTorch中的拷贝与就地操作详解
2020/12/09 Python
static关键字的用法
2013/10/07 面试题
新东网科技Java笔试题
2012/07/13 面试题
不用游标的SQL语句有哪些
2012/09/07 面试题
初任培训自我鉴定
2013/10/07 职场文书
红领巾心向党广播稿
2014/01/19 职场文书
市级文明单位申报材料
2014/05/07 职场文书
英文演讲稿
2014/05/15 职场文书
2014年前台个人工作总结
2014/11/14 职场文书
2016年国庆节新闻稿范文
2015/11/25 职场文书
实例讲解Python中sys.argv[]的用法
2021/06/03 Python
Golang日志包的使用
2022/04/20 Golang