One Chart for Candlestick, Other for Volume
One Chart for Candlestick, Other for Volume
Hi!
I wanna have volume and candlestick in different charts.
But how?
I wanna have volume and candlestick in different charts.
But how?
candle and volume
try something like this:
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);
//you could enter the indicator on the dataset2 to substitue volume
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);
//you could enter the indicator on the dataset2 to substitue volume
The code worked very well! Thanks!
I just have to change the line:
valueAxis timeAxis = new DateAxis("Date",SegmentedTimeline.newMondayThroughFridayTimeline());
for:
ValueAxis timeAxis = new DateAxis("Date");
The compiler doesn't recognize the constructor. The change made code compile well but Sundays, Saturdays and holidays appeared on the chart.
I just have to change the line:
valueAxis timeAxis = new DateAxis("Date",SegmentedTimeline.newMondayThroughFridayTimeline());
for:
ValueAxis timeAxis = new DateAxis("Date");
The compiler doesn't recognize the constructor. The change made code compile well but Sundays, Saturdays and holidays appeared on the chart.
Last edited by brunks on Sat Sep 02, 2006 4:01 pm, edited 1 time in total.
Code: Select all
private void configurePlot(XYPlot plot){
plot.setBackgroundPaint(Color.white);
// fundo tracejado VERTICAL
plot.setDomainGridlinePaint(Color.lightGray);
// fundo tracejado HORIZONTAL
plot.setRangeGridlinePaint(Color.darkGray);
// distância entre os eixos e o gráfico
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
// linha tracejada VERTICAL desenhada no click do mouse
plot.setDomainCrosshairVisible(true);
// linha tracejada HORIZONTAL desenhada no click do mouse
plot.setRangeCrosshairVisible(true);
// formatação de data
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
axis.setVerticalTickLabels(true);
axis.setTickLabelInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
axis.setLowerMargin(0.02);
axis.setUpperMargin(0.02);
axis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());
// formatação de retângulos para representar cada cotação no gráfico de linha
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setShape(new Rectangle(3, 3));
renderer.setShapesVisible(true);
renderer.setShapesFilled(true);
}
}
The candles are middle aligned with the dates, the bars are left aligned...