3

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);
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Jihath
  • 103
  • 2
  • 7
  • Sorry Guys :$ it was just a matter of a variable mismatch – Jihath May 31 '11 at 17:29
  • You can delete your question, or you can expand on your findings in an answer if you think others might benefit. See the [faq](http://stackoverflow.com/faq) for details. – trashgod May 31 '11 at 20:16
  • actually , instead of putting intermediatestringdate in tha date conversion command , i put dateend , since i've tried all i could do (after some searchs of course) i thought it was an api mis use, then i've add a lot of system.out.println i found the problem : Anyway this methode presents a way to set dynamicly a timeseries in jfree if there are some optim i'll be glad to get them – Jihath Jun 01 '11 at 09:24

3 Answers3

7

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

If you change the TimeSeries and TimeSeriesCollection to be TimePeriodValues and TimePeriodValuesCollection, you will not have the unique time constraint to deal with.

here's what i did, and i'm able to plot time values along the x-axis that are identical down to the millisecond:

    TimePeriodValues data = new TimePeriodValues("duration"); 
    for(int x=0; x<_stats.getDataPoints().size();x++)
    {
        DataPoint dp = _stats.getDataPoints().get(x); 
        data.add(new Millisecond(dp._date),dp._duration); 
    }
    TimePeriodValuesCollection dataset = new TimePeriodValuesCollection(); 

    dataset.addSeries(data); 
    JFreeChart chart = ChartFactory.createScatterPlot("Title", "start time", "duration (millis)", dataset, PlotOrientation.VERTICAL, true, true, false);
    XYPlot plot = chart.getXYPlot();
    plot.setDomainAxis(new DateAxis()); 
    DateAxis domainAxis = (DateAxis)plot.getDomainAxis(); 
    domainAxis.setDateFormatOverride(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS")); 
    ChartUtilities.saveChartAsPNG(new File(_reportFolder+File.separator+uuid+".png"), chart, 500, 300);
0

I believe here is the root cause of the problem. When adding list of time/value in the TimeSeries, the time point added must be unique from the whole list.
series2.add(new Day(intermediatedate),500);

Otherwise, using addOrUpdate if there are possibilities of the time in the list.
series2.addOrUpdate(new Day(intermediatedate),500);

Terrence
  • 9
  • 1