我想随着时间的推移显示温度曲线。我现在读了一个与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:文本'2017-07-12T09:02:53 + 02:00'无法在索引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
显示在图表中?我认为分裂和变换就像我做的那样好,不是吗?现在我在代码中有setDateFormatOverride(),但错误信息以及输出保持不变。
答案 0 :(得分:1)
显而易见的几个问题:
您永远不会向lines
添加任何内容;至少,你需要这样的东西:
lines.add(line);
考虑创建时间序列,而不是ChartFactory.createXYLineChart()
:
ChartFactory.createTimeSeriesChart(…)
XYDataset
返回的createDataset()
应该是TimeSeriesCollection
,您可以添加TimeSeries
。
在createDataset()
中,迭代lines
,解析数据字段,并将值添加到TimeSeries
。
给出的时间值最接近LocalTime
建模,但TimeSeries
期望由add()
和RegularTimePeriod
定义的double
坐标;有关下面显示的转化,请参阅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()
的来电,查看当地时区的时间。
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;
}
}