Volodymyr Osypov blog

May 16, 2008

Pie diagram/chart in PHP using GD

Filed under: PHP — Tags: , , , , — admin @ 10:23 am

Let's continue creating functions for diagrams building. Today we'll program a function which builds pie diagram using GD library for PHP. As in our example bar diagram our function will have array $ar ($ar['name']=data;) and $title for diagram title as input.

First we'll create a function in piegraph.php:

PHP:
  1. <?
  2.  
  3. function GraphPie($ar,$title) {
  4. // Sizes of our diagram
  5. $diagramWidth=600;
  6. $diagramHeight=370;
  7. $legendOffset=50;
  8.  
  9. // reverse sorting, saving the keys
  10. arsort($ar);
  11.  
  12. // Now we need to calculate percents of each item
  13. // Sum (you can also use array_sum() PHP function)
  14. $sum=0;
  15. foreach ($ar as $name => $val) {
  16.     $sum+=$val;
  17. }
  18.  
  19. //let's determine which items are less than 1 percent and which are greater
  20. $sumless1=0; // sum of all less than 1 percent items
  21. $countless1=$countgreater1=0; // count of less and more 1 percent items;
  22. foreach ($ar as $name => $val) {
  23.     if ($val/$sum<0.01)
  24.      { $sumless1+=$val;
  25.        $countless1++;
  26.        }
  27.        else $countgreater1++;
  28. }
  29.  
  30.  // create image
  31. $image = imageCreate($diagramWidth, $diagramHeight);
  32.  
  33. // allocate all required colors
  34. $colorBackgr = imageColorAllocate($image, 220,220,220);
  35. $colorText = imageColorAllocate($image, 0, 0, 0);
  36. $colors[0] = imagecolorallocate($image, 255, 0, 0);
  37. $colors[1] = imagecolorallocate($image, 0, 151, 0);
  38. $colors[2] = imagecolorallocate($image, 0, 0, 255);
  39. $colors[3] = imagecolorallocate($image, 255, 255, 0);
  40. $colors[4] = imagecolorallocate($image, 0, 255, 255);
  41. $colors[5] = imagecolorallocate($image, 204, 0, 206);
  42. $colors[6] = imagecolorallocate($image, 155, 105, 27);
  43. $colors[7] = imagecolorallocate($image, 134,188,134);
  44. $colors[8] = imagecolorallocate($image, 255,189,62);
  45. $colors[9] = imagecolorallocate($image, 168,168,255);
  46. $colors[10] = imagecolorallocate($image, 219,255,111);
  47. $colors[11] = imagecolorallocate($image, 255,201,200);
  48. $colors[12] = imagecolorallocate($image, 0,169,168);
  49. $colors[13] = imagecolorallocate($image, 248,255,213);
  50. $colors[14] = imagecolorallocate($image, 255,106,105);
  51. $colors[15] = imagecolorallocate($image, 133,133,133);
  52. $colors[16] = imagecolorallocate($image, 194,255,255);
  53. $colors[17] = imagecolorallocate($image, 90,9,255);
  54. $colors[18] = imagecolorallocate($image, 109,255,110);
  55. $colors[19] = imagecolorallocate($image, 255,133,22);
  56.  
  57.  
  58. // clear the image space with the background color
  59. imageFilledRectangle($image, 0, 0, $diagramWidth - 1, $diagramHeight - 1, $colorBackgr);
  60.  
  61. // start angle for sectors
  62. $startAngle=0;
  63. $perc=360/$sum; // determin how many grads in 1 percent
  64. $i=0; // for output in legend;
  65. foreach ($ar as $name => $val) {
  66. // if this item is greater than 1 percent
  67.   if ($val/$sum<0.01) // then break;
  68.     break;
  69.  
  70. // end angle for sector
  71.   $endAngle=$startAngle+$val*$perc;
  72. // how many percents has our item
  73.   $percents=round(100*($val/$sum),2);
  74.   // legend color rectangle
  75.   imagefilledrectangle($image,285,$legendOffset+$i*15-9,295,$legendOffset+$i*15,$colors[$i]);
  76.   // legend text
  77.   imagettftext ($image, 10, 0, 300, $legendOffset+$i*15, $colorText, "arialcyr.ttf", ($i+1).". ".$name." (".$percents." %)");
  78.   // pie sector
  79.   imagefilledarc($image, $diagramWidth/2-150, $diagramHeight/2, 200, 200, $startAngle, $endAngle, $colors[$i++], IMG_ARC_PIE);
  80.   // next sector will have as start angle the end angle of current one
  81.   $startAngle=$endAngle;
  82. }
  83.  
  84. // if there are items less than 1 percent - we output them all as one other pie sector
  85. if ($countless1) {
  86. // less than 1 percent
  87.   $endAngle=360;
  88.   $percents=round(100*($sumless1/$sum),2);
  89.   // legend rectangle
  90.   imagefilledrectangle($image,285,$legendOffset+$i*15-9,295,$legendOffset+$i*15,$colors[$i]);
  91.   // legend text
  92.   imagettftext ($image, 10, 0, 300, $legendOffset+$i*15, $colorText, "arialcyr.ttf", ($i+1).". "."Other"." (".$percents." %)");
  93.   // Sector for "Other"
  94.   imagefilledarc($image, $diagramWidth/2-150, $diagramHeight/2, 200, 200, $startAngle, $endAngle, $colors[$i++], IMG_ARC_PIE);
  95. }
  96.  
  97.  
  98.   // Title of the diagram
  99.  imagettftext ($image, 14, 0, 10, 20, $colorText, "arialcyr.ttf", $title);
  100.   // some copyright info
  101.  imagettftext ($image, 10, 0, 10, $diagramHeight-10, $colorText, "arialcyr.ttf", "www.aurorascorpio.com");
  102.  
  103. // outputing image
  104. header("Content-type:  image/png");
  105. imagepng($image);
  106. // create an interlaced image for better loading in the browser
  107. imageInterlace($image, 1);
  108. // mark background color as being transparent
  109. imageColorTransparent($image, $colorBackgr);
  110.  
  111. return;
  112. }
  113. ?>

Now let's test it. Create diagrampietest.php with datas about browser using:

PHP:
  1. <?
  2. include("piegraph.php"); // script with our GraphPie function
  3. // our array
  4. $gr_val=array("Firefox"=>80,"Opera"=>50,"IE"=>150);
  5. $gr_val["Netscape"]=2;
  6. $gr_val["Konqueror"]=1;
  7. $gr_val["Mozilla"]=3;
  8. $gr_val["Safari"]=20;
  9. // build the diagram
  10. GraphPie($gr_val,"Browsers");
  11. ?>

You will see the next diagram:

Browsers - Pie Diagram

DEMO

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress