Thursday, November 13, 2008

Google Chart API

It's no secret I'm a big fan of Google most developers are. I've been using the Google chart API a lot lately and thought it should get some exposure in my Blog. For those of you that follow my blog you really should be using this API. It's so darn simple and has no overhead because google takes the hit on processing!




You simply use a URL to get your chart so you're ultimately limited by the URL length and their encoding. While this wont be a full featured reporting system like Jasper or Pentaho putting quick graphs on a webpage or website is a piece of cake.


http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World





Is google's chart example and it's simple and elegant. You can use any type of code to generate your URL.


Google has a javascript code that can encode the URL with values to save space.


I wrote some PHP code (below) that you can use to encode the same type of array with PHP. This has the advantage of calling a function to encode it within your PHP page.


= 0){
			array_push($chartData, substr($simpleEncoding, round((strlen($simpleEncoding)-1)*$currentValue/$maxValue), 1));
		}else{
			array_push($chartData, '_');
		}
}
return join('', $chartData);
}
?>



You should use this sort of API an in any website where you dont want (or need) to have a full blown reporting engine. Stats and metrics can easily be charted with this. Additionally when you read the full API you'll see you can also change colors, control chart types and do all sorts of neat stuff with it.


These charts look great too. Since the cost is low (Free) and the use is simple this is a great way to splash up any webpage with some simple calls to a URL. Google allow you to do 250K of these requests a day which would be a lot for most websites. A little tip too this saves on your overall badwidth on your website too. 250K requests for a 4.8K chart image is about 1.2 gig in bandwidth. So google gives you that for free too. Unless you need some high customization there is no reason you shouldn't be using this everywhere.


Happy Charting!