Charts with PHP and Google Charts API

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:

Chart example

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.

Supported attributes:

  • Type
    What kind of chart to create
  • Title
    Chart title
  • Data
    The data to be used in the chart in an array
  • Size
    The size of the chart (Width x Height)
  • Color
    Colors to be used in the chart as array
  • Fill
    Fill colors to be used between timelines as array
  • Background
    Background color of the chart, defaults to #ffffff
  • LabelsXY
    Show or hide X and Y axis labels (true/false) (false by default)
  • Legend
    Show or hide a Legend (true/false) (true by default)

Supported types:

  • Pie
    Standard pie chart
  • Line
    Standard Line chart
  • Sparkline
    Almost identical to Line, except defaults to no axis lines.
  • Bar-horizontal
    Horizontal bar chart
  • Bar-vertical
    Vertical bar chart

Download

  • GoogChart
    Includes class as well as a short example file.

Disclaimer

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.

Special thanks

I’d like to thank my friend Fredrik Holmström for helping me enhance my code.

Enjoy!

17 comments (now closed)

  1. 's gravatar 1st April 2008

    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 :)

  2. 's gravatar 13th April 2008

    Robin

    Very nice class indeed :)

  3. 's gravatar 14th April 2008

    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.

  4. 's gravatar 15th April 2008

    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.

  5. 's gravatar 15th April 2008

    Benn Glazier

    Good work.

  6. 's gravatar 15th April 2008

    Michael Baggett

    Very nice.

  7. 's gravatar 15th April 2008

    Ludwig Pettersson

    Thanks everyone! Peter: I’ll look into it, seems like a great solution.

  8. 's gravatar 16th April 2008

    Sudar

    Thank you very much for this class.

  9. 's gravatar 16th April 2008

    Phill

    Here is another I have used

    http://code.google.com/p/gchartphp/

  10. 's gravatar 18th April 2008

    DottorZed

    Good Work!! I like this solution for my pages!

    thanks!

  11. 's gravatar 18th April 2008

    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.

  12. 's gravatar 18th April 2008

    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.

  13. 's gravatar 19th April 2008

    riper

    thanks for this class !

    A question : how can i use value instead of percentage ?

  14. 's gravatar 28th April 2008

    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 :)

  15. 's gravatar 1st June 2008

    NiKo

    This is cool, why don’t you host it at google code ? It would be awesome to have a svn repository :)

  16. 's gravatar 19th June 2008

    deni ramdan

    Nice tutor, thanx, i hope this usefull

  17. 's gravatar 23rd June 2008

    Clive

    There is a decent GUI at http://www.jonwinstanley.com/charts