Over a million developers have joined DZone.

Charting with JFreeChart in Groovy.

DZone's Guide to

Charting with JFreeChart in Groovy.

· Java Zone ·
Free Resource

Learn how to stop testing everything every sprint and only test the code you’ve changed. Brought to you by Parasoft.

I’ve started looking into the possibility of wrapping up JFreeChart to make it a little groovier. I know there’s no end of web packages for graphing, but what if you don’t want to transmit sensitive data across the internet? What if you don’t want the data in the URL? What if the application in question is a Swing GUI thats not connected to the internet?

It's for all those situations that I have looked at wrapping up the excellent http://www.jfree.org/jfreechart API in a Groovy builder. My proposed name is FreeChartBuilder.

FreeChartBuilder is an early look at how a builder object using the state machine pattern could provide fairly complete support for the underlying Java API. The currently attached source archive gives an example of what can be achieved for pie and simple line charts.

Example of using the prototype source

import com.thecoderscorner.gfreechart.GFreeChartBuilder
import javax.swing.ImageIcon
import java.awt.Color
import groovy.swing.SwingBuilder
import javax.swing.JFrame
import java.awt.BorderLayout

def chart = new GFreeChartBuilder();

chart.createPie(title: "Pie Chart", legend: true, size: [400,200]) {
dataset {
antiAlias true
backgroundPaint Color.WHITE

plot {
sectionOutlinesVisible true
font name: 'arial', height: 15
labelGap 0.02
simpleGradient( start: new Color(0,0,255),
end: new Color(255,255,255) )

ImageIcon pieImg = chart.getIconImage()

def sb = new SwingBuilder()
def fr = sb.frame( title : 'Test Graph', size:[800,600],
defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
label(icon: pieImg, constraints: BorderLayout.CENTER)


..and the output of the above code:

chart produced by above source
 Right now you can look at the prototype source which I will update as I make progress
However, be aware it is a prototype, and not even close to production quality yet.


Reprinted from original blog entry at: http://www.thecoderscorner.com/blogEntry/show/16

Get the top tips for Java developers and best practices to overcome common challenges. Brought to you by Parasoft.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}