Advertisement
carbonize

Pie Chart

Feb 19th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. ob_start();
  4.  
  5. // First we creat the image
  6. $pie = imagecreatetruecolor(130, 130);
  7.  
  8. // Now lets define some colours
  9. $col[0]  = imagecolorallocate($pie,255,0,0);     // red
  10. $col[1]  = imagecolorallocate($pie,0,128,0);     // green
  11. $col[2]  = imagecolorallocate($pie,255,255,0);   // yellow
  12. $col[3]  = imagecolorallocate($pie,0,0,128);     // navy
  13. $col[4]  = imagecolorallocate($pie,255,0,255);   // fushcia
  14. $col[5]  = imagecolorallocate($pie,0,128,128);   // teal
  15. $col[6]  = imagecolorallocate($pie,192,192,192); // silver
  16. $col[7]  = imagecolorallocate($pie,128,0,0);     // maroon
  17. $col[8]  = imagecolorallocate($pie,0,255,0);     // lime
  18. $col[9]  = imagecolorallocate($pie,128,128,0);   // olive
  19. $col[10] = imagecolorallocate($pie,0,0,255);     // blue
  20. $col[11] = imagecolorallocate($pie,128,0,128);   // purple
  21. $col[12] = imagecolorallocate($pie,0,255,255);   // aqua
  22. $col[13] = imagecolorallocate($pie,128,128,128); // gray
  23. $col[14] = imagecolorallocate($pie,255,255,255); // white used for the background which we then make transparent
  24. $col[15] = imagecolorallocate($pie,0,0,0);       // black
  25.  
  26. // Fill the image then make it transparent
  27. imagecolortransparent($pie, $col[14]);
  28. imagefill($pie,0,0,$col[14]);
  29.  
  30. $percentages = (!empty($_GET['pers'])) ? explode(',', $_GET['pers']) : '';
  31.  
  32. if(!is_array($percentages))
  33. {
  34.   imagestring($pie, 5, 50, 50, 'no percentages provided', $col[15]);
  35. }
  36. else
  37. {
  38.   // Set the start degree
  39.   $x = 0;
  40.  
  41.   // Now loop through our percentages
  42.   for ($z=0, $c=count($percentages); $z<$c; $z++)
  43.   {
  44.     $degrees = (($percentages[$z]/100)*360);
  45.     $color = imagecolorallocate($pie,mt_rand(20,255),mt_rand(20,255),mt_rand(20,255));
  46.     imagefilledarc($pie,65,65,125,125,$x,($x+$degrees),$col[$z],IMG_ARC_PIE);
  47.     $x+=$degrees;
  48.   }
  49. }
  50.  
  51. header("Content-type: image/gif");
  52. imagegif($pie);
  53.  
  54. echo trim(ob_get_clean());
  55.  
  56. imagedestroy($pie);
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement