Home  >  Article  >  Backend Development  >  PHP 生成环形图程序解决办法

PHP 生成环形图程序解决办法

WBOY
WBOYOriginal
2016-06-13 12:12:421209browse

PHP 生成环形图程序
各位大神 有没有php 环形图生成程序,  就像这样子的 
------解决思路----------------------
你是要产生图片还是要在网页上显示
如果只是为了在网页上显示,可以使用 jqPlot、Highcharts 这类 js 图表插件来做
如果是要生成图片,可以考虑用 jpGraph。不知是否有支持你需要的样式
自己写的话也不难,就是一段段环缺计算量比较大
------解决思路----------------------
php运行在服务端只是提供数据,图标显示需要用到js,如上斑竹所说的库.
------解决思路----------------------
你说的是PHP程序画圆吧!我也做过跟你的差不多!给你个参考.....程序仅供参考!
//填充图表的参数
$ChartDiameter = 140; //图表直径
$ChartData = array(20,80);//用于生成图表的数据,可通过数据库来取得来确定也可以多个不过和颜色数组对应
$msg = '56%';

//把角度转换为弧度
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 + 22;
$ChartHeight = $ChartDiameter + 56;
//确定统计的总数
$ChartTotal = '';
for($index = 0;$index  $ChartTotal += $ChartData[$index];
}
$ChartCenterX = $ChartDiameter/2 + 11;
$ChartCenterY = $ChartDiameter/2 + 28;
//生成空白图形
$image = imagecreatetruecolor($ChartWidth, $ChartHeight);
//分配颜色
$colorBody=imagecolorallocate($image,255,255,255);//白色
$colorBorder=imagecolorallocate($image,0,0,0);
$colorText=imagecolorallocate($image, 0, 0, 0);
$colorSlice[] = imagecolorallocate($image, 255,255,255);//这里是和你上面写的数组对应的颜色
$colorSlice[] = imagecolorallocate($image, 241,120,129);//数据完成显示的背景色
//$colorSlice[] = imagecolorallocate($image,0,0,0);//数据完成显示的背景色

//填充背境
imagefill($image, 0, 0, $colorBody);
//画每一个扇形
$Degrees = 0;
for($index = 0; $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);
//填充扇形
$tempnum = $EndDegrees - $startDegrees;
$MidPoint = round(($tempnum/2)+$startDegrees); 
list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
imagefilltoborder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),
$CurrentColor,$CurrentColor);
}
$red = imagecolorallocate($image,255,0,0);//分配红色
$white = imagecolorallocate($image,255,255,255);//分配白色
imagefilledellipse($image,81,98,120,120,$red);
$font = 'simhei.ttf';
imagettftext($image, 20,0, 63, 108, $white, $font,$msg);

//到此产生了一幅图像,把它发到浏览器上,重要是要将标头发给浏览器,让它知道是一个GIF文件。
header("Content-type: image/png");
imagegif($image);
imagedestroy($image);
?>
------解决思路----------------------
补充一下有个simhei.ttf是字体文件需要下载,可以问我要!
------解决思路----------------------
还有就是你说的这个图,控制画图的半径,控制每个扇形的起始点,这就需要你去想想算法了!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn