When I use it, the window that should have the chart is blank. If I don't use it, it works fine but all weekends and holidays appear.
I'm using a SegmentedTimeline and for the holidays I'm adding them to the exception list.
here's the code:
Code: Select all
/**
*
* @author Pedro Baptista
* @version 0.0
*
* Class dedicated to Draw the CandleStick chart
*/
@SuppressWarnings("serial")
public class YardChart extends JFrame {
private int _height = 800;
private int _width = 400;
private JFreeChart _chart;
private YardCalendar _calendar;
private static SegmentedTimeline _timeline;
public JFreeChart getChart() {
return _chart;
}
public YardChart(String market, String stockSymbol, String alarm) {
super("Yard");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Creates and imports the data into the calendar
_calendar = new YardCalendar(market);
_calendar.importASCII("/home/ciccio/CALENDAR/"+market.toUpperCase()+"/Calendar.txt");
_timeline = SegmentedTimeline.newMondayThroughFridayTimeline();
DateAxis domainAxis = new DateAxis("Date");
NumberAxis rangeAxis = new NumberAxis("Price");
CandlestickRenderer renderer = new CandlestickRenderer();
DefaultOHLCDataset dataset = getDataSet(stockSymbol, alarm);
//Do some setting up, see the API Doc
renderer.setSeriesPaint(0, Color.BLACK);
renderer.setDrawVolume(true);
rangeAxis.setAutoRangeIncludesZero(false);
domainAxis.setTimeline(_timeline);
XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);
//Now create the chart and chart panel
_chart = new JFreeChart(mainPlot);
ChartPanel chartPanel = new ChartPanel(_chart);
chartPanel.setPreferredSize(new Dimension(_height, _width));
setContentPane(chartPanel);
this.setTitle(alarm);
}
protected DefaultOHLCDataset getDataSet(String stockSymbol, String alarm) {
//This is the dataset we are going to create
DefaultOHLCDataset result = null;
//This is the data needed for the dataset
OHLCDataItem[] data = getData(stockSymbol,alarm);
//Create a dataset, an Open, High, Low, Close dataset
result = new DefaultOHLCDataset(stockSymbol, data);
return result;
}
protected OHLCDataItem[] getData(String stockSymbol, String alarm) {
List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>();
try {
String path = "/home/ciccio/SIMULAZIONI/"+stockSymbol+"/CARTELLE/DBINFO/"+alarm+"/";
FileInputStream candleValues = new FileInputStream(path+"GuiValue.txt");
FileInputStream dateValues = new FileInputStream(path+"Date.txt");
DataInputStream in = new DataInputStream(dateValues);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String startDate = br.readLine().substring(0, 6);
in.close();
//NOTE: the file year format is yy instead of yyyy, so 2000 is needed to be added
int startYear = 2000+Integer.parseInt(startDate.substring(0, 2));
int startMonth = Integer.parseInt(startDate.substring(2, 4));
int startDay = Integer.parseInt(startDate.substring(4, 6));
ListIterator<YardDay> itr = _calendar.findPosition(startYear, startMonth, startDay);
in = new DataInputStream(candleValues);
br = new BufferedReader(new InputStreamReader(in));
String inputLine;
br.readLine();
YardDay calendarDay = itr.next();
while ((inputLine = br.readLine()) != null) {
System.out.println("input:\t\t\t\t"+inputLine);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date date = formatter.parse(calendarDay.getYear()+"/"+calendarDay.getMonth()+"/"+calendarDay.getDay());
//skips Weekends
while(calendarDay.isWeekend()){
calendarDay = itr.next();
date = formatter.parse(calendarDay.getYear()+"/"+calendarDay.getMonth()+"/"+calendarDay.getDay());
}
//add holidays to the candle exception list
while(calendarDay.isHoliday()){
_timeline.addException(date);
calendarDay = itr.next();
date = formatter.parse(calendarDay.getYear()+"/"+calendarDay.getMonth()+"/"+calendarDay.getDay());
}
if(calendarDay.isWorkDay()){
System.out.println(date.toString()+":\t"+inputLine+"\n");
StringTokenizer st = new StringTokenizer(inputLine, " ");
NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
double open = nf.parse(st.nextToken()).doubleValue();
double high = nf.parse(st.nextToken()).doubleValue();
double low = nf.parse(st.nextToken()).doubleValue();
double close = nf.parse(st.nextToken()).doubleValue();
double volume = nf.parse(st.nextToken()).doubleValue();
OHLCDataItem item = new OHLCDataItem(date, open, high, low, close, volume);
dataItems.add(item);
calendarDay = itr.next();
}
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
//Convert the list into an array
OHLCDataItem[] data = dataItems.toArray(new OHLCDataItem[dataItems.size()]);
return data;
}
}
Can anyone help me to solve this?
Thanks
PS: using JFreeChart 1.0.14 and JCommon 1.0.17.