我正在尝试使用 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 之间的普通整数? (例如已经格式化的时间字符串)?
最佳答案
我不熟悉 Renjin
,但假设 Date
值表示自 epoch 以来的毫秒数。 ,
使用
setDateFormatOverride()
在轴上有合适的SimpleDateFormat
,例如axis.setDateFormatOverride(new SimpleDateFormat("hh:mm"));
TimeSeries
“将确保……每个时期在系列中最多出现一次。”如果正确转换传入值,这应该变得毫无意义。
关于java - 非军事时间中的 JFreeChart TimeSeries 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38331541/