blog rss feed

Wrapping JFreeChart as a builder for 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.

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

Here’s an example of building a pie chart using the prototype builder:

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 above code created:


generated pie chart

 

Right now, the prototype source is available from this location. Be aware that its not ready for prime time yet.

 

 

Comments

There are no comments yet.

Submit a comment






Consulting