⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movingaverage.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ------------------
 * MovingAverage.java
 * ------------------
 * (C) Copyright 2003, 2004, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   Benoit Xhenseval;
 *
 * $Id: MovingAverage.java,v 1.1 2004/08/31 15:34:17 mungady Exp $
 *
 * Changes
 * -------
 * 28-Jan-2003 : Version 1 (DG);
 * 10-Mar-2003 : Added createPointMovingAverage(...) method contributed by Benoit Xhenseval (DG);
 * 01-Aug-2003 : Added new method for TimeSeriesCollection, and fixed bug in XYDataset method (DG);
 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
 *
 */

package org.jfree.data.time;

import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * A utility class for calculating moving averages of time series data.
 */
public class MovingAverage {

    /**
     * Creates a new {@link TimeSeriesCollection} containing a moving average series for each
     * series in the source collection.
     * 
     * @param source  the source collection.
     * @param suffix  the suffix added to each source series name to create the corresponding 
     *                moving average series name.
     * @param periodCount  the number of periods in the moving average calculation.
     * @param skip  the number of initial periods to skip.
     * 
     * @return A collection of moving average time series.
     */
    public static TimeSeriesCollection createMovingAverage(TimeSeriesCollection source,
                                                           String suffix,
                                                           int periodCount,
                                                           int skip) {
    
        // check arguments
        if (source == null) {
            throw new IllegalArgumentException(
                "MovingAverage.createMovingAverage(...) : null source."
            );
        }

        if (periodCount < 1) {
            throw new IllegalArgumentException("MovingAverage.createMovingAverage(...) : "
                + "periodCount must be greater than or equal to 1.");
        }

        final TimeSeriesCollection result = new TimeSeriesCollection();
        
        for (int i = 0; i < source.getSeriesCount(); i++) {
            final TimeSeries sourceSeries = source.getSeries(i);
            final TimeSeries maSeries = createMovingAverage(
                sourceSeries, sourceSeries.getName() + suffix, periodCount, skip
            );
            result.addSeries(maSeries);       
        }
        
        return result;
        
    }
    
    /**
     * Creates a new {@link TimeSeries} containing moving average values for the given series.
     * <p>
     * If the series is empty (contains zero items), the result is an empty series.
     *
     * @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) {

        // check arguments
        if (source == null) {
            throw new IllegalArgumentException(
                "MovingAverage.createMovingAverage(...) : null source."
            );
        }

        if (periodCount < 1) {
            throw new IllegalArgumentException("MovingAverage.createMovingAverage(...) : "
                + "periodCount must be greater than or equal to 1.");

        }

        final TimeSeries result = new TimeSeries(name, source.getTimePeriodClass());

        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...
            final long firstSerial = source.getDataItem(0).getPeriod().getSerialIndex() + skip;

            for (int i = source.getItemCount() - 1; i >= 0; i--) {

                // get the current data item...
                final TimeSeriesDataItem current = source.getDataItem(i);
                final RegularTimePeriod period = current.getPeriod();
                final long serial = period.getSerialIndex();

                if (serial >= firstSerial) {
                    // work out the average for the earlier values...
                    int n = 0;
                    double sum = 0.0;
                    final long serialLimit = period.getSerialIndex() - periodCount;
                    int offset = 0;
                    boolean finished = false;

                    while ((offset < periodCount) && (!finished)) {
                        if ((i - offset) >= 0) {
                            final TimeSeriesDataItem item = source.getDataItem(i - offset);
                            final RegularTimePeriod p = item.getPeriod();
                            final Number v = item.getValue();
                            final 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;

    }

    /**
     * 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).
     * <p>
     * If the series is empty (contains zero items), the result is an empty series.
     * <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, final int pointCount) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -