How to draw candlestick chart in a combined chart
How to draw candlestick chart in a combined chart
The default behavior of candlestick chart draws the volumn chart on the same chart. But I would like the have the volumn chart drawen below the candlestick chart. Is it possibile to change the default behavior to meet my requirements? Or is there any alternative way to do so like using combined chart?
Candle/Volume combined chart
A combined chart is the right way. The constructor of a CandleStickRenderer contains a boolean variable whether the volume should be displayed or not. Just create a seperate plot for the volume data and create a combined chart.
Here is my example code:
Martin
Here is my example code:
Code: Select all
OHLCDataset dataset=createDataset(); // creates dataset for candlestick
TimeSeriesCollection dataset2 =createDataset2(); //creates dataset for volume chart
String charttitle = "";
valueAxis timeAxis = new DateAxis("Date",SegmentedTimeline.newMondayThroughFridayTimeline());
timeAxis.setLowerMargin(0.02); // reduce the default margins on the time axis
timeAxis.setUpperMargin(0.02);
NumberAxis valueAxis1 = new NumberAxis("Price");
valueAxis1.setAutoRangeIncludesZero(false); // override default
NumberAxis valueAxis2 = new NumberAxis("Volume");
valueAxis2.setAutoRangeIncludesZero(false); // override default
valueAxis2.setNumberFormatOverride(new DecimalFormat("0"));
CandlestickRenderer candle = new CandlestickRenderer(4,false,new HighLowItemLabelGenerator());
XYBarRenderer rr2 = new XYBarRenderer();
rr2.setToolTipGenerator(new StandardXYToolTipGenerator("date={1}, volume={2}",new SimpleDateFormat("dd-MM-yyyy"),new DecimalFormat("0")));
XYPlot subplot1 = new XYPlot(dataset, timeAxis, valueAxis1, candle);
subplot1.setBackgroundPaint(Color.white);
XYPlot subplot2 = new XYPlot(dataset2, timeAxis, valueAxis2, rr2);
subplot2.setBackgroundPaint(Color.white);
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(timeAxis);
plot.setGap(10.0);
plot.add(subplot1, 3);
plot.add(subplot2, 1);
plot.setOrientation(PlotOrientation.VERTICAL);
JFreeChart chart = new JFreeChart(charttitle, JFreeChart.DEFAULT_TITLE_FONT, plot, true );
chartPanel.setChart(chart);