Timeseries and CandleStick on the same chart

Discussion about JFreeChart related to stockmarket charts.
Locked
dickieB
Posts: 1
Joined: Tue Aug 28, 2012 4:57 pm
antibot: No, of course not.

Timeseries and CandleStick on the same chart

Post by dickieB » Tue Aug 28, 2012 5:21 pm

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: org.jfree.data.xy.XYSeriesCollection cannot be cast to org.jfree.data.xy.OHLCDataset
	at org.jfree.chart.renderer.xy.CandlestickRenderer.initialise(CandlestickRenderer.java:611)
	at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3713)
	at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3310)
	at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1235)
	at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1663)
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.setBaseShapesVisible(false);
               timeSeriesRenderer.setSeriesPaint(0, Color.blue);
               timeSeriesRenderer.setSeriesStroke(0, myStroke);
               dataTrend.addSeries(series);
               plot.setDataset(plot.getDatasetCount()+1, dataTrend);
               plot.setRenderer(plot.getDatasetCount()+1, timeSeriesRenderer);

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Timeseries and CandleStick on the same chart

Post by david.gilbert » Thu Aug 30, 2012 5:35 am

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.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked