Quick-Start Guide for Google Charts API
Join the DZone community and get the full member experience.Join For Free
one-off chartsif you just need a beautiful graph to put into a document or post, then the chart wizard is the best place to start. you simply choose the type of chart you want, input your data and choose the colours, labels, settings etc, and the wizard generates the url for you to copy and paste!
the charts are highly configurable and can easily be changed by editing the url to the image (view the source of this page to see the url for the chart shown above). this makes it really easy to generate similar-but-different charts in your web applications, by using the wizard and then replacing some relevant parts of it.
dynamic chartsas mentioned above, it is very simple to generate charts with google charts api - and all the information for generating the chart is on a url which returns a png file of the resulting chart. this makes it ideal for integrating into our dynamic web applications and charts that are generated are almost overwhelmingly configurable . that said, there are a few key options that will get you started quickly so let's take a quick tour.
there are so many types of chart available, and as well as being scientifically accurate and correct, they are also very pretty! a full selection of line, bar and pie charts are available and can be combined, labelled, stacked and generally perform some tricks i've never mastered with the graph function of my spreadsheet package. check out the chart gallery for an exhaustive selection; i think my personal favourite is the google-o-meter which i'm increasingly using for dashboard applications.
whatever type of chart you want to create, all the urls start with:
then you start adding information - the chart type is cht so for a line chart, you would pass cht=lc as part of the url.
this is a required parameter; your chart won't work without the size information being included. you specify the width and height, in pixels, that the resulting image should be, separated by an 'x'. so for an image which is 300px wide and 100px high, our image url would need to include chs=300x100 . note that the size is the whole image, including titles, labels, legends and so on, so the graph itself may be smaller than this.
on to the important stuff - the data for the chart. this is passed in the chd parameter, and the simplest format is the text format. this format allows you to specify a comma-separated list of values, with each series separated by a pipe. for example, we now have the basic ingredients for a chart showing some data i just invented:
as you can see, simply listing data points gives us our two data series shown on the graph. the final parameter shown here is chds which defines the scales for the data; we'll talk about axes next.
axes and labelling
the google charts don't just scale the data and then label the axes; these are actually two entirely separate operations. this is because you can have multiple data series on one graph, with axes on both sides. this makes a very powerful and flexible tool but it does make a simple graph a tiny bit more complicated than i initially expected.
the default is for the y axis to range from 0 to 100, with the x axis items equally spaced. in the example above i used the chds parameter to change the scale to run from 0 to 10, so my data would display better.
we can add some axis labels too, for both y axis (numbers) and x axis (days of the week).
there is quite a lot needed to make this all happen, so let us take one parameter at a time. the first parameter to include is chxt which dictates which axes to display titles for. if you forget this parameter, you will not see any labels so beware! it is possible to add multiple labels to the same axes, and to add labels for the right and top axes if desired. the position of each axis in this list dictates how it is referred to in all the other axis settings. so we have x in position 0 and y in position 1.
to set the labels (i.e. not a numeric scale) for the x axis, we use the chxl parameter. since the x axis was the first one listed in chxt . we start our line with 0:| and then add our pipe-separated labels.
as soon as you include the y axis in the chxt list, you will see axis labels, however these will show the default range of 0 to 100! google charts handles its axis scales entirely independent from the data scales so this is in fact a feature although at first glance it perhaps isn't the behaviour you might expect. for a numeric scale we use the chxr to specify a range to label, and optionally the interval to use. in the example shown above, we gave the axis index (from the list we gave as the value for chxt), the minumin and maximum values, and the interval to label at.
as a finishing touch, we can change the colours of our lines to make the graph prettier. google charts uses hex colours, so this is a pretty easy step. one nice touch is that it also supports an additional two digits on the hex colour, which is the transparency of the colour. here's the same example with the colours added, using the chco parameter with a list of colours; one for each data series:
go forth, and chart
hopefully this has shown how easy it is to create a simple yet presentable chart using the google charts api. the examples showed a line chart but the same principles apply to all the different chart types - there are some wonderful visual examples in the extensive documentation so do jump in and see what you can make!
if you do start using google charts, or are using them already, what are you making? i'd love to see some more examples of the kinds of things they can generate!
Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.