如何使用时间戳在Java中导入和绘制CSV

时间:2016-06-21 13:57:42

标签: java date jfreechart

您好目前我正在尝试将CS​​V文件导入Java以绘制数据,基本上,我可以成功导入但它在我的CSV中不起作用,因为它的时间格式为:HH:mm:ss MM-dd-yy

数据如下:

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

代码如下:

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"),'\t');
        // 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);
}

}

但我不知道如何在这段代码中加入时间格式。感谢任何能帮助我的人。

1 个答案:

答案 0 :(得分:2)

在这个更简单的例子中,

  • 使用SimpleDateFormat来解析给定格式的日期。

  • 使用ChartFactory.createTimeSeriesChart()创建时间序列图表;它将使用DateAxis作为域名。

image

数据:

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

代码:

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);
    }
}