source: trunk/src/org/expeditee/items/widgets/charts/TimeSeries.java@ 220

Last change on this file since 220 was 220, checked in by ra33, 16 years ago
File size: 6.3 KB
Line 
1package org.expeditee.items.widgets.charts;
2
3import java.awt.Color;
4import java.text.DateFormat;
5import java.text.ParseException;
6import java.text.SimpleDateFormat;
7import java.util.Calendar;
8import java.util.Collection;
9import java.util.Date;
10
11import org.expeditee.gui.AttributeUtils;
12import org.expeditee.gui.AttributeValuePair;
13import org.expeditee.gui.Frame;
14import org.expeditee.gui.FrameKeyboardActions;
15import org.expeditee.gui.MessageBay;
16import org.expeditee.items.Text;
17import org.jfree.chart.ChartFactory;
18import org.jfree.chart.JFreeChart;
19import org.jfree.data.time.Day;
20import org.jfree.data.time.Hour;
21import org.jfree.data.time.Millisecond;
22import org.jfree.data.time.Minute;
23import org.jfree.data.time.Month;
24import org.jfree.data.time.Quarter;
25import org.jfree.data.time.RegularTimePeriod;
26import org.jfree.data.time.Second;
27import org.jfree.data.time.TimeSeriesCollection;
28import org.jfree.data.time.Week;
29import org.jfree.data.time.Year;
30import org.jfree.data.xy.XYDataset;
31
32public class TimeSeries extends AbstractValueAxis {
33
34 public TimeSeries(Text source, String[] args) {
35 super(source, args);
36 }
37
38 private TimeSeriesCollection _data;
39
40 private Class<? extends RegularTimePeriod> _periodType;
41
42 private Date _startDate;
43
44 protected XYDataset getChartData() {
45 if (_data == null)
46 _data = new TimeSeriesCollection();
47 return _data;
48 }
49
50 @Override
51 public void refreshData(Frame dataFrame) {
52 String period = dataFrame.getAnnotationValue("period");
53 if (period == null) {
54 period = "day";
55 } else {
56 period = period.toLowerCase();
57 }
58 if (period.startsWith("quarter")) {
59 _periodType = Quarter.class;
60 } else if (period.startsWith("year")) {
61 _periodType = Year.class;
62 } else if (period.startsWith("month")) {
63 _periodType = Month.class;
64 } else if (period.startsWith("week")) {
65 _periodType = Week.class;
66 } else if (period.startsWith("hour")) {
67 _periodType = Hour.class;
68 } else if (period.startsWith("min")) {
69 _periodType = Minute.class;
70 } else if (period.startsWith("sec")) {
71 _periodType = Second.class;
72 } else if (period.startsWith("milli")) {
73 _periodType = Millisecond.class;
74 } else if (period.startsWith("day")) {
75 _periodType = Day.class;
76 } else {
77 MessageBay.errorMessage("Invalid time series period type: "
78 + period);
79 _periodType = Day.class;
80 }
81
82 try {
83 String startDateString = dataFrame.getAnnotationValue("start");
84 if (startDateString != null) {
85 _startDate = parseDate(startDateString);
86 }
87 } catch (Exception e) {
88 // Use the current date
89 }
90 if (_startDate == null) {
91 _startDate = new Date();
92 }
93 super.refreshData(dataFrame);
94 }
95
96 /**
97 * @param dataFrame
98 */
99 @Override
100 protected void clearData() {
101 _data.removeAllSeries();
102 }
103
104 @Override
105 protected JFreeChart createNewChart() {
106 return ChartFactory.createTimeSeriesChart(DEFAULT_TITLE, DEFAULT_XAXIS,
107 DEFAULT_YAXIS, getChartData(), true, // legend?
108 true, // tooltips?
109 false // URLs?
110 );
111 }
112
113 @Override
114 protected boolean addCategoryData(String categoryName,
115 Collection<Text> items, boolean swap) {
116 org.jfree.data.time.TimeSeries newSeries = new org.jfree.data.time.TimeSeries(
117 categoryName, _periodType);
118
119 boolean foundData = false;
120 Color newColor = null;
121 for (Text i : items) {
122 if (!i.isLineEnd()) {
123 Text t = (Text) i;
124 AttributeValuePair avp = AttributeUtils.getPair(t.getText());
125 if (avp != null) {
126 Double attribute = avp.getDoubleAttribute();
127 Double value = avp.getDoubleValue();
128 // If the data is not valid move to the next item
129 if (value == null)
130 continue;
131 try {
132 RegularTimePeriod rtp = null;
133 if (attribute == null) {
134 Date date = parseDate(avp.getAttribute());
135 rtp = _periodType.getConstructor(
136 new Class[] { Date.class }).newInstance(
137 new Object[] { date });
138 } else {
139 if (_periodType.equals(Year.class)) {
140 int year = (int) Math.floor(attribute);
141 Calendar c = Calendar.getInstance();
142 c.setTime(_startDate);
143 rtp = new Year(year + c.get(Calendar.YEAR));
144 } else if (_periodType.equals(Quarter.class)) {
145 int quarter = (int) Math.floor(attribute);
146 rtp = new Quarter(quarter, new Year(_startDate));
147 } else if (_periodType.equals(Month.class)) {
148 int month = (int) Math.floor(attribute);
149 rtp = new Month(month, new Year(_startDate));
150 } else if (_periodType.equals(Week.class)) {
151 int week = (int) Math.floor(attribute);
152 rtp = new Week(week, new Year(_startDate));
153 } else if (_periodType.equals(Day.class)) {
154 int day = (int) Math.floor(attribute);
155 Calendar c = Calendar.getInstance();
156 c.setTime(_startDate);
157 rtp = new Day(day
158 + c.get(Calendar.DAY_OF_MONTH), 1 + c
159 .get(Calendar.MONTH), c
160 .get(Calendar.YEAR));
161 } else if (_periodType.equals(Hour.class)) {
162 int hour = (int) Math.floor(attribute);
163 rtp = new Hour(hour, new Day(_startDate));
164 } else if (_periodType.equals(Minute.class)) {
165 int minute = (int) Math.floor(attribute);
166 rtp = new Minute(minute, new Hour(_startDate));
167 } else if (_periodType.equals(Second.class)) {
168 int second = (int) Math.floor(attribute);
169 rtp = new Second(second, new Minute(_startDate));
170 } else if (_periodType.equals(Millisecond.class)) {
171 int milli = (int) Math.floor(attribute);
172 rtp = new Millisecond(milli, new Second(
173 _startDate));
174 }
175 }
176 newSeries.add(rtp, value);
177 foundData = true;
178 if(newColor == null)
179 newColor = i.getColor();
180 } catch (Exception e) {
181 // Ignore the data point if it cant be parsed
182 e.printStackTrace();
183 }
184 }
185 }
186 }
187 if (foundData) {
188 _data.addSeries(newSeries);
189 _paints.put(categoryName, newColor);
190 }
191 return foundData;
192 }
193
194 /**
195 * @param avp
196 * @return
197 * @throws ParseException
198 */
199 private Date parseDate(String dateString) throws ParseException {
200 // Convert the attribute into a date
201 DateFormat df = null;
202 if (dateString.length() > FrameKeyboardActions.SHORT_DATE_FORMAT
203 .length()) {
204 df = new SimpleDateFormat(FrameKeyboardActions.LONG_DATE_FORMAT);
205 } else {
206 df = new SimpleDateFormat(FrameKeyboardActions.SHORT_DATE_FORMAT);
207 }
208 Date date = df.parse(dateString);
209 return date;
210 }
211}
Note: See TracBrowser for help on using the repository browser.