Search code examples
javaexcelapache-poijfreechart

JFreeChartSeries Series Exception... "attempting to add an observation for the time period..."


I want to plot a xy chart with time/date being my domain and integers being my range. I managed to get my graph just how I want it....domain/range min/max being lowest and highest values it read in from the excel cells but it wasn't plotting anything on the chart but after debugging it I'm unsure of how to proceed...

  TimeSeries timeSeries = new TimeSeries("time");
  TimeSeriesCollection timeDataSet = new TimeSeriesCollection(timeSeries);
  while (rowIterator.hasNext()) {
            Date date;


            Number y_data = 0;

            row = (XSSFRow) rowIterator.next();
            XSSFCell x_col = row.getCell(0);
            date = x_col.getDateCellValue();

            Time t = new Time(date.getTime());

            XSSFCell y_col = row.getCell(1);
            y_data = y_col.getNumericCellValue();
            ///////PROBLEM BELOW

            timeSeries.add(new Day(t), y_data); //timeSeries.addOrUpdate(new Day(t), y_data)

        }
   timeDataSet.addSeries(timeSeries);

I have two choices but neither of them help...if I use timeSeries.add() I get the error mentioned above even though they are completely different times....yes they are same day, more specifically, even same minutes for some reads but seconds and milliseconds differ. If I use timeSeries.addOrUpdate() it seems to completely overwrite every read and my TimeSeries variable arraylist just has junk ...this is what it has after it has read all the values, [org.jfree.data.time.TimeSeriesDataItem@40c77f1e, null, null,...] just more null..... I know that it should store the xy pairs because I'm running another more simple example and it has the appropriate pairs maybe this is different because I'm using TimeSeries .....I also tried not using Time so just having it be

 timeSeries.addOrUpdate(new Day(date), y_date);

but same issue......thank you for any suggestions


Solution

  • It is normal. You use the Day JfreeChart class which is a TimePeriod for a day without time aware.

    /**
    * Represents a single day in the range 1-Jan-1900 to 31-Dec-9999.  This class
    * is immutable, which is a requirement for all {@link RegularTimePeriod}
    * subclasses.
    */
    public class Day extends RegularTimePeriod implements Serializable {
    

    Try with a finer TimePeriod such as FixedMillisecond or Millisecond, you should not have the problem any longer. Such as :

    timeSeries.add(new FixedMillisecond (date.getTime()), y_data); 
    

    For your information, in a timeseries, the time period can be any of the following :

    Year 
    Quarter 
    Month 
    Week 
    Day 
    Hour 
    Minute 
    Second 
    Millisecond 
    FixedMillisecond