One Chart for Candlestick, Other for Volume

Discussion about JFreeChart related to stockmarket charts.
Locked
brunks
Posts: 8
Joined: Mon Aug 14, 2006 10:15 am

One Chart for Candlestick, Other for Volume

Post by brunks » Mon Aug 28, 2006 10:06 am

Hi!

I wanna have volume and candlestick in different charts.
But how?

brunks
Posts: 8
Joined: Mon Aug 14, 2006 10:15 am

Post by brunks » Tue Aug 29, 2006 9:41 am

No ideas?

I am a bit "LOST", any help will be fine...

fede
Posts: 20
Joined: Tue May 23, 2006 9:15 am

Post by fede » Tue Aug 29, 2006 1:25 pm

HI,
To draw volume and price in different plots, you have to use a CombinedDomainXYPlot.
In the first subplot you draw you candles with a CandlestickRenderer and in the second subplot you draw volumes using BarsRenderer.
Also you can add as many subplot you want to draw other indicators.

brunks
Posts: 8
Joined: Mon Aug 14, 2006 10:15 am

Post by brunks » Tue Aug 29, 2006 3:14 pm

Thanks!

I will search about the class.

But how I make candlestick chart dont draw the volume bars?

fede
Posts: 20
Joined: Tue May 23, 2006 9:15 am

Post by fede » Tue Aug 29, 2006 3:47 pm

To don't show volumes in candlessticks renderer you just set the setDrawVolume(false) as follow:

CandlestickRenderer(); candlesRenderer = new CandlestickRenderer();
candlesRenderer.setDrawVolume(false);

vuj2010
Posts: 11
Joined: Fri Apr 07, 2006 11:54 pm

candle and volume

Post by vuj2010 » Wed Aug 30, 2006 4:35 am

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

brunks
Posts: 8
Joined: Mon Aug 14, 2006 10:15 am

Post by brunks » Wed Aug 30, 2006 9:00 pm

Thank you, fellows!

I am work on it.
Results (or more doubts) I'll post as soon as I can.

brunks
Posts: 8
Joined: Mon Aug 14, 2006 10:15 am

Post by brunks » Thu Aug 31, 2006 7:00 pm

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.
Last edited by brunks on Sat Sep 02, 2006 4:01 pm, edited 1 time in total.

brunks
Posts: 8
Joined: Mon Aug 14, 2006 10:15 am

Post by brunks » Fri Sep 01, 2006 11:09 pm

valueAxis timeAxis = new DateAxis("Date",SegmentedTimeline.newMondayThroughFridayTimeline());
That was supposed to work according to the api documentation but is not. Some clue?

brunks
Posts: 8
Joined: Mon Aug 14, 2006 10:15 am

Post by brunks » Sat Sep 02, 2006 7:45 pm

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);
		}
	}
More 1 question. The volume bars are not aligned with the candles.
The candles are middle aligned with the dates, the bars are left aligned...

Locked