Skip to content

Instantly share code, notes, and snippets.

@guymac
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guymac/8813728 to your computer and use it in GitHub Desktop.
Save guymac/8813728 to your computer and use it in GitHub Desktop.
Groovy x-y charting function
import org.jfree.chart.ChartFactory
import org.jfree.chart.ChartPanel
import org.jfree.chart.plot.XYPlot
import org.jfree.chart.renderer.xy.XYDotRenderer
import org.jfree.data.time.TimeSeriesCollection
import org.jfree.data.xy.XYSeriesCollection
import groovy.swing.SwingBuilder
import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment as gfx
import static javax.swing.WindowConstants.EXIT_ON_CLOSE
import static org.jfree.chart.plot.PlotOrientation.VERTICAL
def static chartWidget(dataset)
{
/* these can be changed in the properties menu
*/
def labs =
[
null, // plot title
null, // x-axis
null // y-axis
]
def none =
[
false, // legend
false, // tooltips
false, // urls
]
def chart
switch (dataset)
{
case TimeSeriesCollection:
chart = ChartFactory.createTimeSeriesChart(*labs, dataset, *none)
break
case XYSeriesCollection:
chart = ChartFactory.createScatterPlot(*labs, dataset, VERTICAL, *none)
default:
break
}
/* override the default (huge) dots
*/
if (chart.plot instanceof XYPlot)
{
chart.plot.renderer = new XYDotRenderer(dotWidth:3, dotHeight:3)
}
def mode = gfx().screenDevices[0].displayMode
/* override the default (tiny) plot size
*/
def draw =
[
3/4*mode.width, // preferred width
3/4*mode.height, // preferred height
mode.width/2, // minimum width
mode.height/2, // minimum height
mode.width, // maximum width
mode.height // maximum height
] as int[]
/* enable all the options
*/
def opts =
[
true, // use buffer
true, // editable labels etc
true, // saveable options
true, // printable
true, // zoomable
true // tooltips
]
def frame = new SwingBuilder().frame(defaultCloseOperation:EXIT_ON_CLOSE)
{
panel(id:'canvas')
{
widget(new ChartPanel(chart, *draw, *opts))
}
}
frame.pack()
frame.show()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment