php 扇形比例百分比显示程序代码

我们在很多网站会看到一些图形的百分比显示图,像三个地区所占地多少或者是成绩等一下我来给大家介绍一款用php生成的扇形比例百分比显示程序代码.

不过使用它首先得有phpGD库支持,代码如下:

  1. <?php
  2. //填充图表的参数
  3. $ChartDiameter = 60; //图表直径
  4. $ChartData = array(30,70);//用于生成图表的数据,可通过数据库来取得来确定也可以多个不过和颜色数组对应
  5. //把角度转换为弧度
  6. function radians($degrees){return($degrees*(pi()/180.0));}
  7. //取得在圆心为(0,0)圆上 x,y点的值
  8. function circle_point($degrees,$diameter){$x=cos(radians($degrees))*($diameter/2);$y=sin(radians($degrees))*($diameter/2);return (array($x,$y));}
  9. //确定图形的大小
  10. $ChartWidth = $ChartDiameter + 20;
  11. $ChartHeight = $ChartDiameter + 20;
  12. //确定统计的总数
  13. $ChartTotal = “”;
  14. for($index = 0;$index < count($ChartData);$index++){
  15. $ChartTotal += $ChartData[$index];
  16. }
  17. $ChartCenterX = $ChartDiameter/2 + 10;
  18. $ChartCenterY = $ChartDiameter/2 + 10;
  19. //生成空白图形
  20. $image = imagecreate($ChartWidth, $ChartHeight);
  21. //分配颜色
  22. $colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  23. $colorBorder = imagecolorallocate($image, 0×00, 0×00, 0×00);
  24. $colorText = imagecolorallocate($image, 0×00, 0×00, 0×00);
  25. $colorSlice[] = imagecolorallocate($image, 0xFF, 0×00, 0×00);//这里是和你上面写的数组对应的颜色
  26. $colorSlice[] = imagecolorallocate($image, 0×00, 0xFF, 0×00);
  27. //填充背境
  28. imagefill($image, 0, 0, $colorBody);
  29. //画每一个扇形
  30. $Degrees = 0;
  31. for($index = 0; $index < count($ChartData); $index++){
  32. $StartDegrees = round($Degrees);
  33. $Degrees += (($ChartData[$index]/$ChartTotal)*360);
  34. $EndDegrees = round($Degrees);
  35. $CurrentColor = $colorSlice[$index%(count($colorSlice))];
  36. //画图F
  37. imagearc($image,$ChartCenterX,$ChartCenterY,$ChartDiameter,$ChartDiameter,$StartDegrees,$EndDegrees, $CurrentColor);
  38. //画直线
  39. list($ArcX, $ArcY) = circle_point($StartDegrees, $ChartDiameter);
  40. imageline($image,$ChartCenterX,$ChartCenterY,floor($ChartCenterX + $ArcX),
  41. floor($ChartCenterY + $ArcY),$CurrentColor);
  42. //画直线
  43. list($ArcX, $ArcY) = circle_point($EndDegrees, $ChartDiameter);
  44. imageline($image,$ChartCenterX,$ChartCenterY,ceil($ChartCenterX + $ArcX),
  45. ceil($ChartCenterY + $ArcY),$CurrentColor);
  46. //填充扇形
  47. $MidPoint = round((($EndDegrees – $StartDegrees)/2) + $StartDegrees);
  48. list($ArcX, $ArcY) = circle_point($MidPoint, $ChartDiameter/2);
  49. imagefilltoborder($image,floor($ChartCenterX + $ArcX),floor($ChartCenterY + $ArcY),
  50. $CurrentColor,$CurrentColor);
  51. }//开源代码phpfensi.com
  52. //到此脚本 已经生了一幅图像的,现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个GIF文件。不然的话你只能看到一堆奇怪的乱码
  53. header(“Content-type: image/png”);
  54. imagegif($image);
  55. ?>