Page 1 of 1

Timeseries and CandleStick on the same chart

Posted: Tue Aug 28, 2012 5:21 pm
by dickieB
Does anyone know how to create a chart that has a candlestick chart and a timeseries chart on the same plot? (i.e. I don't want a combined domain chart I want the timeseries and the candles overlayed).

I tried adding a timeseries dataset to the candlestick plot but it throws a runtime error saying:

Code: Select all

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: cannot be cast to
	at org.jfree.chart.renderer.xy.CandlestickRenderer.initialise(
	at org.jfree.chart.plot.XYPlot.render(
	at org.jfree.chart.plot.XYPlot.draw(
	at org.jfree.chart.JFreeChart.draw(
	at org.jfree.chart.ChartPanel.paintComponent(
a code snippet looks like

Code: Select all

		JFreeChart jfreechart = ChartFactory.createCandlestickChart("Prototype", "Time", "Value", getDataset(bars), true);
		XYPlot plot = (XYPlot)jfreechart.getPlot();
   	        Stroke myStroke = new BasicStroke((float) 1.0);
    	       XYLineAndShapeRenderer timeSeriesRenderer = new XYLineAndShapeRenderer();
               timeSeriesRenderer.setSeriesStroke(0, myStroke);
               plot.setDataset(plot.getDatasetCount()+1, dataTrend);
               plot.setRenderer(plot.getDatasetCount()+1, timeSeriesRenderer);

Re: Timeseries and CandleStick on the same chart

Posted: Thu Aug 30, 2012 5:35 am
by david.gilbert
JFreeChart supports overlaying by letting you add more than one renderer (for example a HighLowRenderer and an XYLineAndShapeRenderer) to the plot instance. Each renderer should have its own data set - you'll see there is a method for adding data sets that lets you specify an index. The data set should be a compatible type for the corresponding renderer.