Monday, February 4, 2008

Updated GoogleChart Plugin ver 0.4

A couple weeks ago I released a GoolgeChartBuilder for the Google Charts API. At about the same time James Williams released the Google Chart Plugin for Grails.

Up until now the Google Chart Plugin provided the Grails developer with a fantastic way to generate charts in the view layer (GSP) via a tag library.

With the addition of the builder; the chart URLs can now be easily created in a domain, controller, or service object. The builder functionally provides the opportunity to save the URL to a database or pre-fetch the image file and cache it on the server.

The core functionally of the original builder remains intact and examples can be found here, and documentation can be found here.

The names of the charts were changed to more closely tie the builder to the tag library.

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.

Thursday, November 29, 2007

Coding with Crayons

A while back I listened to a great interview of Neal Ford speaking on DSL's. In this interview Mr. Ford states that he
"Would rather code Groovy in Vi than Java in IDEA"

At first when I heard this, it brought me pause, but I quickly deemed it not worthy of any additional thought. That is until recently, when I was forced to write a small, trivial application in Java...with pen and paper. The problem is stated below, and it had to compile and run. If I had my trusty IDE with intellisence, code completion, syntax highlighting, and refactoring I could have probably cranked out the solution in about a minute (it actually took me about 4 minutes when I tried, but there were unit tests involved).

As a counterpoint I decided to solve the same problem in Groovy. To do the Groovy development I fired up TextMate on my Mac (but I could have just as easily used the ever crappy Notepad on Windows).

What I came away from this exercise: How relient I had become on my IDE to make me an efficient and effective Java developer. Java is just to verbose and rigid of a language to code in pen and paper, even for a simple app. With Groovy I feel like I could break out the box of 64 Crayola Crayons and happily code away on some construction paper, run it through an OCR scanner and then execute it via the command line. Joy!

It also got me thinking of Mr. Fords original quote and I now appreciate what a profound statement that really was.

PROBLEM:
  • You have a List of letters (A-G)
  • Create a class and the necessary methods that do the following:
    • In the list replace:
      • B with C
      • D with X
      • X with Z
  • The replacement should handle all transitive replacements
  • All duplicates in the list should be removed.
My Java Solution:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ListSwapper {


public static void main(String[] args) {
ListSwapper swapper = new ListSwapper();

List baseList = new ArrayList() {
{
add("A"); add("B"); add("C"); add("D");
add("E"); add("F"); add("G");
}
};

List dedupedList = new ArrayList() {
{
add("A"); add("C"); add("Z");
add("E"); add("F"); add("G");
}
};

List result = swapper.dedupe(swapper.swap(baseList));

assert result.equals(dedupedList);
}

private Map map = new HashMap(){
{
put("B", "C"); put("D", "X"); put("X", "Z");
}
};

List swappedList = new ArrayList();

public List swap(List baseList) {
for (Iterator iterator = baseList.iterator(); iterator.hasNext();) {
String letter = (String) iterator.next();
swapLetter(letter);
}
return swappedList;
}


private void swapLetter(String letter) {
String swappedLetter = map.get(letter);
if (swappedLetter == null) {
swappedList.add(letter);
} else if (swappedLetter != null) {
swapLetter(swappedLetter);
}
}

public List dedupe(List swappedList) {
for (int i = 0; i < j =" i">



My Groovy Solution (This is a method-to-method replacement)


class Util {
def myMap = ['B':'C' , 'D':'X', 'X':'Z']

def swap(List list){
list.eachWithIndex {item, index ->
list[index] = swap(item)
}
list
}

def swap(letter){
def newLetter = myMap.get(letter)
newLetter ? swap(newLetter) : letter
}

def prune(list){
list.unique()
}
}

def x = new Util()
assert x.prune(x.swap(('A'..'G').toList())) == ["A", "C", "Z", "E", "F", "G"]

This code could probably be made Groovier. The prune(list) method seems a little unnecessary at this point.

Tuesday, May 15, 2007

Conversations Under a Waterfall : Documentation

I don't know yet if this will be a on going series, but I have a lot of material to work with. The current company that I am working at has fully embraced all of the best practices of software development that the 1970's has to offer. That's right...they love their waterfall. To be honest management loves the waterfall. The developers simply "put up with it". I don't know which is worse. So as self proclaimed agile developer, I thought it would be fun to document some of the conversations that I have with my colleagues (the names and identities have been changed to protect the innocent).

Shout from over a cube wall: Does anyone know of a good Eclipse UML plugin?

I get up to investigate the source of the question.

Me: What do you want to do with UML?

Andy (original shouter): Dave asked me to put together a proposal for the web service I just wrote.

Me: A proposal? For something you already implemented?

Andy: Yep.

Me: Is it in production?

Andy: Yep.

Me: So why do you need a proposal?

Andy (now appearing annoyed that I am asking him a bunch of questions that do not address his original question): Dave needs to get Tom to sign off on the app, and he wont do it unless it is "fully" documented. So Dave wants me to make the document so large that Tom wont want to read it.

Me: Does make any sense you?

Andy: This is just the way it is.


I just walked away. I really wanted to help Andy, but more so I wanted him to be as upset as I was. The fact that management, or anyone for that matter, would value his time, energy, and talent so little, as to make him knowingly produce something that has no attainable benefit to anyone is a shame.

Tuesday, April 3, 2007

First Post

Until now I have made a conscious decision not to "blog". Mostly because I hate the word "Blog" and utterly loathe the word: "Blog-o-sphere". But also because of time. My plan for this medium is to use it to share my thoughts and experiences in the art and practice of software development, agility, and what I'm probably going to write about most...Groovy and Grails. I've been playing with these languages/frameworks for a while now and am a big fan and look forward to sharing what I learn along the way.