7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Web Development

Easy Graphs with Google Chart Tools

Scroll to top
Read Time: 15 mins

Google Chart Tools provide several ways to easily add charts to any web page. Charts may be static or interactive, and in this tutorial, we'll learn how to use both of them.

Static vs Interactive Charts

There are two different types of graphs that Chart Tools can generate: image charts (static graphs) and interactive charts.

  • Image Charts.- Use the Google Chart API.
  • Interactive Charts.- Use the Google Visualization API.

Image Charts are quite easy to use, however, interactive charts are far more flexible, because they can trigger events which we can use to interact with other elements in the page.

First, The Super-Easy Way

Yes, there is a super-easy way to include a chart in your page - as easy as writing a URL like this:

if you copy and paste this url in your browser, you'll see the following:

You can place the image anywhere in your page using the URL as the src attribute of an image tag:

That's the Google Charts API. Request are sent as GET or POST URLs, and the Google charts server returns a PNG image in response. The type of chart, data and options are all specified within the querystring of the URL. The API defines how to do that. Let's review the different options.


This is the base URL; we'll use it for all image chart requests. The rest are parameters in the form name=value separated by &.

Mandatory Parameters

There are only three mandatory parameters: cht, chs, and chd. The rest are optional.


This is the chart type. We are using a 3D pie chart which is p3. You can visit the chart gallery for all available chart types.


This is the chart size in pixels (width x height).


This is the data to display in the chart. The first letter (t) indicates the data format. In this case, we are using basic text format which is a list of comma separated values.

Optional Parameters

Each chart type has a few optional parameters to configure some aspects of your graph: title, labels, font types, colors, gradients, etc. This is what we have included:


Chart labels for each pie slice.


Chart title.


Chart color in rrggbb hexadecimal format.

If you specify one single color, the slices will have different gradations. You can also specify a gradient with two colors (chco=ff0000,00ff00) or a color for each slice (chco=ff0000|3355aa|8322c2|112233).

This is it for image charts. There isn't much to it! There are a lot of different chart types available, and, if you play with the parameters, you can get some really nice results. The Google Live Chart Playground is an excellent tool to do this. You play with parameters and see the changes in the generated image - an easy way to fine-tune the url for your graph!

Interactive Charts

To include interactive charts in your web pages, you have to use a different API: the Google Visualization API. In this case, the interface is not a URL. You'll have to use a JavaScript library, and write a few lines of code - but nothing difficult.

There is a gallery of ready-made visualizations (graphs) that you can use. You can also create and share your own graph, but the visualizations in the gallery will probably cover most of your needs for displaying data.

The level of interactivity depends on the particular visualizations you use. Usually, the graph will react in a certain way when clicked (showing a tool tip or animating), but the really powerful feature is that they can trigger events and you can register callbacks to perform any action related to that event. Examples of events can be selecting a bar or a pie slice, mouseOver, mouseOut, etc.

We'll use local data to feed the visualizations in our examples, but you can obtain your data in any other way. A common option would be to retrieve the data from a database using AJAX. You can even use the Visualization API; it also defines a way to request and offer (for servers) data in a format which can be immediatly used in any visualization, but we won't cover that here.

Formatting the Data

It doesn't matter how we get our data, but all visualizations need to receive it in a DataTable object. It's basically a table with rows and columns. Each column is defined with a particular data type (and an ID and a Label which are optional).

To reference a particular cell in the table, you use the pair (row, column). Row is always a number, starting a zero. Column can also be a zero-based number or an optional ID.

If we want to display the earnings of our company in 2009 in a column chart, we have to prepare the data in the following way:

Quarters 2009 Earnings
Q1 308
Q2 257
Q3 375
Q4 123

Two columns: the first one (with type 'string') is the label for each bar in the chart, and the second one (with type 'number') is the value for that bar. We have four rows which means well have four bars to display.

How do we put that in a DataTable object? This is the code to do so - each line is explained later:

First we create our DataTable object with:

Then we define the two columns in our table using the method addColumn(). The first value is the type and the second value is the optional label.

And finally, we define the data rows using the addRows() method.

Each row is an array, and all data is also enclosed in another array.

Rows can also be defined one row at a time:

or even one cell at a time:

Here, the first two numbers are the row and column, respectively.

This is the way to create DataTable objects. Every visualization needs to be loaded with data in this format. That doesn't mean that the table is the same for every visualization. The particular number and type of columns and rows has to be checked in the documentation for each chart.

Visualizing our Data as a Column Chart

For this first example, we'll use a Column Chart to present our data. In the Google Visualization Gallery, we can click any chart type to see documentation and examples.

To use any visualization, we have to load the Google AJAX API before; it provides the core functionality needed in many other google APIs.

Now we can load the Visualization API using the google.load() function (from the AJAX API):

The second parameter, '1,' refers to the version of the API to load ('1' means the current version). 'packages' is an array with all the visualizations we are going to use. In this case, we'll use only one: the column chart.

At this point, we have the necessary libraries to create our DataTable object and display our graph, however, we need to be sure that the visualization is completely loaded, otherwise we'll get JavaScript errors and our graph won't display.

The way to do this is by registering a callback. The function will be called when the visualization (API and package) is loaded.

Function createChart is where we create our data table and our chart. The final, complete code, is:

The chart object is created with this line:

The argument is the DOM reference to the element containing the visualization. In this case, we have a <div id="chart"></div>.

Then, we define the options we want and draw the chart:

Our graph looks like this:

Note: All images here are static to make the tutorial available regardless of your browser or your JavaScript settings. Review the live demo for the interactive version.

And a Pie Chart Too

The advantage of having a clearly defined data format is that once you know how to create and populate a DataTable object, you know how to feed any visualization. You just have to check the documentation to see the particular table (number and type of columns) you have to build.

For a pie chart, we can use the exact same table we have now. Let's add a pie chart in the same page.

We have to add our new package in the google.load() line:

and extend our createChart function with these two lines:

The complete code is:

And the generated charts:

Note: check the live demo for the interactive version.

This was easy in this case, because both visualizations used the same table columns and rows. But there are visualizations that need more columns or columns of different types, and you cannot use the data table directly. However, you can solve this by generating a different view of the original table to feed a visualization. We'll review that shortly.

More Columns for Our Column Chart!

The data table for a column chart doesn't have to be as simple as in the previous example. We can have bars representing the earnings for each quarter in the last three years, for example. In that case the data
table would look like so:

Quarters Earnings 2009 Earnings 2008 Earnings 2007
Q1 308 417 500
Q2 257 300 420
Q3 375 350 235
Q4 123 100 387

The only code we have to change from our first example is the DataTable object, to add two more columns and more data in each row:

The rest of the code does not change. The generated chart is:

But, what if now we want to use a pie chart to represent part of this data? We cannot use the same data table as we did before, because pie charts need a two-column table (slice label and value). There is an easy way to obtain a different table from an existing DataTable object and use it to feed a chart: data Views.

Using Data Views

Views are a way to adapt our table for a different visualization. If we now want to display, in the same page, a pie chart showing the quarterly earnings distribution for last year, the table we need is only this:

Quarters Earnings 2009
Q1 308
Q2 257
Q3 375
Q4 123

A data view (DataView object) allows you to use only a subset of the original data. You can reorder or duplicate columns and rows or introduce columns with calculated values.

First, create the View object:

A data view is initialized with the original table and then we use the DataView methods to hide, show or filter columns or rows ( setColumns(), hideColumns(), setRows(), hideRows(), getFilteredRows, getColumnRange, etc ).

We can filter the original table to get only the first two columns (columns 0 and 1) using setColumns():

Now we can draw the pie chart using this view as a data table:

Remember that we have to include the piechart package with google.load(), and we have to create the pieChart object with:

Now we can see both charts generated using the same data table:

Introducing Events

Events provide an easy way to connect your visualizations with other elements on your page. Visualizations can trigger some events, and you can register a listener to react to that event and perform some action. The event model is similar to the browser event model. Once again, we have to look at the documentation to check the events triggered for each visualization.

To show how events work, let's return to our first example, the simplest column chart:

This graph triggers events on mouseover, on mouseout and on select. That means we can make it much more interactive than it is by default.

Since this graphic shows earnings for a company, it could be interesting to show a message with a brief explanation of the most important achievements or sales for each quarter when the user places the pointer over a column (onmouseover event).

Our callback will be showDetails(), and we register it for the onmouseover event:

The first parameter is the variable that contains our chart object.

We will also need to hide the message when the pointer goes out of the column so we need another function to be called when onmouseout event triggers:

Within the <body> or our HTML page we have to define four divs with the messages:

And then the callback functions just show or hide the corresponding message:

Our functions accept a single parameter: the event fired. This object has all available information about the event details.

To know what bar we are over, we check the 'row' property of the event object. This information refers to the rows and columns of the DataTable object, but we know that row 0 corresponds to Q1, first column, and so on.

Note: Not all events pass the event object. Sometimes you have to use methods to get the information you need, read the visualization documentation to know how to get the information relative to the event fired.

The following listing include the complete code for this example. I have included a short internal CSS snippet to hide the message divs, and provide minimal formatting.

And this is the result:

Once again, check the live demo to see the interactivity.

The Playground

As with the static images, there is a Google Code Playground where you can play with your visualizations and parameters, and view the results:


Hopefully, this should be enough to get you started with Google Chart Tools. Once you get the hang of it, you'll find that a huge level of flexibility is available to you in your web applications. Thanks for reading!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.