php 三維餅圖的實(shí)現(xiàn)代碼
更新時(shí)間:2008年09月28日 14:36:47 作者:
一直想發(fā)表點(diǎn)東西。最近剛把php4中的php_gd.dll搞定,就迫不及待的想做點(diǎn)圖形程序玩玩??吹接性S多php做餅圖的例子,看了一下都是2維的,于是就想做個(gè)3維的。
經(jīng)過(guò)努力pie3d完成了,好東西與大家分享。不過(guò)小弟是php新手,代碼可能不夠精煉,希望大家指教共同來(lái)完善這個(gè)程序。記得通知我(estorm@yeah.net)
+------------------------+
| pie3dfun.php//公用函數(shù) |
+------------------------+
define("ANGLE_STEP",5);//定義畫(huà)橢圓弧時(shí)的角度步長(zhǎng)
function chx_getdarkcolor($img,$clr){//求$clr對(duì)應(yīng)的暗色
$rgb=imagecolorsforindex($img,$clr);
return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
}
function chx_getexy($a,$b,$d){//求角度$d對(duì)應(yīng)的橢圓上的點(diǎn)坐標(biāo)
$d=deg2rad($d);
return array(round($a*Cos($d)),round($b*Sin($d)));
}
function chx_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr){//橢圓弧函數(shù)
$n=ceil(($ed-$sd)/ANGLE_STEP);
$d=$sd;
list($x0,$y0)=chx_getexy($a,$b,$d);
for($i=0;$i<$n;$i++){
$d=($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
list($x,$y)=chx_getexy($a,$b,$d);
imageline($img,$x0+$ox,$y0+$oy,$x+$ox,$y+$oy,$clr);
$x0=$x;
$y0=$y;
}
}
function chx_sector($img,$ox,$oy,$a,$b,$sd,$ed,$clr){//畫(huà)扇面
$n=ceil(($ed-$sd)/ANGLE_STEP);
$d=$sd;
list($x0,$y0)=chx_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)=chx_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)=chx_getexy($a/2,$b/2,($d+$sd)/2);
imagefill($img,$x+$ox,$y+$oy,$clr);
}
function chx_sector3d($img,$ox,$oy,$a,$b,$v,$sd,$ed,$clr){//3d扇面
chx_sector($img,$ox,$oy,$a,$b,$sd,$ed,$clr);
if($sd<180){
list($R,$G,$B)=chx_getdarkcolor($img,$clr);
$clr=imagecolorallocate($img,$R,$G,$B);
if($ed>180) $ed=180;
list($sx,$sy)=chx_getexy($a,$b,$sd);
$sx+=$ox;
$sy+=$oy;
list($ex,$ey)=chx_getexy($a,$b,$ed);
$ex+=$ox;
$ey+=$oy;
imageline($img,$sx,$sy,$sx,$sy+$v,$clr);
imageline($img,$ex,$ey,$ex,$ey+$v,$clr);
chx_arc($img,$ox,$oy+$v,$a,$b,$sd,$ed,$clr);
list($sx,$sy)=chx_getexy($a,$b,($sd+$ed)/2);
$sy+=$oy+$v/2;
$sx+=$ox;
imagefill($img,$sx,$sy,$clr);
}
}
function chx_getindexcolor($img,$clr){//RBG轉(zhuǎn)索引色
$R=($clr>>16) & 0xff;
$G=($clr>>8)& 0xff;
$B=($clr) & 0xff;
return imagecolorallocate($img,$R,$G,$B);
}
?>
+--------------------------+
| pie3d.php //三維餅圖文件 |
+--------------------------+
require("pie3dfun.php");
$a=150;//橢圓長(zhǎng)半軸
$b=50;//橢圓段半軸
$v=20;//圓餅高度
$font=5;//字體
$ox=5+$a;
$oy=5+$b;
$fw=imagefontwidth($font);
$fh=imagefontheight($font);
$datLst=array(30,10,20,20,10,20,10,20);//數(shù)據(jù)
$labLst=array("a1","a2","a3","a4","a5","a6","a7","a8");//標(biāo)簽
$clrLst=array(0x99ff00,0xff6666,0x0099ff,0xff99ff,0xffff99,0x99ffff,0xff3333,0x009999);
$w=10+$a*2;
$h=10+$b*2+$v+($fh+2)*count($datLst);
$img=imagecreate($w,$h);
//轉(zhuǎn)RGB為索引色
for($i=0;$i
$clrbk=imagecolorallocate($img,0xff,0xff,0xff);
$clrt=imagecolorallocate($img,0x00,0x00,0x00);
//填充背景色
imagefill($img,0,0,$clrbk);
//求和
$tot=0;
for($i=0;$i
$sd=0;
$ed=0;
$ly=10+$b*2+$v;
for($i=0;$i $sd=$ed;
$ed+=$datLst[$i]/$tot*360;
//畫(huà)圓餅
chx_sector3d($img,$ox,$oy,$a,$b,$v,$sd,$ed,$clrLst[$i]);//$sd,$ed,$clrLst[$i]);
//畫(huà)標(biāo)簽
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);
$ly+=$fh+2;
}
//輸出圖形
header("Content-type:image/gif");
imagegif($img);
?>
+------------------------+
| pie3dfun.php//公用函數(shù) |
+------------------------+
define("ANGLE_STEP",5);//定義畫(huà)橢圓弧時(shí)的角度步長(zhǎng)
function chx_getdarkcolor($img,$clr){//求$clr對(duì)應(yīng)的暗色
$rgb=imagecolorsforindex($img,$clr);
return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
}
function chx_getexy($a,$b,$d){//求角度$d對(duì)應(yīng)的橢圓上的點(diǎn)坐標(biāo)
$d=deg2rad($d);
return array(round($a*Cos($d)),round($b*Sin($d)));
}
function chx_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr){//橢圓弧函數(shù)
$n=ceil(($ed-$sd)/ANGLE_STEP);
$d=$sd;
list($x0,$y0)=chx_getexy($a,$b,$d);
for($i=0;$i<$n;$i++){
$d=($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
list($x,$y)=chx_getexy($a,$b,$d);
imageline($img,$x0+$ox,$y0+$oy,$x+$ox,$y+$oy,$clr);
$x0=$x;
$y0=$y;
}
}
function chx_sector($img,$ox,$oy,$a,$b,$sd,$ed,$clr){//畫(huà)扇面
$n=ceil(($ed-$sd)/ANGLE_STEP);
$d=$sd;
list($x0,$y0)=chx_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)=chx_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)=chx_getexy($a/2,$b/2,($d+$sd)/2);
imagefill($img,$x+$ox,$y+$oy,$clr);
}
function chx_sector3d($img,$ox,$oy,$a,$b,$v,$sd,$ed,$clr){//3d扇面
chx_sector($img,$ox,$oy,$a,$b,$sd,$ed,$clr);
if($sd<180){
list($R,$G,$B)=chx_getdarkcolor($img,$clr);
$clr=imagecolorallocate($img,$R,$G,$B);
if($ed>180) $ed=180;
list($sx,$sy)=chx_getexy($a,$b,$sd);
$sx+=$ox;
$sy+=$oy;
list($ex,$ey)=chx_getexy($a,$b,$ed);
$ex+=$ox;
$ey+=$oy;
imageline($img,$sx,$sy,$sx,$sy+$v,$clr);
imageline($img,$ex,$ey,$ex,$ey+$v,$clr);
chx_arc($img,$ox,$oy+$v,$a,$b,$sd,$ed,$clr);
list($sx,$sy)=chx_getexy($a,$b,($sd+$ed)/2);
$sy+=$oy+$v/2;
$sx+=$ox;
imagefill($img,$sx,$sy,$clr);
}
}
function chx_getindexcolor($img,$clr){//RBG轉(zhuǎn)索引色
$R=($clr>>16) & 0xff;
$G=($clr>>8)& 0xff;
$B=($clr) & 0xff;
return imagecolorallocate($img,$R,$G,$B);
}
?>
+--------------------------+
| pie3d.php //三維餅圖文件 |
+--------------------------+
require("pie3dfun.php");
$a=150;//橢圓長(zhǎng)半軸
$b=50;//橢圓段半軸
$v=20;//圓餅高度
$font=5;//字體
$ox=5+$a;
$oy=5+$b;
$fw=imagefontwidth($font);
$fh=imagefontheight($font);
$datLst=array(30,10,20,20,10,20,10,20);//數(shù)據(jù)
$labLst=array("a1","a2","a3","a4","a5","a6","a7","a8");//標(biāo)簽
$clrLst=array(0x99ff00,0xff6666,0x0099ff,0xff99ff,0xffff99,0x99ffff,0xff3333,0x009999);
$w=10+$a*2;
$h=10+$b*2+$v+($fh+2)*count($datLst);
$img=imagecreate($w,$h);
//轉(zhuǎn)RGB為索引色
for($i=0;$i
$clrbk=imagecolorallocate($img,0xff,0xff,0xff);
$clrt=imagecolorallocate($img,0x00,0x00,0x00);
//填充背景色
imagefill($img,0,0,$clrbk);
//求和
$tot=0;
for($i=0;$i
$sd=0;
$ed=0;
$ly=10+$b*2+$v;
for($i=0;$i $sd=$ed;
$ed+=$datLst[$i]/$tot*360;
//畫(huà)圓餅
chx_sector3d($img,$ox,$oy,$a,$b,$v,$sd,$ed,$clrLst[$i]);//$sd,$ed,$clrLst[$i]);
//畫(huà)標(biāo)簽
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);
$ly+=$fh+2;
}
//輸出圖形
header("Content-type:image/gif");
imagegif($img);
?>
相關(guān)文章
php 參數(shù)過(guò)濾、數(shù)據(jù)過(guò)濾詳解
這篇文章給大家介紹php參數(shù)過(guò)濾及php數(shù)據(jù)過(guò)濾,包括php提交數(shù)據(jù)過(guò)濾的基本原則,php簡(jiǎn)單的數(shù)據(jù)過(guò)濾,感興趣的朋友一起學(xué)習(xí)吧2015-10-10yii2 modal彈窗之ActiveForm ajax表單異步驗(yàn)證
這篇文章主要介紹了yii2 modal彈窗之ActiveForm ajax表單驗(yàn)證的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06YII Framework框架教程之使用YIIC快速創(chuàng)建YII應(yīng)用詳解
這篇文章主要介紹了YII Framework框架教程之使用YIIC快速創(chuàng)建YII應(yīng)用的方法,詳細(xì)分析說(shuō)明了YII Framework框架使用YIIC命令行創(chuàng)建應(yīng)用的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-03-03php 判斷字符串編碼是utf-8 或gb2312實(shí)例
這篇文章主要介紹了php 判斷字符串編碼是utf-8 或gb2312實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-11-11