Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

date - how to import and plot CSV in Java with timestamp

Hi Currently I am trying to import the CSV file into the Java to plot the data, basically, I can import successfully but it does not work in my CSV because it has the time format as: HH:mm:ss MM-dd-yy

The data is as follow:

2016-05-15 00:00:00 0
2016-05-15 00:00:00 0
2016-05-15 00:00:00 5.44852
2016-05-15 00:00:01 0
2016-05-15 00:00:01 0
2016-05-15 00:00:01 5.26064

the code is as follow:

import java.io.FileNotFoundException; 
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.jfree.chart.axis.DateAxis;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import au.com.bytecode.opencsv.CSVReader;
public class Test extends ApplicationFrame{
/**
 * 
 */
private static final long serialVersionUID = 1L;
XYSeriesCollection dataset;
JFreeChart chart;
final ChartPanel chartPanel;
final int chartWidth = 560;
final int chartHeight = 367;
CSVReader reader;
String[] readNextLine;
XYSeries series;

public Test(String applicationTitle) throws IOException {
    super(applicationTitle);
    dataset = createDataset();
    chart = createChart(dataset);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(chartHeight,
            chartWidth));
    this.add(chartPanel);
}

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

        // Set up series
        final XYSeries seriesX = new XYSeries("X");
        final XYSeries seriesY = new XYSeries("Y");
        final XYSeries seriesZ = new XYSeries("Z");

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

            double Time = Double.valueOf(readNextLine[0]);
            double X = Long.valueOf(readNextLine[1]);
            double Y = Long.valueOf(readNextLine[2]);
            double Z = Long.valueOf(readNextLine[3]);
            seriesX.add(Time, X);
            seriesY.add(Time, Y);
            seriesZ.add(Time, Z);

        }

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

        dataset.addSeries(seriesX);
        dataset.addSeries(seriesY);
        dataset.addSeries(seriesZ);
    } 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;
    }

public static void main(String[] args) throws IOException {
    System.out.println("In here, to create a Test");
    final Test demo = new Test("Test XY Line chart");
    System.out.println("Created, pakcking");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
}

}

but I don't know how to put the time format in this code. Thank you for anyone who can help me with it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In this simpler example,

  • Use SimpleDateFormat to parse dates of the given format.

  • Use ChartFactory.createTimeSeriesChart() to create a time series chart; it will use a DateAxis for the domain.

image

Data:

2016-05-15 00:00:00, 20
2016-05-15 00:01:01, 21
2016-05-15 00:02:02, 42

Code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Test extends ApplicationFrame {

    public Test(String applicationTitle) throws IOException {
        super(applicationTitle);
        this.add(new ChartPanel(createChart(createDataset())));
    }

    public XYSeriesCollection createDataset() {
        final XYSeries series = new XYSeries("X");
        try {
            BufferedReader in = new BufferedReader(new FileReader("data.txt"));
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String s = null;
            while ((s = in.readLine()) != null) {
                String[] a = s.split(",");
                Date d = f.parse(a[0]);
                int v = Integer.valueOf(a[1].trim());
                series.add(d.getTime(), v);
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace(System.err);
        }
        return new XYSeriesCollection(series);
    }

    public JFreeChart createChart(XYDataset dataset)
        throws NumberFormatException, IOException {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Acceleration vs Time", "Time", "Acceleration", dataset,
            true, true, false);
        return chart;
    }

    public static void main(String[] args) throws IOException {
        final Test demo = new Test("Test Time Series Chart");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...