0

選択した期間の時間に基づく移動平均線を作成しようとしています。x軸は1日の24時間を表し、y軸は全期間の各時間の平均値を表すようにします。たとえば、人の努力の4月の1時間あたりの平均が必要です。

次のプログラムを作成しましたが、最初のプログラムの2日後に印刷されます。どんな助けでも大歓迎です。

編集:ここにクロスポスト。

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();
}

}
4

1 に答える 1

1

あなたのコメントから私が理解していることから、あなたは本当にプロットに2ではなく4つのシリーズを表示したいのです-つまり

  • 2012 年 5 月 1 日 新規
  • 2012 年 5 月 2 日 新規
  • 2012 年 5 月 1 日 キャンセルされました
  • 2012 年 5 月 2 日 キャンセルされました

次に、メソッドを変更するか、さらに良いことに、で数回呼び出されgetDateByHour()たコンストラクターと組み合わせることができます。new Hour(Date)createDataset()

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

DateFormatを「HH」に設定しているため、使用する実際の日/月/年は重要ではありません。そのため、レンダリングされません。(後でx軸を変更しない限り...)

于 2012-05-24T12:45:48.187 に答える