Google Chart from Array


/ Published in: PHP
Save to your folder(s)

Used to make a Google Pie Chart, with optional labels and size from an array, using the array keys as the labels and the values for the data.


Copy this code and paste it in your HTML
  1. function googleChart($statsArray, $w = 300, $h = 175, $return = 'URL', $labels = true)
  2. {
  3. // Start the Google charts URL
  4. $gChartURL = "http://chart.apis.google.com/chart?cht=p&chd=t:";
  5.  
  6. // Append our values
  7. $gChartURL .= implode(',', $statsArray);
  8.  
  9. // Optionally return our labels
  10. if($labels)
  11. {
  12. $gChartURL .= '&&chdl=' . implode('|', array_keys($statsArray));
  13. }
  14.  
  15. // Set our chart size
  16. $gChartURL .= "&chs={$w}x{$h}";
  17.  
  18. if(strtoupper($return) == 'URL')
  19. {
  20. return $gChartURL;
  21. }
  22. elseif(strtoupper($return) == 'IMG')
  23. {
  24. return "<img src=\"{$gChartURL}\" width={$w} height={$h} />";
  25. }
  26. }

Report this snippet


Comments


You need to login to view comments.