打造超酷的PHP数据饼图

打造超酷的PHP数据饼图

  1. 源代码:
  2. //+------------------------+
  3. //| pie3dfun.PHP//公用函数 |
  4. //+------------------------+
  5. define("ANGLE_STEP", 5); //定义画椭圆弧时的角度步长
  6. function draw_getdarkcolor($img,$clr) //求$clr对应的暗色
  7. {
  8. $rgb = imagecolorsforindex($img,$clr);
  9. return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
  10. }
  11. function draw_getexy($a, $b, $d) //求角度$d对应的椭圆上的点坐标
  12. {
  13. $d = deg2rad($d);
  14. return array(round($a*Cos($d)), round($b*Sin($d)));
  15. }
  16. function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr) //椭圆弧函数
  17. {
  18. $n = ceil(($ed-$sd)/ANGLE_STEP);
  19. $d = $sd;
  20. list($x0,$y0) = draw_getexy($a,$b,$d);
  21. for($i=0; $i<$n; $i++)
  22. {
  23. $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
  24. list($x, $y) = draw_getexy($a, $b, $d);
  25. imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
  26. $x0 = $x;
  27. $y0 = $y;
  28. }
  29. }
  30. function draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) //画扇面
  31. {
  32. $n = ceil(($ed-$sd)/ANGLE_STEP);
  33. $d = $sd;
  34. list($x0,$y0) = draw_getexy($a, $b, $d);
  35. imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
  36. for($i=0; $i<$n; $i++)
  37. {
  38. $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
  39. list($x, $y) = draw_getexy($a, $b, $d);
  40. imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
  41. $x0 = $x;
  42. $y0 = $y;
  43. }
  44. imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
  45. list($x, $y) = draw_getexy($a/2, $b/2, ($d+$sd)/2);
  46. imagefill($img, $x+$ox, $y+$oy, $clr);
  47. }