Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

exception - JFreechart SeriesException

I am trying to plot a bar chart using timeseries by introducing the beginning and the end date , but i got a problem with the end date it indicate : Exception in thread "main" org.jfree.data.general.SeriesException: You are attempting to add an observation for the time period 4-mai-2011 but the series already contains an observation for that time period. Duplicates are not permitted. Try using the addOrUpdate() method.

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming you are creating a time-based bar chart using ChartFactory.createXYBarChart() with an IntervalXYDataset, just construct the domain of your TimeSeries with a suitable 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;
}

Also, note that Day.class is no longer required by TimeSeries.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...