Example usage for org.jfree.data.time TimeSeries getRawDataItem

List of usage examples for org.jfree.data.time TimeSeries getRawDataItem

Introduction

In this page you can find the example usage for org.jfree.data.time TimeSeries getRawDataItem.

Prototype

TimeSeriesDataItem getRawDataItem(RegularTimePeriod period) 

Source Link

Document

Returns a data item for the series.

Usage

From source file:org.jfree.data.time.MovingAverage.java

/**
 * Creates a new {@link TimeSeries} containing moving average values for
 * the given series, calculated by number of points (irrespective of the
 * 'age' of those points).  If the series is empty (contains zero items),
 * the result is an empty series./*  w w  w. j ava 2  s . com*/
 * <p>
 * Developed by Benoit Xhenseval (www.ObjectLab.co.uk).
 *
 * @param source  the source series.
 * @param name  the name of the new series.
 * @param pointCount  the number of POINTS used in the average calculation
 *                    (not periods!)
 *
 * @return The moving average series.
 */
public static TimeSeries createPointMovingAverage(TimeSeries source, String name, int pointCount) {

    ParamChecks.nullNotPermitted(source, "source");
    if (pointCount < 2) {
        throw new IllegalArgumentException("periodCount must be greater " + "than or equal to 2.");
    }

    TimeSeries result = new TimeSeries(name);
    double rollingSumForPeriod = 0.0;
    for (int i = 0; i < source.getItemCount(); i++) {
        // get the current data item...
        TimeSeriesDataItem current = source.getRawDataItem(i);
        RegularTimePeriod period = current.getPeriod();
        // FIXME: what if value is null on next line?
        rollingSumForPeriod += current.getValue().doubleValue();

        if (i > pointCount - 1) {
            // remove the point i-periodCount out of the rolling sum.
            TimeSeriesDataItem startOfMovingAvg = source.getRawDataItem(i - pointCount);
            rollingSumForPeriod -= startOfMovingAvg.getValue().doubleValue();
            result.add(period, rollingSumForPeriod / pointCount);
        } else if (i == pointCount - 1) {
            result.add(period, rollingSumForPeriod / pointCount);
        }
    }
    return result;
}

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 w w w  .  j  a v  a2 s  .co  m
 *
 * @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

/**
 * Adds or updates data from one series to another.  Returns another series
 * containing the values that were overwritten.
 *
 * @param series  the series to merge with this.
 *
 * @return A series containing the values that were overwritten.
 *///from  w  ww .  j  av  a2  s.c  o m
public TimeSeries addAndOrUpdate(TimeSeries series) {
    TimeSeries overwritten = new TimeSeries("Overwritten values from: " + getKey());
    for (int i = 0; i < series.getItemCount(); i++) {
        TimeSeriesDataItem item = series.getRawDataItem(i);
        TimeSeriesDataItem oldItem = addOrUpdate(item.getPeriod(), item.getValue());
        if (oldItem != null) {
            overwritten.add(oldItem);
        }
    }
    return overwritten;
}