javatime-seriesjfreechartrenjin

JFreeChart TimeSeries Axis in non-military time


I am trying to graph TimeSeries data in Java using JFreeChart, however the data consists of doubles that are the output of calls to Renjin. The code to add the data to the TimeSeries is as follows:

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);

The problem is that whenever the data is graphed, the times on the X axis are displayed in 24-hour military time, and if the time[] array contains any duplicate values the following SeriesException is thrown:

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.

The addOrUpdate() method recommended by the Exception will just overwrite the first data point in place for that time 12 hours earlier rather than making a new one. I would like to instead display the data in the 12-hour format with AM and PM shown.

Is there a convenient way to do this with JFreeChart, or would it be more convenient to change the code so that my calls to Renjin return something formatted, rather than just plain integers between 1 and 24? (e.g. already formatted time strings)?

Cross-posted here.


Solution

  • I'm not familiar with Renjin, but assuming a Date value representing milliseconds since the epoch,

    1. Use setDateFormatOverride() on the axis with a suitable SimpleDateFormat, e.g.

      axis.setDateFormatOverride(new SimpleDateFormat("hh:mm"));
      
    2. TimeSeries "will ensure…that each period appears at most one time in the series." This should become moot if the incoming value is converted correctly.