gpt4 book ai didi

java - 将CSV文件中的数据读取到ArrayList中并显示在XY图表中

转载 作者:行者123 更新时间:2023-11-30 06:36:14 24 4
gpt4 key购买 nike

我想显示随时间变化的温度曲线。我现在已经读取了一个类似于 CSV 的文件,它让我知道指示的时间和温度。现在我想使用 JFreeChart 显示值的 XY 图表。该文件的原始数据如下:

utc,local,celsius
2017-07-12T07:02:53+00:00,2017-07-12T09:02:53+02:00,26.25
2017-07-12T08:02:54+00:00,2017-07-12T10:02:54+02:00,26.08
2017-07-12T09:02:55+00:00,2017-07-12T11:02:55+02:00,25.78
2017-07-12T10:02:56+00:00,2017-07-12T12:02:56+02:00,25.96
2017-07-12T10:51:02+00:00,2017-07-12T12:51:02+02:00,26.14
2017-07-12T10:51:02+00:00,2017-07-12T12:51:02+02:00,26.14

时间和温度值的输出(我已从原始文件中分离出来)如下所示:

09:02:53,26.25
10:02:54,26.08
11:02:55,25.78
12:02:56,25.96
12:51:02,26.14
12:51:02,26.14

编辑:现在我在 Trashgod 的示例中插入了 DateTimeFormatter:看起来像:

public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
ApplicationFrame frame = new ApplicationFrame("CSVTest");
Test test = new Test();
frame.add(test.createChart("Temperature profile"));
frame.pack();
frame.setLocationRelativeTo(null);;
frame.setVisible(true);
});
}

private ChartPanel createChart(String chartTitle) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(chartTitle,
"Time", "Temperature", createDataset(), true, true, false);
ChartPanel chartPanel = new ChartPanel(chart);
XYPlot plot = chart.getXYPlot();
DateAxis domain = (DateAxis) plot.getDomainAxis();
domain.setDateFormatOverride(DateFormat.getDateInstance());
plot.setBackgroundPaint(Color.WHITE);
return chartPanel;
}


private XYDataset createDataset() {
TimeSeries series = new TimeSeries("Temperature");
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
try (FileReader fr = new FileReader("TestCSV.csv");
BufferedReader br = new BufferedReader(fr)) {
String line;
br.readLine();
while ((line = br.readLine()) != null) {
String[] split = line.split(",");

System.out.println(ZonedDateTime.parse(split[1]).format(DateTimeFormatter.ISO_LOCAL_TIME) + "," +split[2]);
ZonedDateTime zdt = ZonedDateTime.of(LocalDate.now(),LocalTime.parse(split[0]), ZoneId.systemDefault());
String s = ZonedDateTime.parse(split[0]).format(DateTimeFormatter.ISO_LOCAL_TIME);

Second second = new Second(Date.from(zdt.toInstant()));
series.add(second, Double.valueOf(split[1]));
}
} catch (IOException | SeriesException e) {
System.err.println("Error: " + e);
}
return dataset;
}

“CSV”类文件的第一行仍然显示 09:02:53,26.25然后我得到一个 DateTimeParseException: Text '2017-07-12T09:02:53+02:00' Could not be parsed at index 2

Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '2017-07-12T07:02:53+00:00' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
at org.jfree.chart.demo.Test.createDataset(Test.java:63)
at org.jfree.chart.demo.Test.createChart(Test.java:43)
at org.jfree.chart.demo.Test.lambda$0(Test.java:34)

为什么无法读取或显示文件的其余部分?(“System.out.println()”只能作为最后的控件)。 DateTimeFomatter 是正确的,不是吗?

按照你的方法,本地化的时间我就不再继续了,程序无法翻译。我做错了什么?如果直接输出怎么办

09:02:53,26.25
10:02:54,26.08
11:02:55,25.78
12:02:56,25.96
12:51:02,26.14
12:51:02,26.14

显示在图表中?我想像我一样 split 和转变也可以吧?现在我在代码中添加了 setDateFormatOverride (),但错误消息以及输出保持不变。

最佳答案

有几个问题是显而易见的:

  • 你永远不会向行添加任何内容;至少,您需要这样的东西:

    lines.add(line);
  • 考虑创建时间序列,而不是 ChartFactory.createXYLineChart():

    ChartFactory.createTimeSeriesChart(…)
  • createDataset() 返回的 XYDataset 应该是一个 TimeSeriesCollection,您可以在其中添加 TimeSeries .

  • createDataset()中,迭代lines,解析数据字段,并将值添加到TimeSeries

  • 给定的时间值最接近地由 LocalTime 建模,但 TimeSeries 期望通过 add() 坐标定义 RegularTimePerioddouble;请参阅Legacy Date-Time Code关于如下所示的转换。

  • 请注意,TimeSeries 对于重复的域值会抛出 SeriesException;因此,在示例输入空气图表中,只有四行中的三行。

  • 不要替换工厂提供的XYLineAndShapeRenderer,而是获取对其的引用以供以后修改。

  • 使用显示的方法之一更改图表的大小 here .

  • 避免扩展顶级容器行ApplicationFrame

  • event dispatch thread 上构造和操作 Swing GUI 对象.

  • 使用try-with-resources statement确保每个资源在语句结束时关闭。

  • 作为您的actual data包含ISO 8601日期,ZonedDateTime.parse()可以直接使用;使用setDateFormatOverride()设置日期轴标签的格式;下面的示例指定了 ISO 8601 格式的 UTC 时区,以便于比较;注释掉对 setDateFormatOverride() 的调用以查看本地时区的时间。

image

import java.awt.Color;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;

/** @see https://stackoverflow.com/a/45173688/230513 */
public class CSVTest {

public static void main(String[] args) {
EventQueue.invokeLater(() -> {
ApplicationFrame frame = new ApplicationFrame("CSVTest");
CSVTest test = new CSVTest();
frame.add(test.createChart("Temperature profile"));
frame.pack();
frame.setLocationRelativeTo(null);;
frame.setVisible(true);
});
}

private ChartPanel createChart(String chartTitle) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(chartTitle,
"Time", "Temperature", createDataset(), true, true, false);
ChartPanel chartPanel = new ChartPanel(chart);
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.WHITE);
XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
r.setBaseShapesVisible(true);
DateAxis axis = (DateAxis) plot.getDomainAxis();
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ssX");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
axis.setDateFormatOverride(df);
return chartPanel;
}

private XYDataset createDataset() {
TimeSeries series = new TimeSeries("Temperature");
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
try (FileReader fr = new FileReader("temp.csv");
BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = br.readLine()) != null) {
String[] s = line.split(",");
ZonedDateTime zdt = ZonedDateTime.parse(s[0]);
Second second = new Second(Date.from(zdt.toInstant()));
series.add(second, Double.valueOf(s[2]));
}
} catch (IOException | SeriesException e) {
System.err.println("Error: " + e);
}
return dataset;
}
}

关于java - 将CSV文件中的数据读取到ArrayList中并显示在XY图表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com