我试图通过介绍开始日期和结束日期来绘制使用时间序列的条形图,但是我发现结束日期存在问题:
线程“main” org.jfree.data.general.SeriesException中的异常:您试图添加对时间段4-mai-2011的观察值,但是该系列已经包含该时间段的观察值。不允许重复。尝试使用addOrUpdate()方法。

final TimeSeries series2 = new TimeSeries("ip max", Day.class);

String datebegin = "04/29/2011 02:00:01";
String dateend = "05/04/2011 02:00:01";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
date = formatter.parse(datebegin);
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date);
Date date2;
date2 = (Date) formatter.parse(dateend);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (((cal1.compareTo(cal2)) != 0))  {
    cal1.add(Calendar.HOUR, 24);
    String intermediatestringdate = formatter.format(cal1.getTime());
    System.out.println( intermediatestringdate);
    Date intermediatedate = (Date) formatter.parse(dateend);
    series2.add(new Day(intermediatedate),500);
}

最佳答案

假设您要使用 ChartFactory.createXYBarChart() IntervalXYDataset 创建基于时间的条形图,只需使用合适的 TimeSeries 构造 TimePeriod 的域。

private static IntervalXYDataset createDataset() {
    TimeSeries series = new TimeSeries("Series");
    RegularTimePeriod day = new Day(29, 4, 2011);
    RegularTimePeriod end = new Day(4, 5, 2011);
    int i = 1;
    while (day.compareTo(end) < 1) {
        series.addOrUpdate(day, i++);
        day = day.next();
    }
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);
    return dataset;
}

另外,请注意Day.class不再需要TimeSeries

10-06 03:37