php生成扇形比例图实例
程序员文章站
2022-06-11 15:09:10
我们在很多网站会看到一些图形的百分比显示图,像三个地区所占地多少或者是成绩等,给大家介绍一款用php生成的扇形比例百分比显示程序代码,不过使用它首先得有phpgd库支持。复...
我们在很多网站会看到一些图形的百分比显示图,像三个地区所占地多少或者是成绩等,给大家介绍一款用php生成的扇形比例百分比显示程序代码,不过使用它首先得有phpgd库支持。
<?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);
?>
复制代码 代码如下:
<?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);
?>