Naftuli Tzvi Kay Naftuli Tzvi Kay - 11 months ago 97
Java Question

Relative Timestamps in a TimeSeries

I'm trying to make a pretty simple chart which graphs measured forces over the period of a second. I have plain-jane example which is outputting the following graph:

enter image description here

Here's my code:

TimeSeries series1 = new TimeSeries("Force", "Domain", "Range");

//generate values
for (int i = 0; i < 1000; i++) {
series1.add(new TimeSeriesDataItem(new Millisecond(i, 0, 0, 0, 1, 1, 2012), i));

TimeSeriesCollection dataset = new TimeSeriesCollection();

JFreeChart chart = ChartFactory.createXYLineChart("Measured Forces",
"Time", "Force in Pounds", dataset, PlotOrientation.VERTICAL,
false, false, false);

ChartUtilities.saveChartAsPNG(new File("/home/rfkrocktk/Desktop/chart.png"), chart, 1280, 720);

How can I transform those ugly 1,325,404,800,000 values into simple 0-1,000 values representing the overall time measured instead of the exact system time?

I've been looking for constructors that match my requirements, but I can't seem to find them.

Answer Source

ChartFactory.createXYLineChart() creates two instances of NumberAxis, one for each of the domain and range. As an alternative, consider ChartFactory.createTimeSeriesChart(), which uses a DateAxis for the domain. In either case, you can use setXxxFormatOverride() on the axis to get any desired format. The example shown here specifies a factory DateFormat.