How to create overlaid vertical bar chart?

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

How to create overlaid vertical bar chart?

Post by Andy » Thu Jan 17, 2002 9:16 pm

Hello,

I want to overlay three vertical bar charts. But I don't understand how to do this!
I have had a look at the overlaid examples, but I don't understand them. Can someone post an easy example? It's very very important for me. I hope someone can help me.

The biggest problem are the Datasets. I don't know how to create the CombinedDataset.

Thank you,

Andreas

Bill Kelemen

RE: How to create overlaid vertical bar chart?

Post by Bill Kelemen » Sun Jan 20, 2002 9:46 pm

Hi Andreas,

The key to the combined charts is that the main top-level chart (that will contain the other charts) has to have a Dataset that is a super-set of all the other Datasets. That's where the CombinedDataset is used. Example:

// these are the individual Datasets for each (individual) sub-chart
XYDataset dataset1 = ...
XYDataset dataset2 = ...
XYDataset dataset3 = ...

// now make the CombinedDataset
CombinedDataset combinedDataset = new CombinedDataset();
combinedDataset.add(dataset1);
combinedDataset.add(dataset2);
combinedDataset.add(dataset3);

// now make the common horizontal axis (ex. a date axis)
Axis timeAxis = new HorizontalDateAxis("Date");

// now make one veritical axis for each chart to combine
NumberAxis valueAxis1 = new VerticalNumberAxis("Vertical Axis 1");
NumberAxis valueAxis2 = new VerticalNumberAxis("Vertical Axis 2");
NumberAxis valueAxis3 = new VerticalNumberAxis("Vertical Axis 3");

// now make the charts and combine them vertically.
CombinedPlot combinedPlot = new CombinedPlot(timeAxis, CombinedPlot.VERTICAL);
CombinedChart chartToCombine;

// add the first chart (simple XY chart)
chartToCombine = ChartFactory.createCombinableXYChart(timeAxis, valueAxis1, dataset1);
combinedPlot.add(chartToCombine);

// add the second chart (time series chart)
chartToCombine = ChartFactory.createCombinableTimeSeriesChart(timeAxis, valueAxis2, dataset2);
combinedPlot.add(chartToCombine);

// add a third chart (a CandleSticksChart)
// (note that dataset3 would need to implement a HighLowDataset)
chartToCombine = ChartFactory.createCombinableCandleSticksChart(timeAxis, valueAxis3, dataset3);
combinedPlot.add(chartToCombine);

// call this method after all sub-plots have been added
combinedPlot.adjustPlots();

// now create the combined JFreeChart object
chart = new JFreeChart(combinedDataset, combinedPlot);

Hope this helps,
Bill

Locked