亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

打造超酷的PHP數據餅圖

系統 2185 0

效果圖如下:

源代碼:


<?
//+------------------------+
//| 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 '<IMG SRC="'.$imgFileName.'" BORDER="1" ALT="統計餅圖">';
}

$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數據餅圖


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久99视频 | 狠狠干狠狠色 | 久久久久久网址 | 狠狠干影视 | 久久久久国产一级毛片高清板 | 99在线热视频 | 国内自拍青青草 | 日韩国产欧美成人一区二区影院 | 最近中文字幕在线视频1 | 久久国产这里只精品免费 | 日本精品久久久久中文字幕 1 | 操片免费| 久久综久久美利坚合众国 | 日本一区二区视频免费播放 | 久草手机在线 | 亚洲视频免费在线观看 | 男女交黄 | 欧美亚洲国产色综合 | 色综合久久中文字幕 | 夜夜做日日做夜夜爽 | 国内精品视频一区二区八戒 | 农村寡妇一级毛片免费播放 | 久草婷婷在线 | 精品国产日韩亚洲一区二区 | 成人毛片免费观看视频大全 | 老司机午夜在线视频免费观 | 99久久99久久免费精品蜜桃 | 国产羞羞视频在线播放 | 免费观看欧美一级特黄 | 日韩一区二区中文字幕 | 香蕉免费在线视频 | 成人免费观看网欧美片 | 国产一级αv片免费观看 | 91新网站| 久 在线播放 | 亚洲欧美大片 | 欧美性xxxxbbbb| 亚洲一级毛片视频 | 999精品视频在线观看 | 草久久免费视频 | 成人免费毛片观看 |