Wednesday, January 9, 2008

Google Chart Builder

For Groovy and Grails

UPDATE: (2/4/2008) This builder has been added to the Google Charts Plugin for Grails version 0.4

UPDATE: Due to a conflict in Grails with the set closure, it has been renamed to dataSet. The examples have been updated as well as the source code and tests.

A little while ago Google released a their Chart API into the wild. It’s basically allows a client to call a RESTful url and get a PNG image back of the chart. Very cool stuff. After playing with it for a little bit I quickly realized that this RESTful approach was both very powerful and very annoying. I primarily do web development and if I’m creating a chart I want it to be on the fly and dynamic. Injecting data and labels into a potentially long url was not my idea of a good time.

Enter the GoogleChartBuilder (Download Source).
Written in Groovy utilizing it’s fantastic BuilderSupport class.


Here is an example on how to create a 3D Pie Chart:


def chart = new GoogleChartBuilder()
def textList = (1..5).toList()
def result = chart.pie3DChart{
size(w:350, h:200)
data(encoding:'text'){
dataSet(textList)
}
labels{
textList.each{ label(it) }
}
}

The result will yield this url string:

http://chart.apis.google.com/chart?cht=p3&chs=350x200&chd=t:1,2,3,4,5&chl=1|2|3|4|5

Submit this URL and you will get the following chart:





Here is an example on how to create a Horizontal Grouped Bar Chart:


def chart = new GoogleChartBuilder()
result = chart.barChart(['horizontal', 'grouped']){
barSize(witdth:10, space:2)
size(w:350, h:200)
title(color:808080, size:16){
row('Chart 1')
row('Sampel bar chart')
}
data(encoding:'simple'){
dataSet((1..5).toList())
dataSet((5..1).toList())
}
colors{
color('66CC00')
color('3399ff')
}
legend{
label('Joy')
label('Pain')
}
axis(left:(1..5).toList(), bottom:[])
backgrounds{
background{
solid(color:'999999')
}
area{
gradient(angle:45, start:'CCCCCC', end:'999999')
}
}
}
}

The result will yield this url string:

http://chart.apis.google.com/chart?cht=bhg&chbh=10,2&chs=350x200&chts=808080,16&
chtt=Chart+1|Sampel+bar+chart&chd=s:BCDEF,FEDCB&chco=66CC00,3399ff&
chdl=Joy|Pain&chxt=y,x&chxl=0:|1|2|3|4|5&chf=bg,s,999999|c,lg,45,CCCCCC,0,999999,1

Submit this URL and you will get the following chart:


Here is an example on how to create a Line Chart:


def chart = new GoogleChartBuilder()
result = chart.lineChart{
size(w:300, h:200)
title{
row('Joy vs. Pain')
}
data(encoding:'extended'){
dataSet([1,18,200,87,1090,44,3999])
dataSet([88,900,77,1,2998,4])
}
colors{
color('66CC00')
color('3399ff')
}
lineStyle(line1:[1,6,3])
legend{
label('Joy')
label('Pain')
}
axis(left:(1..5).toList(), bottom:[])
backgrounds{
background{
solid(color:'999999')
}
area{
gradient(angle:45, start:'CCCCCC', end:'999999')
}
}
markers{
rangeMarker(type:'horizontal', color:'FF0000', start:0.75, end:0.25)
rangeMarker(type:'vertical', color:'0000cc', start:0.7, end:0.71)
}
}

The result will yield this url string:

http://chart.apis.google.com/chart?cht=lc&chs=300x200&chtt=Joy+vs.+Pain&
chd=e:ABASDIBXRCAs-f,BYOEBNABu2AE&chco=66CC00,3399ff&chls=1,6,3&
chdl=Joy|Pain&chxt=y,x&chxl=0:|1|2|3|4|5&chf=bg,s,999999|c,lg,45,CCCCCC,0,999999,1&
chm=r,FF0000,0,0.75,0.25|R,0000cc,0,0.7,0.71



Submit this URL and you will get the following chart:



The Google Charts API also allows for the creation of:
  • Scatter Point charts.
  • Venn Diagrams
  • 2D pie charts
  • Vertical Stacked Bar Charts
  • Vertical Grouped Bar Charts
  • Horizontal Stacked Bar Chars
  • Line XY Plot Chart
To see examples of these charts (Download the source code) and run the test suite under the 'test' folder. The following tests will have examples on how to use the Google Chart Builder and will also generate HTML files (in a folder named GoogleCharts under root)that contain the URL strings.
  • PieChartTest.groovy
  • BarChartTest.groovy
  • ScatterPlotTest.groovy
  • VennDiagramTest.groovy
  • LineChartTest.groovy
These

Documentation on the keywords that are used in the Google Chart Builder can be found in the 'docs' folder or here.

As you can see the Google Charts API allows you do generate some pretty complicated charts. However because of the RESTful nature of these charts the URLs can get out of hand very fast. My intention is to ease the pain in using this useful API.

5 comments:

Graeme Rocher said...

Looks nice, maybe you should consider contributing to the existing grails google chart plugin

http://plugins.grails.org/grails-google-chart/

The current author is James williams:

http://www.jameswilliams.be/blog/entry/54;jsessionid=FA9F14C05405403BD0DCDCC3CC811875

He has a custom tag, but the adding a builder to the mix would be neat

board tc said...
This comment has been removed by the author.
Anonymous said...

Hi!!! zanthrash.blogspot.com is one of the best resourceful websites of its kind. I take advantage of reading it every day. zanthrash.blogspot.com rocks!

Anonymous said...

miley cyrus nude miley cyrus nude miley cyrus nude

Anonymous said...

Hi
Costume Watches Manufacturers & Suppliers, Global Sources

[url=http://auxwatches.007sites.com]cie watches[/url]
[url=http://colewatches.5webs.net]citizen solar watches[/url]
[url=http://jojowatches.hostaim.com]cole watches[/url]
[url=http://watches.awardspace.biz]concord diamond watch[/url]
[url=http://watchestop.0catch.com]costume watches[/url]