Jim Clark has put together a great little JFreeChart builder for Groovy called GroovyChart. I’ve been thinking for a while that I’d love to add some simple charting to GroovyBlogs, and I figure this was a great way to get into it. I’ve had a play, and could generate the demo charts just fine, but once I wanted to go a little deeper, I got a little lost in the builder semantics. It looks like it’s going to be a great little library, though.

So I retreated to using the standard JFreeChart APIs, working off some of the samples you get with the manual, and I’ve got my first little chart happening. Still a little work to do on the fonts, but it’s travelling very nicely:

Stats Graph with Alpha Blend

That alpha-blending transparency on the png was the only really tricky bit and courtesy of a little of Aaron’s magic, even that is very doable. The final pieces of implementing the magic in my Grails controller ended up being:

chart.setBackgroundPaint(new Color(255,255,255,0))
KeypointPNGEncoderAdapter encoder = new KeypointPNGEncoderAdapter()
encoder.setEncodingAlpha(true)

def cb = encoder.encode(chart.createBufferedImage(170, 150,
     BufferedImage.BITMASK, null))

response.setContentType("image/png")
response.setContentLength(cb.length)
response.getOutputStream().write(cb)

Hope to get it deployed to prod groovyblogs later in the week. I’ve also got some interesting translation functionality in the mix, too. So stay tuned…