Charting with JFreeChart in Groovy.

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 {
First(20)
Second(30)
Third(10)
Fourth(40)
}
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)
}

fr.pack();
fr.show();


..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

AttachmentSize
gfreechart-piedemo.jpg20.21 KB
0
Average: 5 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Markus replied on Mon, 2008/02/11 - 2:14am

That looks great. JFreeChart is awesome. I am using it in a current project. The choice of charts is impressive. 

Your builder API looks fantastic. That will make JFreeChart code a lot easier to read and understand.

 Markus 

http://www.codekite.com

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.