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

📄 movingaverage.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

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

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

        }

        final TimeSeries result = new TimeSeries(name, source.getTimePeriodClass());
        double rollingSumForPeriod = 0.0;
        for (int i = 0; i < source.getItemCount(); i++) {
            // get the current data item...
            final TimeSeriesDataItem current = source.getDataItem(i);
            final RegularTimePeriod period = current.getPeriod();
            rollingSumForPeriod += current.getValue().doubleValue();

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

    /**
     * Creates a new {@link XYDataset} containing the moving averages of each series in the
     * <code>source</code> dataset.
     *
     * @param source  the source dataset.
     * @param suffix  the string to append to source series names to create target series names.
     * @param period  the averaging period.
     * @param skip  the length of the initial skip period.
     *
     * @return The dataset.
     */
    public static XYDataset createMovingAverage(XYDataset source, String suffix,
                                                long period, final long skip) {

        return createMovingAverage(source, suffix, (double) period, (double) skip);
        
    }


    /**
     * Creates a new {@link XYDataset} containing the moving averages of each series in the
     * <code>source</code> dataset.
     *
     * @param source  the source dataset.
     * @param suffix  the string to append to source series names to create target series names.
     * @param period  the averaging period.
     * @param skip  the length of the initial skip period.
     *
     * @return The dataset.
     */
    public static XYDataset createMovingAverage(XYDataset source, String suffix,
                                                double period, double skip) {

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

        for (int i = 0; i < source.getSeriesCount(); i++) {
            final XYSeries s = createMovingAverage(source, i, source.getSeriesName(i) + suffix,
                                             period, skip);
            result.addSeries(s);
        }

        return result;

    }

    /**
     * Creates a new {@link XYSeries} containing the moving averages of one series in the
     * <code>source</code> dataset.
     *
     * @param source  the source dataset.
     * @param series  the series index (zero based).
     * @param name  the name for the new series.
     * @param period  the averaging period.
     * @param skip  the length of the initial skip period.
     *
     * @return The dataset.
     * 
     * @deprecated Use similar method with 'double' parameters.
     */
    public static XYSeries createMovingAverage(XYDataset source, 
                                               int series, String name,
                                               long period, long skip) {
        return createMovingAverage(source, series, name, (double) period, (double) skip);
    }

    /**
     * Creates a new {@link XYSeries} containing the moving averages of one series in the
     * <code>source</code> dataset.
     *
     * @param source  the source dataset.
     * @param series  the series index (zero based).
     * @param name  the name for the new series.
     * @param period  the averaging period.
     * @param skip  the length of the initial skip period.
     *
     * @return The dataset.
     */
    public static XYSeries createMovingAverage(XYDataset source, 
                                               int series, String name,
                                               double period, double skip) {

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

        if (period < Double.MIN_VALUE) {
            throw new IllegalArgumentException("MovingAverage.createMovingAverage(...) : "
                + "period must be positive.");

        }

        if (skip < 0.0) {
            throw new IllegalArgumentException("MovingAverage.createMovingAverage(...) : "
                + "skip must be >= 0.0.");

        }

        final XYSeries result = new XYSeries(name);

        if (source.getItemCount(series) > 0) {

            // if the initial averaging period is to be excluded, then calculate the lowest 
            // x-value to have an average calculated...
            final double first = source.getXValue(series, 0) + skip;

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

                // get the current data item...
                final double x = source.getXValue(series, i);

                if (x >= first) {
                    // work out the average for the earlier values...
                    int n = 0;
                    double sum = 0.0;
                    final double limit = x - period;
                    int offset = 0;
                    boolean finished = false;

                    while (!finished) {
                        if ((i - offset) >= 0) {
                            final double xx = source.getXValue(series, i - offset);
                            final Number yy = source.getY(series, i - offset);
                            if (xx > limit) {
                                if (yy != null) {
                                    sum = sum + yy.doubleValue();
                                    n = n + 1;
                                }
                            }
                            else {
                                finished = true;
                            }
                        }
                        else {
                            finished = true;
                        }
                        offset = offset + 1;
                    }
                    if (n > 0) {
                        result.add(x, sum / n);
                    }
                    else {
                        result.add(x, null);
                    }
                }

            }
        }

        return result;

    }

}

⌨️ 快捷键说明

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