James.Cridland.net

James Cridland's blog

Where radio and new platforms collide. With beer.

« Radio at the Edge podcast | Blog index | Wherefore art thou, Frazier Chorus? »

Google Charts API - using PHP

Posted on Thursday, December 6th, 2007 at 10:11pm. #

Google have just released the Google Chart API which enables you to produce swizzy graphs like the above, which is the total ‘hours-listened’ to BBC Radio 1 from March 1999 to September 2007.

In their API documentation, they publish some JavaScript code to help with encoding the URL.

I’ve quickly whipped up a near-equivalent in PHP, which you’ll find here.

I’ll not be using it in its current form, at least not replacing my ugly home-grown graphs: the API doesn’t add labels above the data, which I currently use - but it’s a really handy bit of code from Google, and I might use it elsewhere. Thanks, chaps.

11 comments

Barry Hunter said at December 6th, 2007 at 11:56pm

That’s great, thanks!

However would simply the use of the max() function be a better method rather than sorting to get the highest value, as that can take an array directly.

James Cridland said at December 7th, 2007 at 12:16am

Agreed, Barry - I’ve changed the code accordingly. (Didn’t know about max().)

Chris Stevens said at December 7th, 2007 at 2:08am

Off topic… Is it me, or do you have a version of this blog optimised for the iPhone/ipod? It looked very neat on mine over the weekend!

Jose said at December 7th, 2007 at 2:37am

Hello

Thanks for the code!

What I don’t like about Google chart API is the fact axis don’t have nothing to do with values.

Thus, in order for a simple x,y graph to make sense, you have to find the percentage of all your values and then plot them. I mean, if your max value is 150, you must make 150=100% and then calculate all values below it in terms of percentage. Your laber on the x axe will be 150, but your value will be 100%.

I wonder who will be the first to do a PHP class to do this… ;)

Regards,
Jose

Also: it does not work on forums with the [IMG] tag…
It would be usefull for the PHP class to load the url as binary and outputs it as .png, so that it can be recognized everywhere.

Lexalytics Development Blog » Blog Archive » Google Charts API said at December 7th, 2007 at 11:57am

[...] via the BBC Backstage mailing list (and James Cridlands personal blog) that Google have released a simple charting [...]

Olly said at December 8th, 2007 at 12:27am

Have you seen Swivel?

I had a very basic look/play with it, don’t know whether it can help but looks quite good. http://www.swivel.com/

Olly

William T said at December 11th, 2007 at 8:05pm

The charts are *very* elegant (especially the pie charts and venn diagrams) - however, maybe its just me, how do we have confidence that Google won’t withdraw it or start to charge for it or it’ll suddenly get very slow or go away altogether?

Yes, everyone’s using Google Maps these days, but I just wonder how many “large” or “critical” sites would be prepared to risk this (there’s a 50,000 queries per day limit.)

Sorry to be boring.

William T said at December 11th, 2007 at 8:42pm

(or, I meant to add, might they slap on a large Copyright Google notice or start running inline text ads based on the chart-data?)

The charts *are* very pretty though..

Kev said at January 11th, 2008 at 4:51pm

Hi James

Nice work!

Can I just ask - I’ve used your code in a dynamcially generated HTML table (now with chart!) but I’m having difficulties loading my string into the array of your code i.e.

$test_data=array(0.5,6,12,17,2,0.3,9);

becomes

$string1 =”0.5,6,12,17,2,0.3,9″
$test_data=array($string1);

My string is being created at the same time as the table and echoing the data gives a result of 0.5,6,12,17,2,0.3,9 and the max value is also 0.5,6,12,17,2,0.3,9 but no data??

Any suggestions?

Thanks

Kev

Aneesh Kulkarni said at January 12th, 2008 at 2:19pm

Thanks, this is great! Works perfectly.

One small note: The last line (return $chartData.”&chxt=y&chxl=0:|0|”.$maxValue; — which puts a scale on the axis) assumes a vertical orientation. Anyone interested in a graph with horizontal orientation (ie bhs or bhg) should write chxt=x instead of chxt=y to get the desired graph.

Toby said at January 20th, 2008 at 12:44pm

Thanks for this James - being close to newbie status, this just what I was looking for. Cheers!

Leave a comment

This website's Gravatar enabled (that's the pictures on the right)

To prove you're human, type the two words below into the box provided.