List of usage examples for org.jfree.data.time RegularTimePeriod getSerialIndex
public abstract long getSerialIndex();
From source file:org.jfree.data.time.MovingAverage.java
/** * Creates a new {@link TimeSeries} containing moving average values for * the given series. If the series is empty (contains zero items), the * result is an empty series./*from ww w . ja va 2 s.c om*/ * * @param source the source series. * @param name the name of the new series. * @param periodCount the number of periods used in the average * calculation. * @param skip the number of initial periods to skip. * * @return The moving average series. */ public static TimeSeries createMovingAverage(TimeSeries source, String name, int periodCount, int skip) { ParamChecks.nullNotPermitted(source, "source"); if (periodCount < 1) { throw new IllegalArgumentException("periodCount must be greater " + "than or equal to 1."); } TimeSeries result = new TimeSeries(name); if (source.getItemCount() > 0) { // if the initial averaging period is to be excluded, then // calculate the index of the // first data item to have an average calculated... long firstSerial = source.getTimePeriod(0).getSerialIndex() + skip; for (int i = source.getItemCount() - 1; i >= 0; i--) { // get the current data item... RegularTimePeriod period = source.getTimePeriod(i); long serial = period.getSerialIndex(); if (serial >= firstSerial) { // work out the average for the earlier values... int n = 0; double sum = 0.0; long serialLimit = period.getSerialIndex() - periodCount; int offset = 0; boolean finished = false; while ((offset < periodCount) && (!finished)) { if ((i - offset) >= 0) { TimeSeriesDataItem item = source.getRawDataItem(i - offset); RegularTimePeriod p = item.getPeriod(); Number v = item.getValue(); long currentIndex = p.getSerialIndex(); if (currentIndex > serialLimit) { if (v != null) { sum = sum + v.doubleValue(); n = n + 1; } } else { finished = true; } } offset = offset + 1; } if (n > 0) { result.add(period, sum / n); } else { result.add(period, null); } } } } return result; }
From source file:org.jfree.data.time.TimeSeries.java
/** * Age items in the series. Ensure that the timespan from the supplied * time to the oldest record in the series does not exceed history count. * oldest items will be removed if required. * * @param latest the time to be compared against when aging data * (specified in milliseconds).//from w w w .j a va 2 s . co m * @param notify controls whether or not a {@link SeriesChangeEvent} is * sent to registered listeners IF any items are removed. */ public void removeAgedItems(long latest, boolean notify) { if (this.data.isEmpty()) { return; // nothing to do } // find the serial index of the period specified by 'latest' long index = Long.MAX_VALUE; try { Method m = RegularTimePeriod.class.getDeclaredMethod("createInstance", new Class[] { Class.class, Date.class, TimeZone.class }); RegularTimePeriod newest = (RegularTimePeriod) m.invoke(this.timePeriodClass, new Object[] { this.timePeriodClass, new Date(latest), TimeZone.getDefault() }); index = newest.getSerialIndex(); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } // check if there are any values earlier than specified by the history // count... boolean removed = false; while (getItemCount() > 0 && (index - getTimePeriod(0).getSerialIndex()) > this.maximumItemAge) { this.data.remove(0); removed = true; } if (removed) { updateMinMaxYByIteration(); if (notify) { fireSeriesChanged(); } } }