java - 非军事时间中的 JFreeChart TimeSeries 轴

标签 java time-series jfreechart renjin

我正在尝试使用 JFreeChart 在 Java 中绘制 TimeSeries 数据,但是该数据由 double 组成,这些 double 是调用 Renjin 的输出。 。将数据添加到TimeSeries的代码如下:

for (int i=0; i<series2Values.length; i++) {
    if (!Double.isNaN(series2Values[i])) {
        series2.add(new Hour((int)times[i], new Day()), series2Values[i]);
    } else {
        series2.add(new Hour((int)times[i], new Day()), null);
    }
}

final TimeSeriesCollection dataset2 = new TimeSeriesCollection();
dataset2.addSeries(series2);

JFreeChart chart2 = ChartFactory.createTimeSeriesChart(title, "Time (Hour)", "Vehicles Parked", dataset2, true, true, false);
ChartFrame frame2 = new ChartFrame(title, chart2, false);
frame2.pack();
frame2.setIconImage(img.getImage());
frame2.setVisible(true);

问题在于,每当绘制数据图表时,X 轴上的时间均以 24 小时军用时间显示,并且如果 time[] 数组包含任何重复值,则以下 SeriesException 被抛出:

Exception in thread "AWT-EventQueue-0" org.jfree.data.general.SeriesException: You are attempting to add an observation for the time period [6,12/7/2016] but the series already contains an observation for that time period. Duplicates are not permitted.  Try using the addOrUpdate() method.

异常推荐的 addOrUpdate() 方法只会覆盖 12 小时前该时间的第一个数据点,而不是创建新的数据点。我想以 12 小时格式显示数据,并显示 AM 和 PM。

是否有一种方便的方法可以使用 JFreeChart 执行此操作,或者更改代码是否更方便,以便我对 Renjin 的调用返回格式化的内容,而不是只是 1 到 24 之间的普通整数? (例如已经格式化的时间字符串)?

Cross-posted here.

最佳答案

我不熟悉 Renjin,但假设 Date 值表示自 epoch 以来的毫秒数。 ,

  1. 使用setDateFormatOverride()在轴上有合适的SimpleDateFormat ,例如

    axis.setDateFormatOverride(new SimpleDateFormat("hh:mm"));
    
  2. TimeSeries “将确保……每个时期在系列中最多出现一次。”如果正确转换传入值,这应该变得毫无意义。

关于java - 非军事时间中的 JFreeChart TimeSeries 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38331541/

相关文章:

java - 使用 Ellipse.2D 形状注释绘制旋转椭圆

java - 如何在 Java 中使用流从两个数组创建映射?

java - 设置 JVM 参数启动 catalina.bat

java - 弃用最终父类(super class)方法?

algorithm - 噪声正弦时间序列中的实时峰值检测

database - Cassandra 时间序列 : Allow Filtering, 桶或其他

Java JfreeChart - 如何使其缩放更小?

java - 当 ArrayList 都是通用类型时,为什么我无法通过 addAll 方法将 ArrayList 的内容添加到另一个 ArrayList 中?

python - statespace.SARIMAX model : why the model use all the data to train mode, 和 train 模型预测范围

java - JFreeChart 注释