Friday, January 11, 2008

How to create quick and nice charts in JGrass

Now JFreechart is a beautiful charting library.
Now that they have SWT support built in, it is really the best open sourced out there.
Visit them and buy a nice manual, they deserve it :) www.jfree.org

Lately we have built in some facilities in JGrass to do some nice and straightforward charting. Here we go with a standalone example that links to the JGrass eu.hydrologis.jgrass.charting plugin:


public static void main( String[] args ) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());

NumericChartData numericChartData = new NumericChartData(2);

/*
* first tab
*/
NumericChartDataItem tab1 = numericChartData.getChartDataItem(0);
/*
* title to be taken in the case of composed charts. In that case it is ambiguos which title
* of which chart should be taken
*/
tab1.bigTitle = "Title of tab 1";
/*
* extra string that will be taken to give the tab a name, the title could be non suitable
*/
tab1.chartStringExtra = "Text of tab 1";
/*
* in tab 1: first chart with 2 series
*/
// the title for this chart
tab1.chartTitles.add("Chart title 1 in tab 1");
// x label
tab1.chartXLabels.add("X data");
// y label
tab1.chartYLabels.add("Y data");
// define some data
double[][][] data11 = new double[][][]{{{-1, -2, -3}, {2, 4, 6}}, {{1, 2, 3}, {2, 4, 6}}};
tab1.chartSeriesData.add(data11);
// give the series for this chart a name
tab1.seriesName.add(new String[]{"series 1", "series 2"});
/*
* in tab 1: second chart with 1 serie
*/
tab1.seriesName.add(new String[]{"series 1"});
double[][][] data12 = new double[][][]{{{1, 2, 3}, {2, 4, 6}}};
tab1.chartTitles.add("Chart title 2 in tab 1");
tab1.chartXLabels.add("X data");
tab1.chartYLabels.add("Y data");
tab1.chartSeriesData.add(data12);

/*
* second tab
*/
NumericChartDataItem tab2 = numericChartData.getChartDataItem(1);
tab2.bigTitle = "Title of tab 2";
tab2.chartStringExtra = "Text of tab 2";
/*
* in tab 2: one single chart with 3 series
*/
tab2.chartTitles.add("Chart title 1 in tab 2");
tab2.chartXLabels.add("X data");
tab2.chartYLabels.add("Y data");
double[][][] data2 = new double[][][]{{{-1, -2, -3}, {2, 4, 6}}, {{1, 2, 3}, {2, 4, 6}},
{{1, 2, 3}, {-2, -4, -6}}};
tab2.chartSeriesData.add(data2);
tab2.seriesName.add(new String[]{"series 1", "series 2", "series 3"});

/*
* create the chart using a
*/
ChartCreator creator = new MultiXYTimeChartCreator();
// tweak some stuff
/* create all the charts in the list for every tab? Yes. */
creator.M_HINT_CREATE_CHART = new boolean[][]{{true, true}, {true, false}};
/* create the checkboxes to hide and unhide the series? First no, second yes */
creator.M_HINT_CREATE_TOGGLEHIDESERIES = new boolean[][]{{false, false}, {true, false}};
/* define the types of chart to create */
creator.M_HINT_CHART_TYPE = new int[][]{
{ChartCreator.XYBARCHART, ChartCreator.XYLINECHART}, {ChartCreator.XYLINECHART, -1}};
/* define the vertical orientation of the chart */
creator.M_HINT_CHARTORIENTATION_UP = new boolean[][]{{false, true}, {true, true}};
/*
* define the colors of the series, if = null, colors are taken automatically
*/
creator.M_HINT_CHARTSERIESCOLOR = new Color[2][2][3];
// tab 1, chart 1, all series
creator.M_HINT_CHARTSERIESCOLOR[0][0] = new Color[]{Color.blue, Color.red, null};
// tab 1, chart 2, all series
creator.M_HINT_CHARTSERIESCOLOR[0][1] = new Color[]{Color.green, null, null};
// tab 2, chart 1, all series
creator.M_HINT_CHARTSERIESCOLOR[1][0] = new Color[]{Color.blue, Color.red, Color.yellow};

/*
* finally create that plot
*/
creator.makePlot(shell, numericChartData);

shell.pack();
shell.open();
while( !shell.isDisposed() ) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}


And this is how the snippet looks in the real world:



Post a Comment