Example usage for org.jfree.data.time RegularTimePeriod previous

List of usage examples for org.jfree.data.time RegularTimePeriod previous

Introduction

In this page you can find the example usage for org.jfree.data.time RegularTimePeriod previous.

Prototype

public abstract RegularTimePeriod previous();

Source Link

Document

Returns the time period preceding this one, or null if some lower limit has been reached.

Usage

From source file:com.dreikraft.axbo.timeseries.TimeSeriesUtil.java

/**
 * Create a moving average time series from a given source series.
 *
 * @param source the source timeseries//  w w w.jav  a2s. com
 * @param preLen number of timeperiods before current time period included in
 * moving average calculation
 * @param postLen number of timeperiods after current time period included in
 * moving average calculation
 * @return a moving average time series
 */
public static TimeSeries createMovingAverage(final TimeSeries source, final int preLen, final int postLen) {

    final int len = preLen + postLen + 1;
    final TimeSeries result = new TimeSeries(source.getKey());
    final RegularTimePeriod lastTimePeriod = source.getTimePeriod(source.getItemCount() - 1);

    // process all timeperiods including empty ones
    RegularTimePeriod t = source.getTimePeriod(0);
    while (!(t.getFirstMillisecond() > lastTimePeriod.getFirstMillisecond())) {

        // calculate the moving avg value for the current time period
        double value = getValue(source, t);
        RegularTimePeriod ti = t;
        for (int i = 0; i < preLen; i++) {
            ti = ti.previous();
            value += getValue(source, ti);
        }
        ti = t;
        for (int i = 0; i < postLen; i++) {
            ti = ti.next();
            value += getValue(source, ti);
        }

        // add the moving avg value to the included time periods
        result.addOrUpdate(t, value / len);
        t = t.next();
    }

    return result;
}

From source file:org.jfree.chart.demo.TimeSeriesDemo11.java

/**
 * Creates a sample dataset./*from   w w w  .  j  a v  a2s .  c  o m*/
 * 
 * @param name  the dataset name.
 * @param base  the starting value.
 * @param start  the starting period.
 * @param count  the number of values to generate.
 *
 * @return The dataset.
 */
private XYDataset createDataset(final String name, final double base, final RegularTimePeriod start,
        final int count) {

    final TimeSeries series = new TimeSeries(name, start.getClass());
    RegularTimePeriod period = start;
    double value = base;
    for (int i = 0; i < count; i++) {
        series.add(period, value);
        period = period.previous();
        value = value * (1 + (Math.random() - 0.495) / 10.0);
    }

    final TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);

    return dataset;

}