Issues with TimeSeries chart (12-hour vs 24-hour time)

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jstrieb
Posts: 6
Joined: Tue Jul 12, 2016 3:49 pm
antibot: No, of course not.

Issues with TimeSeries chart (12-hour vs 24-hour time)

Post by jstrieb » Tue Jul 12, 2016 3:55 pm

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:

Code: Select all

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

Code: Select all

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)

Question has also been asked here.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Issues with TimeSeries chart (12-hour vs 24-hour time)

Post by paradoxoff » Tue Jul 12, 2016 6:42 pm

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.
Dou you mean that the times values are in fact hours between 1 and 12 which may represent both AM and PM times, and that you need to distinguihs between these two formats?
Or dou you mean that though the times values are different and couly be converted to different instance of Hour, an AM value is overwriting an existing PM value in the TimeSeries class?

jstrieb
Posts: 6
Joined: Tue Jul 12, 2016 3:49 pm
antibot: No, of course not.

Re: Issues with TimeSeries chart (12-hour vs 24-hour time)

Post by jstrieb » Tue Jul 12, 2016 8:38 pm

paradoxoff wrote:Dou you mean that the times values are in fact hours between 1 and 12 which may represent both AM and PM times, and that you need to distinguihs between these two formats?
Or dou you mean that though the times values are different and couly be converted to different instance of Hour, an AM value is overwriting an existing PM value in the TimeSeries class?

The times array consists of integers between 1 and 24, which currently display as 24-hour times on the X axis. I am trying to have the program automatically differentiate AM/PM, rather than displaying in military time. I experimented with having two of each value in the array to see if it would work, but instead the exception was thrown when I attempted that. Does that clarify?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Issues with TimeSeries chart (12-hour vs 24-hour time)

Post by paradoxoff » Wed Jul 13, 2016 6:34 am

jstrieb wrote:The times array consists of integers between 1 and 24, which currently display as 24-hour times on the X axis. I am trying to have the program automatically differentiate AM/PM, rather than displaying in military time.
Then, all you have to do is to choose a suitable SimpleDateFormat and use that as parameter on DateAxis.setDateFormatOverride(theDateFormat);

jstrieb
Posts: 6
Joined: Tue Jul 12, 2016 3:49 pm
antibot: No, of course not.

Re: Issues with TimeSeries chart (12-hour vs 24-hour time)

Post by jstrieb » Wed Jul 13, 2016 3:35 pm

paradoxoff wrote:Then, all you have to do is to choose a suitable SimpleDateFormat and use that as parameter on DateAxis.setDateFormatOverride(theDateFormat);
Thank you, that's exactly what I did. Here was my final solution:

Code: Select all

JFreeChart chart2 = ChartFactory.createTimeSeriesChart(title, "Time (Hour)", "Vehicles Parked", dataset2, true, true, false);
((DateAxis)chart2.getXYPlot().getDomainAxis()).setDateFormatOverride(new SimpleDateFormat("hh:mm a"));

Locked