持续时间内的每小时移动平均值

时间:2012-05-24 09:59:38

标签: jfreechart

我正在尝试根据所选持续时间的小时创建移动平均线。我希望x轴代表一天中的24小时,而y轴代表整个持续时间内每小时的平均值。例如,我想要一个人努力的四月份的小时平均值。

我编写了以下程序,但它在第一个程序后两天打印。任何帮助都非常感谢。

修改:交叉发布here

public class MovingAverageDemo extends ApplicationFrame {

private static final long serialVersionUID = -1570942379483983865L;

/**
 * A moving average demo.
 * @param title the frame title.
 */
public MovingAverageDemo(String title) {
    super(title);

    // create a title...
    String chartTitle = "Hourly Average";
    XYDataset dataset = createDataset();

    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            chartTitle,
            "Hours",
            "Actions",
            dataset,
            true,
            true,
            false
    );

    LegendTitle legend = (LegendTitle) chart.getLegend();
    legend.setVisible(true);

    XYPlot plot = chart.getXYPlot();
    XYItemRenderer renderer = plot.getRenderer();
    if(renderer instanceof StandardXYItemRenderer) {
        StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
        rr.setPlotLines(true);
        rr.setBaseShapesFilled(true);
    }

    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);
    XYItemLabelGenerator generator =
        new StandardXYItemLabelGenerator(
                StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT,
            format, format);
    renderer.setBaseItemLabelGenerator(generator);
    renderer.setBaseItemLabelsVisible(true);

    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("HH"));
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);

}

/**
 * Creates a dataset, one series containing unit trust prices, the other a moving average.
 *
 * @return the dataset.
 */
public XYDataset createDataset() {

    TimeSeries s1 = new TimeSeries("New", Hour.class);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 1)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 2)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 3)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 4)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 5)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 6)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 7)), 148);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 8)), 153);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 9)), 142);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 10)), 123);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 11)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 12)), 139);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 13)), 142);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 14)), 138);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 15)), 137);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 16)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 17)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 18)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 19)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 20)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 21)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 22)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 23)), 0);
    s1.add(new Hour(getDateByHour(1, 4, 2012, 24)), 0);

    s1.add(new Hour(getDateByHour(2, 4, 2012, 1)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 2)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 3)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 4)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 5)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 6)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 7)), 168);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 8)), 173);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 9)), 162);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 10)), 143);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 11)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 12)), 119);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 13)), 122);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 14)), 118);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 15)), 117);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 16)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 17)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 18)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 19)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 20)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 21)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 22)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 23)), 0);
    s1.add(new Hour(getDateByHour(2, 4, 2012, 24)), 0);

    TimeSeries s2 = MovingAverage.createMovingAverage(s1, "NewC", 1, 0);

    TimeSeries s3 = new TimeSeries("Cancelled", Hour.class);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 1)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 2)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 3)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 4)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 5)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 6)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 7)), 9);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 8)), 7);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 9)), 2);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 10)), 8);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 11)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 12)), 9);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 13)), 7);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 14)), 3);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 15)), 9);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 16)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 17)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 18)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 19)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 20)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 21)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 22)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 23)), 0);
    s3.add(new Hour(getDateByHour(1, 4, 2012, 24)), 0);

    s3.add(new Hour(getDateByHour(2, 4, 2012, 1)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 2)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 3)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 4)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 5)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 6)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 7)), 9);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 8)), 7);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 9)), 2);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 10)), 8);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 11)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 12)), 9);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 13)), 7);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 14)), 3);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 15)), 9);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 16)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 17)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 18)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 19)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 20)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 21)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 22)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 23)), 0);
    s3.add(new Hour(getDateByHour(2, 4, 2012, 24)), 0);

    TimeSeries s4 = MovingAverage.createMovingAverage(s3, "CancelledC", 1, 0);

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    //dataset.addSeries(s1);
    dataset.addSeries(s2);
    //dataset.addSeries(s3);
    dataset.addSeries(s4);

    return dataset;
}

/**
 * Starting point for the demonstration application.
 *
 * @param args ignored.
 */
public static void main(String[] args) {

    MovingAverageDemo demo = new MovingAverageDemo("Moving Average Demo 1");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);

}

private Date getDateByHour(int day, int month, int year, int hour) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);

    return cal.getTime();
}

}

1 个答案:

答案 0 :(得分:1)

根据我的评论理解,你真的想在你的情节上展示4个系列,而不是2 - 即

  • 01-May-2012 New
  • 02-May-2012 New
  • 2012年5月1日取消
  • 02-May-2012取消

然后,您可以更改getDateByHour()方法,或者更好地与new Hour(Date)中多次调用的createDataset()构造函数结合使用:

private Hour makeHour(int hour) {
  return new Hour(hour, 1, 1, 2012);
}

您使用的实际日/月/年不重要,因为您已将DateFormat设置为" HH",因此不会呈现任何内容。 (假设你以后不改变x轴......)