2

I am getting the values from a database which is of Timestamp datatype. I am now trying to plot the timestamp values in the xxaxis in Jfreechart. Currently I am using

final TimeSeries s1 = new TimeSeries("Series1", Millisecond.class);

I am confused in adding the timestamp values to the time series. The timestamp values are in the following format.

2013-07-22 17:10:49.219

Can anyone please help me? I have referred many sites. In some sites they have suggested to extract the date, hours, minutes, and milliseconds to add in the series. But from timestamp how should I extract the values? I am new to this topic and the Date functions are really confusing for me.

2 Answers 2

4

Timestamp is a subclass of the java.util.Date class, so for a TimeSeries in JFreeChart you can directly create a Millisecond instance using this constructor:

 public Millisecond(Date time);

Your code will look something like this (assuming s1 is your TimeSeries instance):

 TimeStamp t = ... // read from somewhere
 double value = ... // the data value associated with your timestamp
 s1.add(new Millisecond(t), value);

...looping around of course to handle all your data items.

Sign up to request clarification or add additional context in comments.

Comments

2

Thanks David Gilbert for your answer. I was not entirely successful or maybe I just misunderstood you but Millisecond needs a Date as Parameter. Anyway: the following code works for me.

public class MyFrame extends JFrame {
  public MyFrame() {
    XYDataset xyData = createDataset();
    JFreeChart timeSeriesChart = 
    ChartFactory.createTimeSeriesChart("name of chart",
    "X-axis-name", "y-axis-name", xyData);

    ChartPanel panel = new ChartPanel(timeSeriesChart);
    MainPane.add(panel);
    this.pack();
  }

private XYDataset createDataset() {
        TimeSeries timeSeries = new TimeSeries("valueVsTime");

        // loop start
        long timeStamp = ...//read from somewhere, on x axis
        double value = ...//read from somewhere, on y axis
        timeSeries.add(new Millisecond(new Date(timeStamp), value));
        // loop end

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(timeSeries);
        return dataset;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.