Developer Testing and Code Metrics22 May 2008 09:46 am
![]()
Everyone loves sparklines, and if you want to build them using Groovy, here’s some code that I took from a fabulous example in JRuby, and modified:
package com.stelligent.gsparkyimport org.jfree.chart.JFreeChart
import org.jfree.chart.axis.NumberAxis
import org.jfree.chart.plot.XYPlot
import org.jfree.chart.renderer.xy.StandardXYItemRenderer
import org.jfree.data.general.Dataset
import org.jfree.data.xy.XYSeries
import org.jfree.data.xy.XYSeriesCollection
import org.jfree.chart.ChartUtilities
class GSparky {
def DEFAULT_HEIGHT = 30
def DEFAULT_WIDTH = 150
boolean build( def data, def imgPath, def height = DEFAULT_HEIGHT, def width = DEFAULT_WIDTH ) {
def chart = buildChartFromData(data)
return buildImageFromChart( chart: chart, height: height, width: width, path: imgPath)
}
private JFreeChart buildChartFromData( data ) {
def dataset = generateDataset(data)
def plot = new XYPlot()
plot.dataset = dataset
plot.domainAxis = minimalAxis()
plot.rangeAxis = minimalAxis()
plot.domainGridlinesVisible = false
plot.domainCrosshairVisible = false
plot.rangeGridlinesVisible = false
plot.rangeCrosshairVisible = false
plot.outlinePaint = null
plot.renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES)
plot.insets = new RectangleInsets(-1, -1, 0, 0)
def chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, false)
chart.borderVisible = false
return chart
}
private boolean buildImageFromChart( args ) {
ChartUtilities.saveChartAsPNG( new File(args.path), args.chart, args.width, args.height )
return true
}
private Object minimalAxis() {
def a = new NumberAxis()
a.tickLabelsVisible = false
a.tickMarksVisible = false
a.axisLineVisible = false
a.negativeArrowVisible = false
a.positiveArrowVisible = false
a.visible = false;
return a
}
private Dataset generateDataset(def data) {
def series = new XYSeries("Sparkline")
def i = 0
data.each { y -> series.add(i++, y) }
def dataset = new XYSeriesCollection()
dataset.addSeries(series)
return dataset
}
}
And.. a test to demonstrate how it works:
package com.stelligent.gsparky
class GSparklineTest extends GroovyTestCase {
public void testBuildSparklineImage() {
def gs = new GSparky()
def data = [20]
def r = new Random( new Date().getTime() )
(0..99).each { x ->
def y = data.get(x) + (x/2 - r.nextInt(x + 1))
data << y
}
assert gs.build( data, "out/GSparklineTest_test1.png")
}
}
*Update* - added a line to remove the grey border from around the graph.

May 24th, 2008 at 5:23 am
nice graph!
can I do this in Mac os X (newbie to Ruby, some BASIC programming experience from decades ago)
May 27th, 2008 at 8:53 am
Stephan,
This is Groovy code, not Ruby, but yes it should work. The original link is Ruby code, but you have to use JRuby, which is a little different.
June 16th, 2008 at 5:55 pm
[…] pm on June 16, 2008 | # | Tags: sparkline Groovy Sparklines, begging to be converted to Scala:http://www.testearly.com/2008/05/22/groovy-sparklines/ […]
June 16th, 2008 at 8:38 pm
The Google charting API also supports sparklines, in case you don’t want to use your own cpu.