欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 饼图统计代码用法

程序员文章站 2024-02-08 18:41:28
...
  1. define("ANGLE_STEP", 5); //定义画椭圆弧时的角度步长
  2. function draw_getdarkcolor($img,$clr) //求$clr对应的暗色
  3. {
  4. $rgb = imagecolorsforindex($img,$clr);
  5. return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
  6. }
  7. function draw_getexy($a, $b, $d) //求角度$d对应的椭圆上的点坐标
  8. {
  9. $d = deg2rad($d);
  10. return array(round($a*Cos($d)), round($b*Sin($d)));
  11. }
  12. function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr) //椭圆弧函数
  13. {
  14. $n = ceil(($ed-$sd)/ANGLE_STEP);
  15. $d = $sd;
  16. list($x0,$y0) = draw_getexy($a,$b,$d);
  17. for($i=0; $i {
  18. $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
  19. list($x, $y) = draw_getexy($a, $b, $d);
  20. imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
  21. $x0 = $x;
  22. $y0 = $y;
  23. }
  24. }
  25. function draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) //画扇面
  26. {
  27. $n = ceil(($ed-$sd)/ANGLE_STEP);
  28. $d = $sd;
  29. list($x0,$y0) = draw_getexy($a, $b, $d);
  30. imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
  31. for($i=0; $i {
  32. $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
  33. list($x, $y) = draw_getexy($a, $b, $d);
  34. imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
  35. $x0 = $x;
  36. $y0 = $y;
  37. }
  38. imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
  39. list($x, $y) = draw_getexy($a/2, $b/2, ($d+$sd)/2);
  40. imagefill($img, $x+$ox, $y+$oy, $clr);
  41. }
  42. function draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) //3d扇面
  43. {
  44. draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);
  45. if($sd {
  46. list($R, $G, $B) = draw_getdarkcolor($img, $clr);
  47. $clr=imagecolorallocate($img, $R, $G, $B);
  48. if($ed>180) $ed = 180;
  49. list($sx, $sy) = draw_getexy($a,$b,$sd);
  50. $sx += $ox;
  51. $sy += $oy;
  52. list($ex, $ey) = draw_getexy($a, $b, $ed);
  53. $ex += $ox;
  54. $ey += $oy;
  55. imageline($img, $sx, $sy, $sx, $sy+$v, $clr);
  56. imageline($img, $ex, $ey, $ex, $ey+$v, $clr);
  57. draw_arc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr);
  58. list($sx, $sy) = draw_getexy($a, $b, ($sd+$ed)/2);
  59. $sy += $oy+$v/2;
  60. $sx += $ox;
  61. imagefill($img, $sx, $sy, $clr);
  62. }
  63. }
  64. function draw_getindexcolor($img, $clr) //RBG转索引色
  65. {
  66. $R = ($clr>>16) & 0xff;
  67. $G = ($clr>>8)& 0xff;
  68. $B = ($clr) & 0xff;
  69. return imagecolorallocate($img, $R, $G, $B);
  70. }
  71. // 绘图主函数,并输出图片
  72. // $datLst 为数据数组, $datLst 为标签数组, $datLst 为颜色数组
  73. // 以上三个数组的维数应该相等
  74. function draw_img($datLst,$labLst,$clrLst,$a=250,$b=120,$v=20,$font=10)
  75. {
  76. $ox = 5+$a;
  77. $oy = 5+$b;
  78. $fw = imagefontwidth($font);
  79. $fh = imagefontheight($font);
  80. $n = count($datLst);//数据项个数
  81. $w = 10+$a*2;
  82. $h = 10+$b*2+$v+($fh+2)*$n;
  83. $img = imagecreate($w, $h);
  84. //转RGB为索引色
  85. for($i=0; $i $clrLst[$i] = draw_getindexcolor($img,$clrLst[$i]);
  86. $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);
  87. $clrt = imagecolorallocate($img, 0x00, 0x00, 0x00);
  88. //填充背景色
  89. imagefill($img, 0, 0, $clrbk);
  90. //求和
  91. $tot = 0;
  92. for($i=0; $i $tot += $datLst[$i];
  93. $sd = 0;
  94. $ed = 0;
  95. $ly = 10+$b*2+$v;
  96. for($i=0; $i {
  97. $sd = $ed;
  98. $ed += $datLst[$i]/$tot*360;
  99. //画圆饼
  100. draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clrLst[$i]); //$sd,$ed,$clrLst[$i]);
  101. //画标签
  102. imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrLst[$i]);
  103. imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt);
  104. //imagestring($img, $font, 5+2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)", $clrt);
  105. $str = iconv("GB2312", "UTF-8", $labLst[$i]);
  106. ImageTTFText($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "./simsun.ttf", $str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)");
  107. $ly += $fh+2;
  108. }
  109. //输出图形
  110. header("Content-type: image/png");
  111. //输出生成的图片
  112. $imgFileName = "temp/".time().".png";
  113. imagepng($img,$imgFileName);
  114. echo 'php 饼图统计代码用法';
  115. }
  116. ?>
复制代码

2、调用方法:

  1. require_once ("piefunction.php");
  2. $datLst = array(10,110,300); //数据
  3. $labLst = array("好评","中评","差评"); //标签
  4. $clrLst = array(0x99ff00, 0xff6666, 0x0099ff);
  5. draw_img($datLst,$labLst,$clrLst);
  6. ?>
复制代码

说明: 只要数据的个数与后面的标签,颜色个数一致就可以输出正确的饼状图效果了。