如何在 2 天内使用实时 DateAxis 创建动态图表

How to create a dynamic Chart with real time DateAxis over a 2 days period

我正在使用 JfreeChart 制作动态图表以显示烤箱记录的温度。 这些值每 2 秒记录一次并存储在数据库中,然后我获取该数据库以将其显示在我的动态图表上。 烤箱始终 运行,目标是能够查看是否出现任何温度峰值(注意实际上有多个烤箱)。

我的工作主要是围绕 Trashgod 响应 here and there 或其他关于该主题的响应。

这是我的代码的简化版本:

public test(final String title) {
    super(title);
    
    final DynamicTimeSeriesCollection dataset =
            new DynamicTimeSeriesCollection(3, 10, new Second()); // 1
    
    dataset.setTimeBase(new Second(new Date()));
          
[...]

private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart result = ChartFactory.createTimeSeriesChart(
        TITLE, "hh:mm:ss", "°C", dataset, true, true, false);
    
   final XYPlot plot = result.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
    ValueAxis range = plot.getRangeAxis();
    range.setRange(-MINMAX, MINMAX);
    
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setFixedAutoRange(10000);                  // 2 
    return result;
}

现在,我其实有几点没弄明白。 首先如文档中所述

public DynamicTimeSeriesCollection(int nSeries,int nMoments,RegularTimePeriod timeSample) Parameters: nSeries - the number of series, nMoments - the number of items per series, timeSample - a time period sample.

问题是,当我在我的代码中将 nMoments 设置为 10 时,显示的时间很好,这意味着它非常接近我计算机上显示的实际时间,但我离 2 天的时间还很远我想监控,因为它只显示最后 10 个项目。但是当我增加 nMoments(每个系列的项目数)时,显示的时间不是“真实的”时间,如果我增加太多,它甚至不会加载。

我只是希望能够有一个足够长的 DateAxis,这样我就可以看到我在 2 天前获得的值,但是是以动态方式(一个 2 天周期的 FIFO),但我也需要 DateAxis足够准确。

我大部分时间都在玩 //1 和 //2 中的值,但我不能让它像我想要的那样工作,而且我不知道应该如何使用这两个值。我设法设置了 //2 以便我有 2 天的范围,但是数据仍然在 10 项的 FIFO 中,正如我所说,我的印象是当我增加这个限制时时间不对。

我也考虑过使用 TimeSeriesCollection,但我没能正确使用它来完成某些工作。我是否需要对此进行更深入的挖掘,或者我能否通过 DynamicTimeSeriesCollection 实现我想要的?

如有任何帮助,我们将不胜感激。

我使用 TimeSeriesCollection 而不是 Trashgod 在评论中提到的 DynamicTimeSeriesCollection 解决了我的问题。以此postReal-time graphing in Java为例