[213] | 1 | package org.expeditee.items.widgets.charts;
|
---|
| 2 |
|
---|
[220] | 3 | import java.awt.Color;
|
---|
| 4 | import java.text.DateFormat;
|
---|
| 5 | import java.text.ParseException;
|
---|
| 6 | import java.text.SimpleDateFormat;
|
---|
| 7 | import java.util.Calendar;
|
---|
| 8 | import java.util.Collection;
|
---|
| 9 | import java.util.Date;
|
---|
| 10 |
|
---|
| 11 | import org.expeditee.gui.AttributeUtils;
|
---|
| 12 | import org.expeditee.gui.AttributeValuePair;
|
---|
| 13 | import org.expeditee.gui.Frame;
|
---|
| 14 | import org.expeditee.gui.FrameKeyboardActions;
|
---|
| 15 | import org.expeditee.gui.MessageBay;
|
---|
[213] | 16 | import org.expeditee.items.Text;
|
---|
| 17 | import org.jfree.chart.ChartFactory;
|
---|
| 18 | import org.jfree.chart.JFreeChart;
|
---|
[220] | 19 | import org.jfree.data.time.Day;
|
---|
| 20 | import org.jfree.data.time.Hour;
|
---|
| 21 | import org.jfree.data.time.Millisecond;
|
---|
| 22 | import org.jfree.data.time.Minute;
|
---|
| 23 | import org.jfree.data.time.Month;
|
---|
| 24 | import org.jfree.data.time.Quarter;
|
---|
| 25 | import org.jfree.data.time.RegularTimePeriod;
|
---|
| 26 | import org.jfree.data.time.Second;
|
---|
| 27 | import org.jfree.data.time.TimeSeriesCollection;
|
---|
| 28 | import org.jfree.data.time.Week;
|
---|
| 29 | import org.jfree.data.time.Year;
|
---|
| 30 | import org.jfree.data.xy.XYDataset;
|
---|
[213] | 31 |
|
---|
[220] | 32 | public class TimeSeries extends AbstractValueAxis {
|
---|
[213] | 33 |
|
---|
| 34 | public TimeSeries(Text source, String[] args) {
|
---|
| 35 | super(source, args);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[220] | 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 |
|
---|
[213] | 50 | @Override
|
---|
[220] | 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
|
---|
[213] | 105 | protected JFreeChart createNewChart() {
|
---|
[220] | 106 | return ChartFactory.createTimeSeriesChart(DEFAULT_TITLE, DEFAULT_XAXIS,
|
---|
| 107 | DEFAULT_YAXIS, getChartData(), true, // legend?
|
---|
[213] | 108 | true, // tooltips?
|
---|
| 109 | false // URLs?
|
---|
| 110 | );
|
---|
| 111 | }
|
---|
[220] | 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;
|
---|
[235] | 124 | AttributeValuePair avp = new AttributeValuePair(t.getText());
|
---|
[220] | 125 | if (avp != null) {
|
---|
[247] | 126 | Double attribute = null;
|
---|
| 127 | try {
|
---|
| 128 | attribute = avp.getDoubleAttribute();
|
---|
| 129 | } catch (NumberFormatException e) {
|
---|
| 130 | }
|
---|
| 131 | Double value = null;
|
---|
| 132 | try {
|
---|
| 133 | // If the data is not valid move to the next item
|
---|
| 134 | value = avp.getDoubleValue();
|
---|
| 135 | } catch (NumberFormatException e) {
|
---|
[220] | 136 | continue;
|
---|
[247] | 137 | }
|
---|
| 138 |
|
---|
[220] | 139 | try {
|
---|
| 140 | RegularTimePeriod rtp = null;
|
---|
| 141 | if (attribute == null) {
|
---|
| 142 | Date date = parseDate(avp.getAttribute());
|
---|
| 143 | rtp = _periodType.getConstructor(
|
---|
| 144 | new Class[] { Date.class }).newInstance(
|
---|
| 145 | new Object[] { date });
|
---|
| 146 | } else {
|
---|
| 147 | if (_periodType.equals(Year.class)) {
|
---|
| 148 | int year = (int) Math.floor(attribute);
|
---|
| 149 | Calendar c = Calendar.getInstance();
|
---|
| 150 | c.setTime(_startDate);
|
---|
| 151 | rtp = new Year(year + c.get(Calendar.YEAR));
|
---|
| 152 | } else if (_periodType.equals(Quarter.class)) {
|
---|
| 153 | int quarter = (int) Math.floor(attribute);
|
---|
| 154 | rtp = new Quarter(quarter, new Year(_startDate));
|
---|
| 155 | } else if (_periodType.equals(Month.class)) {
|
---|
| 156 | int month = (int) Math.floor(attribute);
|
---|
| 157 | rtp = new Month(month, new Year(_startDate));
|
---|
| 158 | } else if (_periodType.equals(Week.class)) {
|
---|
| 159 | int week = (int) Math.floor(attribute);
|
---|
| 160 | rtp = new Week(week, new Year(_startDate));
|
---|
| 161 | } else if (_periodType.equals(Day.class)) {
|
---|
| 162 | int day = (int) Math.floor(attribute);
|
---|
| 163 | Calendar c = Calendar.getInstance();
|
---|
| 164 | c.setTime(_startDate);
|
---|
| 165 | rtp = new Day(day
|
---|
| 166 | + c.get(Calendar.DAY_OF_MONTH), 1 + c
|
---|
| 167 | .get(Calendar.MONTH), c
|
---|
| 168 | .get(Calendar.YEAR));
|
---|
| 169 | } else if (_periodType.equals(Hour.class)) {
|
---|
| 170 | int hour = (int) Math.floor(attribute);
|
---|
| 171 | rtp = new Hour(hour, new Day(_startDate));
|
---|
| 172 | } else if (_periodType.equals(Minute.class)) {
|
---|
| 173 | int minute = (int) Math.floor(attribute);
|
---|
| 174 | rtp = new Minute(minute, new Hour(_startDate));
|
---|
| 175 | } else if (_periodType.equals(Second.class)) {
|
---|
| 176 | int second = (int) Math.floor(attribute);
|
---|
| 177 | rtp = new Second(second, new Minute(_startDate));
|
---|
| 178 | } else if (_periodType.equals(Millisecond.class)) {
|
---|
| 179 | int milli = (int) Math.floor(attribute);
|
---|
| 180 | rtp = new Millisecond(milli, new Second(
|
---|
| 181 | _startDate));
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | newSeries.add(rtp, value);
|
---|
| 185 | foundData = true;
|
---|
[247] | 186 | if (newColor == null)
|
---|
[220] | 187 | newColor = i.getColor();
|
---|
| 188 | } catch (Exception e) {
|
---|
| 189 | // Ignore the data point if it cant be parsed
|
---|
| 190 | e.printStackTrace();
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | if (foundData) {
|
---|
| 196 | _data.addSeries(newSeries);
|
---|
| 197 | _paints.put(categoryName, newColor);
|
---|
| 198 | }
|
---|
| 199 | return foundData;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
| 203 | * @param avp
|
---|
| 204 | * @return
|
---|
| 205 | * @throws ParseException
|
---|
| 206 | */
|
---|
[284] | 207 | public static Date parseDate(String dateString) throws ParseException {
|
---|
[220] | 208 | // Convert the attribute into a date
|
---|
| 209 | DateFormat df = null;
|
---|
| 210 | if (dateString.length() > FrameKeyboardActions.SHORT_DATE_FORMAT
|
---|
| 211 | .length()) {
|
---|
| 212 | df = new SimpleDateFormat(FrameKeyboardActions.LONG_DATE_FORMAT);
|
---|
| 213 | } else {
|
---|
| 214 | df = new SimpleDateFormat(FrameKeyboardActions.SHORT_DATE_FORMAT);
|
---|
| 215 | }
|
---|
| 216 | Date date = df.parse(dateString);
|
---|
| 217 | return date;
|
---|
| 218 | }
|
---|
[213] | 219 | }
|
---|