overlaid chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
bjm

overlaid chart

Post by bjm » Wed Mar 27, 2002 1:49 pm

Hi,

I would like to make a graph of two overlaid plots, where one is a barchart and the other is a timeseriesChart. The result shold be someting like

http://www.ullnermunch.dk/b2.jpg

But I cannot seem to make it work. Does anybody know, if it is at all possible, and how to do it?

regards,
Bjarne

David Gilbert

Re: overlaid chart

Post by David Gilbert » Thu Mar 28, 2002 4:03 pm

Hi Bjarne,

Thanks for your post. I've just finished writing up an example on how to do this, for inclusion in the PDF documentation.

Along the way I made some changes to JFreeChart for version 0.8.1, so the code I'm about to paste here won't compile as is (I've changed the way XYItemRenderer works and added a new constructor to the XYPlot), but it might point you in the right direction.

I hope to release 0.8.1 next week some time (to incorporate the new French, German and Spanish translations), and it will include the small demo that I'm using for the documentation. Anyway, here's the important part of the code:

private JFreeChart createOverlaidChart() {

// create two sample datasets...
IntervalXYDataset data1 = this.createDataset1();
XYDataset data2 = this.createDataset2();

// create a combined dataset...
CombinedDataset data = new CombinedDataset();
data.add(data1);
data.add(data2);

// create a shared time axis...
ValueAxis timeAxis = new HorizontalDateAxis("Date");
timeAxis.setCrosshairVisible(false);

// create a shared value axis...
NumberAxis valueAxis = new VerticalNumberAxis("Value");
valueAxis.setCrosshairVisible(false);

// make an overlaid plot...
CombinedPlot overlaidPlot = new CombinedPlot(timeAxis, valueAxis);

// add the combinable charts to it...
XYPlot plot0 = new XYPlot(timeAxis, valueAxis, new VerticalXYBarRenderer(0.20));
SeriesDataset s0 = new SubSeriesDataset(data, 0);
CombinedChart c0 = ChartFactory.createCombinableChart(s0, plot0);
overlaidPlot.add(c0);
SeriesDataset s1 = new SubSeriesDataset(data, 1);
CombinedChart c1 = ChartFactory.createCombinableTimeSeriesChart(timeAxis, valueAxis, s1);
overlaidPlot.add(c1);

// perform final adjustments...
overlaidPlot.adjustPlots();

// return a new chart containing the overlaid plot...
return new JFreeChart(data, overlaidPlot,
"Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, true);

}

Hope this helps,

Regards,

DG.

Locked