Sunday, 13th April 2008
Just yesterday I found out about the Google Charts API and I was impressed by this excellent service but noticed that creating multiple charts got pretty messy, especially if I wanted to edit the data later on.
I figured if I wanted to use this in the future, I’d like an easier way to create charts out of my data - thus the class GoogChart was born.
What I wanted to do was to simplify the process of calling the API, say I’ve got my data in an array called $data:
<?php
$data = array(
'IE7' => 22,
'IE6' => 30.7,
'IE5' => 1.7,
'Firefox' => 36.5,
'Mozilla' => 1.1,
'Safari' => 2,
'Opera' => 1.4,
);
So I’ve got my data, I want it to be presented as a pie chart and be a specific size as well - Rather than to manually edit this into what the API would accept, as well as adding the labels for each of part of the data - It’s with GoogChart it’s as easy as:
<?php
$chart = new googChart();
$chart->setChartAttrs( array(
'type' => 'pie',
'data' => $data,
'size' => array( 300, 200 )
));
echo $chart;
That tiny bit of code generates a Pie chart with all of my data neatly labeled and presented exactly as it should:
But that’s not all it can do, want to add a title to the chart? just add 'title' => 'Browser market 2008' to $chart->setChartAttrs.
GoogChart also supports multiple data types, just add each data to its own array within $data:
<?php
$data = array(
'February 2008' => array(
'IE7' => 22,
'IE6' => 30.7,
'IE5' => 1.7,
'Firefox' => 36.5,
'Mozilla' => 1.1,
'Safari' => 2,
'Opera' => 1.4,
),
'January 2008' => array(
'IE7' => 22,
'IE6' => 30.7,
'IE5' => 1.7,
'Firefox' => 36.5,
'Mozilla' => 1.1,
'Safari' => 2,
'Opera' => 1.4,
),
);
The names of each data set are automatically added as Legends.
array(Width x Height)arrayarray#ffffff(true/false) (false by default)(true/false) (true by default)GoogChart covers by no means the whole Google Charts API - only the basics for now.
GoogChart is distributed under the MIT license, so feel free to change the class any way you want.
I’d like to thank my friend Fredrik Holmström for helping me enhance my code.
Enjoy!
Peter Haza
I already created one as I use it as a submodule in a project, but if you create a new repository I’d love if that could be master branch instead of mine. This is your baby :)
Robin
Very nice class indeed :)
Greg Hoke
I will use this to display student responses to tutorial questions in the classroom! I hope it will help my students pay attention to see their answers anonymously display in a real time Pie chart.
Peter Haza
May I suggest you put this up on github.com or similar? I plan to use this in my current project, but I need to extend som bits of it, and then it would be nice to have somewhere to pull and push changes instead of having two incomplete forks.
Benn Glazier
Good work.
Michael Baggett
Very nice.
Ludwig Pettersson
Thanks everyone! Peter: I’ll look into it, seems like a great solution.
Sudar
Thank you very much for this class.
Phill
Here is another I have used
http://code.google.com/p/gchartphp/
DottorZed
Good Work!! I like this solution for my pages!
thanks!
Peter Haza
I decided to start on my own implementation, which is scattered into several files, but also allows for better control imo.
I also plan to support the whole api eventually, even though that will probably mean quite a few refactorings.
I’m not challenging your implementation though, as they serve different purposes imo. While your is very light and easy to use (1 file), mine is more a library/framework which requires reading docs to use.
Ludwig Pettersson
Sounds good Peter, the reason why I didn’t support the whole API is because it would defeat my whole idea of the class - to make it really easy to use for smaller things.
I just didn’t find a good way to support everything but still keep it the way I wanted.
riper
thanks for this class !
A question : how can i use value instead of percentage ?
Brant Tedeschi
I haven’t used it yet, but I have a feeling sometime in the very near future I will be needing this. Bookmarked and thanks for the hard work :)
NiKo
This is cool, why don’t you host it at google code ? It would be awesome to have a svn repository :)
deni ramdan
Nice tutor, thanx, i hope this usefull
Clive
There is a decent GUI at http://www.jonwinstanley.com/charts