1

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();
dataset.addSeries(series1);

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.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

3 Answers3

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

As far as I know, TimeSeries does not support the concept of 'relative time' - all of the x value types (like Millisecond) are based on java.util.Date, which yields an absolute time.

I think you might be better off using XYSeries instead of TimeSeries. Then the x values can be integers.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
0

The trick is to use RelativeDateFormat for the domain axis formatting with ChartFactory.createTimeSeriesChart(). This prints labels in the format of #h#m#.###s. For example, you might see "0h0m2.529s".

  JFreeChart chart;
  XYPlot plot;
  DateAxis axis;
  TimeSeriesCollection dataset;
  TimeSeries series;
  RelativeDateFormat format;
  long startTime;

  series    = new TimeSeries("Time");
  dataset   = new TimeSeriesCollection(series);
  chart     = ChartFactory.createTimeSeriesChart("Relative", "Elapsed Time", "Value", dataset, true, true, true); 
  plot      = chart.getXYPlot();
  axis      = (DateAxis) plot.getDomainAxis();
  format    = new RelativeDateFormat();
  startTime = System.currentTimeMillis();

  axis.setDateFormatOverride(format);
  format.setBaseMillis(startTime);
Nathan
  • 8,093
  • 8
  • 50
  • 76