Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Charting with JFreeChart in Groovy.

DZone's Guide to

Charting with JFreeChart in Groovy.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}