Search code examples
javajfreechart

How can multiple datasets be used with JFreeChart?


How could I add to a plot an OHLCSeriesCollection and a TimeSeriesCollection , in order to represent their values in the same chart ?


Solution

  • Both OHLCSeriesCollection and TimeSeriesCollection are based on XYDataset so you should be able to add them both to an XYPlot with something like the following:

    JFreeChart chart = // create your XY chart here.
    XYPlot plot = chart.getXYPlot();
    OHLCSeriesCollection ohlsSeriesDataset = // create you ohlc dataset here.
    TimeSeriesCollection timeSeriesDataset = // create you time dataset here.
    AbstractXYItemRenderer olhsSeriesRenderer = // create your ohlc renderer here.
    AbstractXYItemRenderer timeSeriesRenderer = // create your time renderer here.
    
    plot.setDataset(0, ohlsSeriesDataset);
    plot.setDataset(1, timeSeriesDataset);
    plot.setRenderer(0, olhsSeriesRenderer);
    plot.setRenderer(1, timeSeriesRenderer);
    

    The type of renderer to use for olhsSeriesRenderer and timeSeriesRenderer really depends on the type of chart you want to generate so I cannot give you specifics here.

    I have not tried this myself with XY datasets, but I have been able to do combine CategoryDatasets using this.