Candlestick chart with SegmentedTimeLine does not work...

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
hyeungsf
Posts: 6
Joined: Sat May 31, 2008 3:19 am

Candlestick chart with SegmentedTimeLine does not work...

Post by hyeungsf » Wed Nov 12, 2008 10:24 pm

Candlestick chart with SegmentedTimeLine not working if the data set start at Monday.

I wonder if other folks have the same issue. The Candlestick chart will show, but the candlestick is displayed as vertical lines and the volume is not shown.

A test case is to display a candlestick chart for a stock from 2008-10-12 (Monday) to 2008-11-12. The chart has no issue if SegmentedTimeLine is not used or if the stock data starts from 2008-10-10 (Friday).

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

Post by david.gilbert » Thu Nov 13, 2008 9:21 am

Can you post a small demo that shows the problem? The SegmentedTimeline code is pretty complex and the bugs can be timezone dependent as well. For years now I've said I should come up with a simpler solution....
David Gilbert
JFreeChart Project Leader

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

hyeungsf
Posts: 6
Joined: Sat May 31, 2008 3:19 am

Code Sample - I modified the original candlestick demo

Post by hyeungsf » Thu Nov 13, 2008 6:17 pm

/**
* A demo showing a candlestick chart.
*/
public class CandlestickChartDemo2 extends ApplicationFrame {

/**
* A demonstration application showing a candlestick chart.
*
* @param title the frame title.
*/
public CandlestickChartDemo2(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}

/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return The dataset.
*/
private static JFreeChart createChart(OHLCDataset dataset) {
JFreeChart chart = ChartFactory.createCandlestickChart(
"Candlestick Demo",
"Time",
"Value",
dataset,
true
);
XYPlot plot = (XYPlot) chart.getPlot();
NumberAxis axis = (NumberAxis) plot.getRangeAxis();
axis.setAutoRangeIncludesZero(false);
axis.setUpperMargin(0.0);
axis.setLowerMargin(0.0);
((DateAxis) plot.getDomainAxis()).setTickMarkPosition(DateTickMarkPosition.MIDDLE);
((DateAxis) plot.getDomainAxis()).setTimeline(CustomSegmentedTimeline.getSegmentedTimeLine());

return chart;
}

/**
* Creates a sample high low dataset.
*
* @return a sample high low dataset.
*/
public static OHLCDataset createDataset() {

Date[] date = new Date[22];
double[] high = new double[22];
double[] low = new double[22];
double[] open = new double[22];
double[] close = new double[22];
double[] volume = new double[22];

int nov = 11;
int oct = 10;

date[0] = DateUtilities.createDate(2008, oct, 13);
high[0] = 47.0;
low[0] = 33.0;
open[0] = 35.0;
close[0] = 33.0;
volume[0] = 100.0;

date[1] = DateUtilities.createDate(2008, oct, 14);
high[1] = 47.0;
low[1] = 32.0;
open[1] = 41.0;
close[1] = 37.0;
volume[1] = 150.0;

date[2] = DateUtilities.createDate(2008, oct, 15);
high[2] = 49.0;
low[2] = 43.0;
open[2] = 46.0;
close[2] = 48.0;
volume[2] = 70.0;

date[3] = DateUtilities.createDate(2008, oct, 16);
high[3] = 51.0;
low[3] = 39.0;
open[3] = 40.0;
close[3] = 47.0;
volume[3] = 200.0;

date[4] = DateUtilities.createDate(2008, oct, 17);
high[4] = 60.0;
low[4] = 40.0;
open[4] = 46.0;
close[4] = 53.0;
volume[4] = 120.0;

date[5] = DateUtilities.createDate(2008, oct, 20);
high[5] = 62.0;
low[5] = 55.0;
open[5] = 57.0;
close[5] = 61.0;
volume[5] = 110.0;

date[6] = DateUtilities.createDate(2008, oct, 21);
high[6] = 65.0;
low[6] = 56.0;
open[6] = 62.0;
close[6] = 59.0;
volume[6] = 70.0;

date[7] = DateUtilities.createDate(2008, oct, 22);
high[7] = 55.0;
low[7] = 43.0;
open[7] = 45.0;
close[7] = 47.0;
volume[7] = 20.0;

date[8] = DateUtilities.createDate(2008, oct, 23);
high[8] = 54.0;
low[8] = 33.0;
open[8] = 40.0;
close[8] = 51.0;
volume[8] = 30.0;

date[9] = DateUtilities.createDate(2008, oct, 24);
high[9] = 47.0;
low[9] = 33.0;
open[9] = 35.0;
close[9] = 33.0;
volume[9] = 100.0;

date[10] = DateUtilities.createDate(2008, oct, 27);
high[10] = 54.0;
low[10] = 38.0;
open[10] = 43.0;
close[10] = 52.0;
volume[10] = 50.0;

date[11] = DateUtilities.createDate(2008, oct, 28);
high[11] = 48.0;
low[11] = 41.0;
open[11] = 44.0;
close[11] = 41.0;
volume[11] = 80.0;

date[12] = DateUtilities.createDate(2008, oct, 29);
high[12] = 60.0;
low[12] = 30.0;
open[12] = 34.0;
close[12] = 44.0;
volume[12] = 90.0;

date[13] = DateUtilities.createDate(2008, oct, 30);
high[13] = 58.0;
low[13] = 44.0;
open[13] = 54.0;
close[13] = 56.0;
volume[13] = 20.0;

date[14] = DateUtilities.createDate(2008, oct, 31);
high[14] = 54.0;
low[14] = 32.0;
open[14] = 42.0;
close[14] = 53.0;
volume[14] = 70.0;

date[15] = DateUtilities.createDate(2008, nov, 3);
high[15] = 53.0;
low[15] = 39.0;
open[15] = 50.0;
close[15] = 49.0;
volume[15] = 60.0;

date[16] = DateUtilities.createDate(2008, nov, 4);
high[16] = 47.0;
low[16] = 33.0;
open[16] = 41.0;
close[16] = 40.0;
volume[16] = 30.0;

date[17] = DateUtilities.createDate(2008, nov, 5);
high[17] = 55.0;
low[17] = 37.0;
open[17] = 43.0;
close[17] = 45.0;
volume[17] = 90.0;

date[18] = DateUtilities.createDate(2008, nov, 6);
high[18] = 54.0;
low[18] = 42.0;
open[18] = 50.0;
close[18] = 42.0;
volume[18] = 150.0;

date[19] = DateUtilities.createDate(2008, nov, 7);
high[19] = 48.0;
low[19] = 37.0;
open[19] = 37.0;
close[19] = 47.0;
volume[19] = 120.0;

date[20] = DateUtilities.createDate(2008, nov, 10);
high[20] = 58.0;
low[20] = 33.0;
open[20] = 39.0;
close[20] = 41.0;
volume[20] = 80.0;

date[21] = DateUtilities.createDate(2008, nov, 11);
high[21] = 47.0;
low[21] = 31.0;
open[21] = 36.0;
close[21] = 41.0;
volume[21] = 40.0;

return new DefaultHighLowDataset("Series 1", date, high, low, open,
close, volume);

}

/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}

/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
CandlestickChartDemo2 demo
= new CandlestickChartDemo2("Candlestick Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}

}

Locked