Unsupported scripts and control panel web app for a hosting company
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pie.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?PHP
  2. define('PIE_MAX_SEGS',6); // Maximum number of segments
  3. function pieSort ($item1, $item2) {
  4. return ($item2 - $item1);
  5. }
  6. function doPie ($title, $items) {
  7. assert(is_array($items));
  8. uasort($items,'pieSort');
  9. if (count($items) > PIE_MAX_SEGS) {
  10. $other = 0;
  11. $i = PIE_MAX_SEGS - 1;
  12. foreach ($items as $key => $value) {
  13. $i--;
  14. if ($i < 0) {
  15. $other += $value;
  16. unset($items[$key]);
  17. }
  18. }
  19. $items['Other'] = $other;
  20. }
  21. $im = imagecreate(500,315);
  22. $white = imagecolorallocate($im,255,255,255);
  23. imagefill($im,0,0,$white);
  24. $total = 0;
  25. foreach ($items as $name => $value) {
  26. $total += $value;
  27. }
  28. $colours = array('138-103-173', '000-000-128', '030-144-255', '000-100-000',
  29. '060-179-113', '173-173-047', '189-183-107', '255-215-000',
  30. '205-092-092', '205-133-063', '255-000-000', '255-069-000',
  31. '255-105-180', '148-000-211', '147-112-219', '112-138-144',
  32. '100-100-100');
  33. $black = imagecolorallocate($im,0,0,0);
  34. if ($total != 0) { $dpv = 360/$total; } else { $dpv = 360; }
  35. $last = 0;
  36. $keypos = 0;
  37. foreach ($items as $name => $value) {
  38. $colour = explode('-',array_shift($colours));
  39. $colour = imagecolorallocate($im, $colour[0], $colour[1], $colour[2]);
  40. $cur = $dpv * $value;
  41. imagefilledarc($im, 250, 160, 175, 175, $last, $last+$cur, $colour, IMG_ARC_PIE);
  42. imagefilledarc($im, 250, 160, 175, 175, $last, $last+$cur, $black, IMG_ARC_EDGED | IMG_ARC_NOFILL);
  43. $mid = ($last + $last + $cur)/2;
  44. $multX = $multY = 1;
  45. if ($mid >= 180) { $multY = -1; }
  46. if ($mid >= 90 && $mid < 270) { $multX = -1; }
  47. $deltaX = abs(cos(deg2rad($mid)))*(175/3.5)*$multX;
  48. $deltaY = abs(sin(deg2rad($mid)))*(175/3.5)*$multY;
  49. $posX = 250 + $deltaX; $posY = 160 + $deltaY;
  50. $endX = 250 + ($deltaX * 2.3); $endY = 160 + ($deltaY * 2.3);
  51. if (($value/$total)*100 > 2.3) {
  52. imageline($im, $posX, $posY, $endX, $endY, $black);
  53. imageline($im, $endX, $endY, $endX + $multX * (imagefontwidth(2)*strlen($name)+5), $endY, $black);
  54. if ($multX == 1) {
  55. imagestring($im, 2, $endX + 2, $endY - 12, $name, $black);
  56. imagestring($im, 1, $endX + 2, $endY + 2, '['.round($value*100/$total,1).'%]',$black);
  57. } else {
  58. imagestring($im, 2, $endX - 2 - imagefontwidth(2)*strlen($name), $endY - 12, $name, $black);
  59. imagestring($im, 1, $endX - (imagefontwidth(1)*strlen('['.round($value*100/$total,1).'%]')) , $endY + 2, '['.round($value*100/$total,1).'%]',$black);
  60. }
  61. } else {
  62. if ($keypos == 0) {
  63. imagestring($im,1,5,303,'Key:',$black);
  64. $keypos += imagefontwidth(1)*4 + 10;
  65. }
  66. imagestring($im,1,$keypos,303,$name.' ['.round($value*100/$total,1).'%]',$colour);
  67. $keypos += imagefontwidth(1)*strlen($name.' ['.round($value*100/$total,1).'%]') + 10;
  68. }
  69. $last += $cur;
  70. }
  71. imagestring($im, 3, 250 - imagefontwidth(3)*strlen($title)/2, 5, $title, $black);
  72. return $im;
  73. }
  74. ?>