0

Hi Currently I am trying to plot data from CSV, I have the code but it is not able to take HH:mm:ss MM-dd-yy, the code I have is just for float but not timestamp. I need to plot it, it is current value against time like: "2016-05-15 00:00:00" "20" "2016-05-15 00:00:01" "22" and the data is in my local drive, and the code is as following:

file reader:

public XYSeriesCollection createDataset() throws NumberFormatException,
        IOException {
    dataset = new XYSeriesCollection();
    try {
        reader = new CSVReader(new FileReader("/usr/csv_dump.csv"),'\t');
        // Read the header and chuck it away
        readNextLine = reader.readNext();

import data:

        // Set up series
        final XYSeries seriesX = new XYSeries("X");

        while ((readNextLine = reader.readNext()) != null) {
            // add values to dataset

            double Time = Double.valueOf(readNextLine[0]);
            double X = Long.valueOf(readNextLine[1]);
            seriesX.add(Time, X);

        }

        System.out.println(seriesX.getMaxX() + "; " + seriesX.getMaxY());

        dataset.addSeries(seriesX);
    } catch (FileNotFoundException e) {
        System.out.println("File not found!");
    }
    return dataset;
}

public JFreeChart createChart(XYDataset dataset)
        throws NumberFormatException, IOException {
    chart = ChartFactory.createXYLineChart("Acceleration vs Time", // chart
                                                                    // title
            "Time", // domain axis label
            "Acceleration", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            true, // legend
            true, // tooltips
            false); // urls

    return chart;
}

as you can see it can only read double but not the time, if I want to import the time (HH:mm:ss MM-dd-yy), how can I do it?

rick
  • 1,869
  • 13
  • 22

1 Answers1

0

I am not familiar with the JFreeChart classes and how exactly this kind of plot works, but since the XYSeries#add method seems to accept only a double value as first parameter, you won't be able to pass a Date value. Therefore I don't think your goal can be achieved using this approach.

There is a possibility to "interpret" the timestamp by parsing the corresponding String to a Date object and use its getTime() method. BUT beware: getTime() returns a long, not a double value!

On a side note: for parsing a String to a Date object refer to DateFormat, respectively SimpleDateFormat, and its parse(String s) method.

Muto
  • 140
  • 1
  • 10