@Override public void addSeries(agentgui.ontology.DataSeries series){ org.jfree.data.time.TimeSeries newSeries = new org.jfree.data.time.TimeSeries(series.getLabel()); List valuePairs = ((agentgui.ontology.TimeSeries)series).getTimeSeriesValuePairs(); for (int i = 0; i < valuePairs.size(); i++) { TimeSeriesValuePair valuePair = (TimeSeriesValuePair) valuePairs.get(i); Simple_Long simpleLong = valuePair.getTimestamp(); Simple_Float simpleFloat = valuePair.getValue(); Long timeStampLong = simpleLong.getLongValue(); Float floatValue = simpleFloat.getFloatValue(); if (timeStampLong!=null) { TimeSeriesDataItem newItem = new TimeSeriesDataItem(new FixedMillisecond(timeStampLong), floatValue); newSeries.addOrUpdate(newItem); } } this.getTimeSeriesCollection().addSeries(newSeries); this.setChanged(); this.notifyObservers(ChartModel.EventType.SERIES_ADDED); }
/** * Update the time stamp in all series that contain it * @param oldKey The old time stamp * @param newKey The new time stamp */ public void updateKey(Number oldKey, Number newKey) { // --- Iterate over all series ------------------------------ for (int i=0; i < this.getSeriesCount(); i++) { org.jfree.data.time.TimeSeries series = this.getSeries(i); // Try to find a value pair with the old time stamp TimeSeriesDataItem oldValuePair = series.getDataItem(new FixedMillisecond(oldKey.longValue())); // If found, remove it and add a new one with the new time stamp and the old value if(oldValuePair != null){ series.delete(new FixedMillisecond(oldKey.longValue())); series.addOrUpdate(new FixedMillisecond(newKey.longValue()), oldValuePair.getValue()); } } }
/** * Edits the data series by adding data. * @param series the series * @param targetDataSeriesIndex the target data series index */ public void editSeriesAddData(DataSeries series, int targetDataSeriesIndex) throws NoSuchSeriesException { if (targetDataSeriesIndex<=(this.getSeriesCount()-1)) { // --- Get the series ------------------------- org.jfree.data.time.TimeSeries addToSeries = (org.jfree.data.time.TimeSeries) this.getSeries(targetDataSeriesIndex); List valuePairs = ((agentgui.ontology.TimeSeries)series).getTimeSeriesValuePairs(); for (int i = 0; i < valuePairs.size(); i++) { TimeSeriesValuePair valuePair = (TimeSeriesValuePair) valuePairs.get(i); Simple_Long simpleLong = valuePair.getTimestamp(); Simple_Float simpleFloat = valuePair.getValue(); Long timeStampLong = simpleLong.getLongValue(); Float floatValue = simpleFloat.getFloatValue(); if (timeStampLong!=null) { TimeSeriesDataItem newItem = new TimeSeriesDataItem(new FixedMillisecond(timeStampLong), floatValue); addToSeries.addOrUpdate(newItem); } } } else { throw new NoSuchSeriesException(); } }
/** * Edits the data series by adding or exchanging data. * @param series the series * @param targetDataSeriesIndex the target data series index */ public void editSeriesAddOrExchangeData(DataSeries series, int targetDataSeriesIndex) throws NoSuchSeriesException { if (targetDataSeriesIndex<=(this.getSeriesCount()-1)) { org.jfree.data.time.TimeSeries addToSeries = (org.jfree.data.time.TimeSeries) this.getSeries(targetDataSeriesIndex); List valuePairs = ((agentgui.ontology.TimeSeries)series).getTimeSeriesValuePairs(); for (int i = 0; i < valuePairs.size(); i++) { TimeSeriesValuePair valuePair = (TimeSeriesValuePair) valuePairs.get(i); Simple_Long simpleLong = valuePair.getTimestamp(); Simple_Float simpleFloat = valuePair.getValue(); Long timeStampLong = simpleLong.getLongValue(); Float floatValue = simpleFloat.getFloatValue(); if (timeStampLong!=null) { TimeSeriesDataItem newItem = new TimeSeriesDataItem(new FixedMillisecond(timeStampLong), floatValue); addToSeries.addOrUpdate(newItem); } } } else { throw new NoSuchSeriesException(); } }
private int getDataIndex(TimeSeries timeSeries, Day targetDay) { int low = 0; int high = timeSeries.getItemCount() - 1; while (low <= high) { int mid = (low + high) >>> 1; final TimeSeriesDataItem timeSeriesDataItem = timeSeries.getDataItem(mid); final Day searchDay = (Day)timeSeriesDataItem.getPeriod(); final long cmp = searchDay.compareTo(targetDay); if (cmp < 0) { low = mid + 1; } else if (cmp > 0) { high = mid - 1; } else { return mid; } } return -1; }
/** * Test the setMaximumItemCount() method to ensure that it removes items from the series * if necessary. */ public void testSetMaximumItemCount() { TimeSeries s1 = new TimeSeries("S1", Year.class); s1.add(new Year(2000), 13.75); s1.add(new Year(2001), 11.90); s1.add(new Year(2002), null); s1.add(new Year(2005), 19.32); s1.add(new Year(2007), 16.89); assertTrue(s1.getItemCount() == 5); s1.setMaximumItemCount(3); assertTrue(s1.getItemCount() == 3); TimeSeriesDataItem item = s1.getDataItem(0); assertTrue(item.getPeriod().equals(new Year(2002))); }
/** * Test the equals() method. */ public void testEquals() { TimeSeriesDataItem item1 = new TimeSeriesDataItem( new Day(23, 9, 2001), 99.7 ); TimeSeriesDataItem item2 = new TimeSeriesDataItem( new Day(23, 9, 2001), 99.7 ); assertTrue(item1.equals(item2)); assertTrue(item2.equals(item1)); item1.setValue(new Integer(5)); assertFalse(item1.equals(item2)); item2.setValue(new Integer(5)); assertTrue(item1.equals(item2)); }
/** * Test the setMaximumItemCount() method to ensure that it removes items * from the series if necessary. */ public void testSetMaximumItemCount() { TimeSeries s1 = new TimeSeries("S1", Year.class); s1.add(new Year(2000), 13.75); s1.add(new Year(2001), 11.90); s1.add(new Year(2002), null); s1.add(new Year(2005), 19.32); s1.add(new Year(2007), 16.89); assertTrue(s1.getItemCount() == 5); s1.setMaximumItemCount(3); assertTrue(s1.getItemCount() == 3); TimeSeriesDataItem item = s1.getDataItem(0); assertTrue(item.getPeriod().equals(new Year(2002))); }
public void add(final CalendarDateSeries<?> aSeries) { final String tmpName = aSeries.getName(); final TimeSeries tmpSeries = new TimeSeries(tmpName); for (final Entry<CalendarDate, ? extends Number> tmpEntry : aSeries.entrySet()) { final CalendarDate tmpKey = tmpEntry.getKey(); final FixedMillisecond tmpPeriod = new FixedMillisecond(tmpKey.millis); final Number tmpValue = tmpEntry.getValue(); final TimeSeriesDataItem tmpItem = new TimeSeriesDataItem(tmpPeriod, tmpValue); tmpSeries.add(tmpItem); } myCollection.addSeries(tmpSeries); ColourData tmpColour2 = aSeries.getColour(); if (tmpColour2 == null) { tmpColour2 = ColourData.random(); aSeries.colour(tmpColour2); } final int tmpRgb = tmpColour2.getRGB(); final Color tmpColour = new Color(tmpRgb); this.putColour(tmpName, tmpColour); }
/** * Outputs the given DataPoint to the current TimeSeries. * @param dataPoint the DataPoint to output to the current TimeSeries. */ private void output( DataPoint dataPoint, String timeVariable ) throws InstantiationException, IllegalAccessException, InvocationTargetException, InstantiationException { long timeValue = (long)dataPoint.getIndependentValue(timeVariable); Object[] args = new Object[1]; args[0] = new Date( timeValue ); RegularTimePeriod period = (RegularTimePeriod)timePeriodConstructor.newInstance(args); double value = dataPoint.getDependentValue(); timeSeries.add( new TimeSeriesDataItem(period,value) ); }
/** * A helper function to convert data points (from startIndex to * endIndex) of a (JFreeChart) TimeSeries object into an * OpenForecast DataSet. * @param series the series of data points stored as a JFreeChart * TimeSeries object. * @param startIndex the index of the first data point required from the * series. * @param endIndex the index of the last data point required from the * series. * @return an OpenForecast DataSet representing the data points extracted * from the TimeSeries. */ private DataSet getDataSet( TimeSeries series, int startIndex, int endIndex ) { DataSet dataSet = new DataSet(); if ( endIndex > series.getItemCount() ) endIndex = series.getItemCount(); for ( int i=startIndex; i<endIndex; i++ ) { TimeSeriesDataItem dataPair = series.getDataItem(i); DataPoint dp = new Observation( dataPair.getValue().doubleValue() ); dp.setIndependentValue( "t", i ); dataSet.add( dp ); } return dataSet; }
/** * Test the setMaximumItemCount() method to ensure that it removes items * from the series if necessary. */ public void testSetMaximumItemCount() { TimeSeries s1 = new TimeSeries("S1"); s1.add(new Year(2000), 13.75); s1.add(new Year(2001), 11.90); s1.add(new Year(2002), null); s1.add(new Year(2005), 19.32); s1.add(new Year(2007), 16.89); assertTrue(s1.getItemCount() == 5); s1.setMaximumItemCount(3); assertTrue(s1.getItemCount() == 3); TimeSeriesDataItem item = s1.getDataItem(0); assertTrue(item.getPeriod().equals(new Year(2002))); assertEquals(16.89, s1.getMinY(), EPSILON); assertEquals(19.32, s1.getMaxY(), EPSILON); }
/** * Some more checks for the addOrUpdate() method. */ public void testAddOrUpdate4() { TimeSeries ts = new TimeSeries("S"); TimeSeriesDataItem overwritten = ts.addOrUpdate(new Year(2009), 20.09); assertNull(overwritten); overwritten = ts.addOrUpdate(new Year(2009), 1.0); assertEquals(new Double(20.09), overwritten.getValue()); assertEquals(new Double(1.0), ts.getValue(new Year(2009))); // changing the overwritten record shouldn't affect the series overwritten.setValue(null); assertEquals(new Double(1.0), ts.getValue(new Year(2009))); TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2010), 20.10); overwritten = ts.addOrUpdate(item); assertNull(overwritten); assertEquals(new Double(20.10), ts.getValue(new Year(2010))); // changing the item that was added should not change the series item.setValue(null); assertEquals(new Double(20.10), ts.getValue(new Year(2010))); }
@Override public TimeSeries buildBarTimeSeries(String serieName, SortedMap<Date, BarChart> barSerie, TimeSeries lineSerie) { TimeSeries timeSerie = new TimeSeries(serieName); int cpt = 0; //int gap = Math.max(1, barSerie.size()/208); int gap = 1; for (Date date : barSerie.keySet()) { if (cpt % gap == 0) { RegularTimePeriod period = new Day(date); Number value = barSerie.get(date).getValue(); TimeSeriesDataItem item = new TimeSeriesDataItem(period, value); timeSerie.add(item, false); } cpt++; } return timeSerie; }
private TimeSeriesCollection buildDataSet(SortedMap<DataSetBarDescr, SortedMap<Date,Double>> series, AbstractXYItemRenderer renderer) { TimeSeriesCollection dataset = new TimeSeriesCollection(); int seriesIdx = 0; for (DataSetBarDescr serieDef : series.keySet()) { TimeSeries timeSerie = new TimeSeries(serieDef.getSerieName()); SortedMap<Date, Double> serie = series.get(serieDef); for (Date date : serie.keySet()) { RegularTimePeriod period = new Day(date); Number value = serie.get(date); TimeSeriesDataItem item = new TimeSeriesDataItem(period, value); timeSerie.add(item, false); } dataset.addSeries(timeSerie); renderer.setSeriesPaint(seriesIdx, serieDef.getSerieColor()); renderer.setSeriesFillPaint(seriesIdx, serieDef.getSerieColor()); renderer.setSeriesStroke(seriesIdx, new BasicStroke(serieDef.getSerieStrokeSize())); seriesIdx++; } return dataset; }
private TimeSeries computeSingleTimeSeries(final List<Band> bandList, int pixelX, int pixelY, int currentLevel, String positionName) { final Band firstBand = bandList.get(0); final String firstBandName = firstBand.getName(); final int lastUnderscore = firstBandName.lastIndexOf("_"); final String suffix = positionName.isEmpty()?positionName: "_" + positionName; final String timeSeriesName = firstBandName.substring(0, lastUnderscore); final TimeSeries timeSeries = new TimeSeries(timeSeriesName + suffix); for (Band band : bandList) { final TimeCoding timeCoding = this.timeSeries.getRasterTimeMap().get(band); if (timeCoding != null) { final ProductData.UTC startTime = timeCoding.getStartTime(); final Millisecond timePeriod = new Millisecond(startTime.getAsDate(), ProductData.UTC.UTC_TIME_ZONE, Locale.getDefault()); final double value = getValue(band, pixelX, pixelY, currentLevel); timeSeries.add(new TimeSeriesDataItem(timePeriod, value)); } } return timeSeries; }
@SuppressWarnings("unchecked") @Override protected String getCsv(final JFreeContext ctx) { return Csv.write(new CsvWriter() { @Override public void write(CsvListWriter csv) throws IOException { final DateFormat yearOnly = new SimpleDateFormat("YYYY"); csv.write("Year", "Outbreaks"); final List<TimeSeriesDataItem> items = ((ATSCollection)ctx.dataset()).getSeries(0) .getItems(); for (TimeSeriesDataItem i : items) { csv.write(yearOnly.format(i.getPeriod().getStart()), i.getValue() .intValue() + ""); } }}); }
@Override public void exchangeSeries(int seriesIndex, DataSeries series) throws NoSuchSeriesException { if(seriesIndex < this.getSeriesCount()){ // --- edit series --- org.jfree.data.time.TimeSeries editSeries = (org.jfree.data.time.TimeSeries) this.getSeries(seriesIndex); editSeries.clear(); if (series.getLabel()!=null) { editSeries.setKey(series.getLabel()); } List valuePairs = ((agentgui.ontology.TimeSeries)series).getTimeSeriesValuePairs(); for (int i = 0; i < valuePairs.size(); i++) { TimeSeriesValuePair valuePair = (TimeSeriesValuePair) valuePairs.get(i); Simple_Long simpleLong = valuePair.getTimestamp(); Simple_Float simpleFloat = valuePair.getValue(); Long timeStampLong = simpleLong.getLongValue(); Float floatValue = simpleFloat.getFloatValue(); if (timeStampLong!=null) { TimeSeriesDataItem newItem = new TimeSeriesDataItem(new FixedMillisecond(timeStampLong), floatValue); editSeries.addOrUpdate(newItem); } } } else { throw new NoSuchSeriesException(); } this.setChanged(); this.notifyObservers(ChartModel.EventType.SERIES_EXCHANGED); }
/** * Test the equals() method. */ public void testEquals() { final TimeSeriesDataItem item1 = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7); final TimeSeriesDataItem item2 = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7); assertTrue(item1.equals(item2)); assertTrue(item2.equals(item1)); item1.setValue(new Integer(5)); assertFalse(item1.equals(item2)); item2.setValue(new Integer(5)); assertTrue(item1.equals(item2)); }
/** * Test that an instance is equal to itself. * * SourceForge Bug ID: 558850. */ public void testEqualsSelf() { TimeSeriesDataItem item = new TimeSeriesDataItem( new Day(23, 9, 2001), 99.7 ); assertTrue(item.equals(item)); }
private void processSystemStr(String tsKey, List<String> fieldNameList, List<String> valueStrList, Date logEntryDate, Map<String, List<TimeSeriesDataItem>> log4jMessageMap, Map<String, List<Double>> log4jMessageDataMap) { int fieldIndex = fieldNameList.indexOf(tsKey); if (fieldIndex != -1) { DateFormat modelDateFormat = getModelDateFormat(); Locale locale = getLocale(); NumberFormat numberFormat = getNumberFormat(); TimeZone timeZone = modelDateFormat.getTimeZone(); String valueStr = null; try { valueStr = valueStrList.get(fieldIndex); double value = numberFormat.parse(valueStr).doubleValue(); addToMap(log4jMessageMap, tsKey, logEntryDate, timeZone, locale, value); List<Double> log4jMessageDataList = log4jMessageDataMap.get(tsKey); if (log4jMessageDataList == null) { log4jMessageDataList = new LinkedList<Double>(); log4jMessageDataMap.put(tsKey, log4jMessageDataList); } log4jMessageDataList.add(value); } catch (Exception e) { LOG.error("Error adding to map:" + valueStr, e); } } else { LOG.error("could not find key" + tsKey); } }
/** * Builds a DataPoint from the given TimeSeriesDataItem. The name used for * the independent, time variable can be changed using the setTimeVariable * method. * @param dataItem the TimeSeriesDataItem from which the values are to be * read and used to construct a new DataPoint. * @return a DataPoint object with values as specified by the given * TimeSeriesDataItem. */ private DataPoint build( TimeSeriesDataItem dataItem ) { DataPoint dataPoint = new Observation( dataItem.getValue().doubleValue() ); // Get time value (at middle of time period) double timeValue = dataItem.getPeriod().getMiddleMillisecond(); // Store time value as independent variable dataPoint.setIndependentValue( getVariableName(0), timeValue ); return dataPoint; }
@Override public List getItems() { return new AbstractList<TimeSeriesDataItem>() { @Override public TimeSeriesDataItem get(int index) { return getDataItem(index); } @Override public int size() { return getItemCount(); } }; }
/** * Create a TimeSeriesDataItem, add it to a TimeSeries. Now, modifying * the original TimeSeriesDataItem should NOT affect the TimeSeries. */ public void testAdd_TimeSeriesDataItem() { TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2009), 1.0); TimeSeries series = new TimeSeries("S1"); series.add(item); assertTrue(item.equals(series.getDataItem(0))); item.setValue(new Double(99.9)); assertFalse(item.equals(series.getDataItem(0))); }
/** Create the dataset used for the plot. */ private TimeTableXYDataset createDataset() { TimeTableXYDataset dataset = new TimeTableXYDataset(); for (SeriesDescriptor seriesDesriptor : seriesList) { String seriesTitle = seriesDesriptor.seriesTitle; TimeSeries timeSeries = createTimeSeries(seriesTitle, seriesDesriptor.series); for (int i = 0; i < timeSeries.getItemCount(); ++i) { TimeSeriesDataItem di = timeSeries.getDataItem(i); dataset.add(di.getPeriod(), di.getValue().doubleValue(), seriesTitle); } } return dataset; }
@Override public TimeSeries buildBarTimeSeries(String serieName, SortedMap<Date, BarChart> barSerie, TimeSeries lineSerie) { TimeSeries timeSerie = new TimeSeries(serieName); Number prevLineValue = null; int cpt = 0; int gap = Math.max(1, barSerie.size()/208); for (Date date : barSerie.keySet()) { if (cpt % gap == 0) { RegularTimePeriod period = new Day(date); Number lineValue = lineSerie.getValue(period); if (lineValue != null) { prevLineValue = lineValue; } if (prevLineValue !=null) { Number value = barSerie.get(date).getValue(); value = value.doubleValue() * prevLineValue.doubleValue(); TimeSeriesDataItem item = new TimeSeriesDataItem(period, value); timeSerie.add(item, false); } } cpt++; } return timeSerie; }
@Override public TimeSeries validate(TimeSeries timeSeries, String sourceName, TimeSeriesType type) throws ParseException { String qualifiedSourceName = createQualifiedSourcename(sourceName, type); final Symbol symbol = namespace.resolveSymbol(qualifiedSourceName); if (symbol == null) { throw new ParseException("No variable for identifier '" + qualifiedSourceName + "' registered."); } final String expression = getExpressionFor(qualifiedSourceName); if (expression == null || expression.trim().isEmpty()) { return timeSeries; } final Variable variable = (Variable) symbol; final Term term = parser.parse(expression, namespace); final int seriesCount = timeSeries.getItemCount(); final TimeSeries validatedSeries = new TimeSeries(timeSeries.getKey()); for (int i = 0; i < seriesCount; i++) { final TimeSeriesDataItem dataItem = timeSeries.getDataItem(i); final Number value = dataItem.getValue(); variable.assignD(null, value.doubleValue()); if (term.evalB(null)) { validatedSeries.add(dataItem); } } return validatedSeries; }
public static TimeSeries createDerivative(TimeSeries series, long miliseconds) { TimeSeries result = new TimeSeries(series.getKey().toString(), series.getTimePeriodClass()); List<TimeSeriesDataItem> dataItems = series.getItems(); int i = 0; do { TimeSeriesDataItem now = dataItems.get(i); long then = now.getPeriod().getSerialIndex() + miliseconds; int j = i + 1; while (j < dataItems.size() && dataItems.get(j).getPeriod().getSerialIndex() < then) { j++; } if (j == dataItems.size()) { j = dataItems.size() - 1; } TimeSeriesDataItem future = dataItems.get(j); double derivative = 1000.0 * (future.getValue().doubleValue() - now.getValue().doubleValue()); derivative /= future.getPeriod().getSerialIndex() - now.getPeriod().getSerialIndex(); result.add(now.getPeriod(), derivative); i++; } while (i < dataItems.size()); return result; }
@Override public void add(TimeSeriesDataItem arg0, boolean arg1) { throw new UnsupportedOperationException(); }
@Override public void add(TimeSeriesDataItem item) { throw new UnsupportedOperationException(); }
@Override public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period, double value) { throw new UnsupportedOperationException(); }
@Override public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period, Number value) { throw new UnsupportedOperationException(); }