澳门新葡萄京娱乐场php 扇形比例百分比显示程序代码

咱俩在很多网址拜会到局地图片的比例突显图,像三个地区所占地多少仍为成绩等,给大家介绍豆蔻梢头款用php生成的扇形比例百分比展现程序代码,然而使用它首先得有phpGD库援救。
复制代码 代码如下:
//填充图表的参数
$ChartDiameter = 60; //图表直径
$ChartData =
array(30,70State of Qatar;//用于生成图表的多少,可由此数据库来博取来鲜明也足以八个可是和颜料数组对应
//把角度调换为弧度
function radians($degrees){return($degrees*(pi()/180.0));}
//得到在圆心为(0,0)圆上 x,y点的值
function
circle_point($degrees,$diameter){$x=cos(radians($degrees))*($diameter/2);$y=sin(radians($degrees))*($diameter/2);return
(array($x,$y));}
//明确图形的深浅
$ChartWidth = $ChartDiameter + 20;
$ChartHeight = $ChartDiameter + 20;
//分明总计的总额
$ChartTotal = “”;
for($index = 0;$index < count($ChartData);$index++){
$ChartTotal += $ChartData[$index];
}
$ChartCenterX = $ChartDiameter/2 + 10;
$ChartCenterY = $ChartDiameter/2 + 10;
//生成空白图形
$image = imagecreate($ChartWidth, $ChartHeight);
//分配颜色
$colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorBorder = imagecolorallocate($image, 0×00, 0×00, 0×00);
$colorText = imagecolorallocate($image, 0×00, 0×00, 0×00);
$colorSlice[] = imagecolorallocate($image, 0xFF, 0×00,
0×00卡塔尔(قطر‎;//这里是和您上边写的数组对应的颜色
$colorSlice[] = imagecolorallocate($image, 0×00, 0xFF, 0×00);
//填充背境
imagefill($image, 0, 0, $colorBody);
//画每三个扇形
$Degrees = 0;
for($index = 0; $index < count($ChartData); $index++){
$StartDegrees = round($Degrees);
$Degrees += (($ChartData[$index]/$ChartTotal)*360);
$EndDegrees = round($Degrees);
$CurrentColor = $colorSlice[$index%(count($colorSlice))];
//画图F
imagearc($image,$ChartCenterX,$ChartCenterY,$ChartDiameter,$ChartDiameter,$StartDegrees,$EndDegrees,
$CurrentColor);
//画直线
list($ArcX, $ArcY) = circle_point($StartDegrees, $ChartDiameter);
imageline($image,$ChartCenterX,$ChartCenterY,floor($ChartCenterX +
$ArcX),
floor($ChartCenterY + $ArcY),$CurrentColor);
//画直线
list($ArcX, $ArcY) = circle_point($EndDegrees, $ChartDiameter);
imageline($image,$ChartCenterX,$ChartCenterY,ceil($ChartCenterX +
$ArcX),
ceil($ChartCenterY + $ArcY),$CurrentColor);
//填充扇形
$MidPoint = round((($EndDegrees – $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
imagefilltoborder($image,floor($ChartCenterX +
$ArcX),floor($ChartCenterY + $ArcY),
$CurrentColor,$CurrentColor);
}
//到此脚本
已经生了生机勃勃幅图像的,以往急需的是把它发到浏览器上,首要的一些是要将标头发给浏览器,让它精晓是二个GIF文件。不然的话你只好见到一批古怪的乱码
header(“Content-type: image/png”);
imagegif($image);
?>
 

然则使用它首先得有phpGD库帮衬。

效果图:源代码: [code] //+————————+ //|
pie3dfun.PHP//公用函数 | //+————————+ define;
//定义画椭圆弧时的角度步长 function draw_getdarkcolor
//求$clr对应的暗色 { $rgb = imagecolorsforindex; return
array($rgb[“red”]/2,$rgb[“green”]/2,$rgb[“blue”]/2); } function
draw_getexy //求角度$d对应的椭圆上的点坐标 { $d = deg2rad; return
array, round; } function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr卡塔尔//椭圆弧函数 { $n = ceil; $d = $sd; list = draw_getexy; for { $d =
>$ed?$ed:; list = draw_getexy; 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; $d = $sd; list = draw_getexy; imageline($img, $x0+$ox, $y0+$oy,
$ox, $oy, $clr); for { $d = >$ed?$ed:; list = draw_getexy;
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 =
draw_getexy/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 { list =
draw_getdarkcolor; $clr=imagecolorallocate; if $ed = 180; list =
draw_getexy; $sx += $ox; $sy += $oy; list = draw_getexy; $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 = draw_getexy; $sy += $oy+$v/2;
$sx += $ox; imagefill; } } function draw_getindexcolor //RBG转索引色 {
$Odyssey = & 0xff; $G = & 0xff; $B = & 0xff; return imagecolorallocate; } //
绘图主函数,并出口图片 // $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; $fh = imagefontheight; $n =
count;//数据项个数 $w = 10+$a*2; $h = 10+$b*2+$v+*$n; $img =
imagecreate; //转ENCOREGB为索引色 for $clrLst[$i] = draw_getindexcolor;
$clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff卡塔尔国; $clrt =
imagecolorallocate($img, 0x00, 0x00, 0x00卡塔尔; //填充背景观 imagefill;
//求和 $tot = 0; for $tot += $datLst[$i]; $sd = 0; $ed = 0; 333 $ly =
10+$b*2+$v; for { $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*/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*/100卡塔尔(قطر‎.”%卡塔尔国”State of Qatar; $ly += $fh+2; }
//输出图形 header(“Content-type: image/png”卡塔尔(قطر‎; //输出生成的图形
$imgFileName = “../temp/”.time(卡塔尔.”.png”; imagepng; echo
”””””””””””””””””””””””””””””””””””’
} $datLst = array(30, 10, 20, 20, 10, 20, 10, 20卡塔尔国; //数据 $labLst =
array(“中国防外贸学院”, “江西理理高校”, “清华”, “北大”,
“南大”, “上大”, “河海大学”, “中大”卡塔尔(قطر‎; //标签 $clrLst =
array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff,
0xff3333, 0x009999State of Qatar; //画图 draw_img($datLst,$labLst,$clrLst); ?>

 

 代码如下

<?php
//填充图表的参数
$ChartDiameter = 60; //图表直径
$ChartData =
array(30,70卡塔尔;//用于生成图表的数据,可透过数据库来获得来明确也能够八个可是和颜料数组对应
//把角度转变为弧度
function radians($degrees){return($degrees*(pi()/180.0));}
//获得在圆心为(0,0)圆上 x,y点的值
function
circle_point($degrees,$diameter){$x=cos(radians($degrees))*($diameter/2);$y=sin(radians($degrees))*($diameter/2);return
(array($x,$y));}
//明确图形的轻重
$ChartWidth = $ChartDiameter + 20;
$ChartHeight = $ChartDiameter + 20;
//明确计算的总的数量
$ChartTotal = “”;
for($index = 0;$index < count($ChartData);$index++){
$ChartTotal += $ChartData[$index];
}
$ChartCenterX = $ChartDiameter/2 + 10;
$ChartCenterY = $ChartDiameter/2 + 10;
//生成空白图形
$image = imagecreate($ChartWidth, $ChartHeight);
//分配颜色
$colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorBorder = imagecolorallocate($image, 0×00, 0×00, 0×00);
$colorText = imagecolorallocate($image, 0×00, 0×00, 0×00);
$colorSlice[] = imagecolorallocate($image, 0xFF, 0×00,
0×00卡塔尔;//这里是和你上面写的数组对应的颜色
$colorSlice[] = imagecolorallocate($image, 0×00, 0xFF, 0×00);
//填充背境
imagefill($image, 0, 0, $colorBody);
//画每一个扇形
$Degrees = 0;
for($index = 0; $index < count($ChartData); $index++){
$StartDegrees = round($Degrees);
$Degrees += (($ChartData[$index]/$ChartTotal)*360);
$EndDegrees = round($Degrees);
$CurrentColor = $colorSlice[$index%(count($colorSlice))];
//画图F
imagearc($image,$ChartCenterX,$ChartCenterY,$ChartDiameter,$ChartDiameter,$StartDegrees,$EndDegrees,
$CurrentColor);
//画直线
list($ArcX, $ArcY) = circle_point($StartDegrees, $ChartDiameter);
imageline($image,$ChartCenterX,$ChartCenterY,floor($ChartCenterX +
$ArcX),
floor($ChartCenterY + $ArcY),$CurrentColor);
//画直线
list($ArcX, $ArcY) = circle_point($EndDegrees, $ChartDiameter);
imageline($image,$ChartCenterX,$ChartCenterY,ceil($ChartCenterX +
$ArcX),
ceil($ChartCenterY + $ArcY),$CurrentColor);
//填充扇形
$MidPoint = round((($EndDegrees – $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
imagefilltoborder($image,floor($ChartCenterX +
$ArcX),floor($ChartCenterY + $ArcY),
$CurrentColor,$CurrentColor);
}
//到此脚本
已经生了风度翩翩幅图像的,未来急需的是把它发到浏览器上,主要的少数是要将标头发给浏览器,让它理解是二个GIF文件。不然的话你一定要见到一批奇异的乱码
header(“Content-type: image/png”);
imagegif($image);
?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注