java – 将日期/时间添加到JFreeChart图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 将日期/时间添加到JFreeChart图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3004字,纯文字阅读大概需要5分钟。
内容图文
![java – 将日期/时间添加到JFreeChart图](/upload/InfoBanner/zyjiaocheng/721/0537137b30a04167a45c7fada6414192.jpg)
我目前有一个方法可以在数据库中查询值,并将它们绘制成图形.唯一的问题是,时间变量很长,导致我的图形如下所示:
我想将其转换为日期格式,然后将其添加到图表中.
我怎样才能做到这一点?
这是我的图形代码:
private Long time;
private Long intensity;
public XYSeries series = new XYSeries("Sensor");
private XYDataset xyDataset;
public JFreeChart chart;
xyDataset = new XYSeriesCollection(series);
chart = ChartFactory.createXYLineChart("Sensor Data", "Time", "Intensity", xyDataset, PlotOrientation.VERTICAL, true, true, false);
这是我添加到图表的方法:
public void GetDustLevels() {
series.clear();
try {
currentSensor = Application.getInstance().getMinesite().getSensors().get(sensorID);
} catch (Exception e1) {
e1.printStackTrace();
}
if (currentSensor != null) {
sensorKDTree = currentSensor.getSensorData();
Iterator<Map.Entry<GenericPoint<Long>, String>> allPoints = sensorKDTree.iterator(sensorKDTree.getMin(null), sensorKDTree.getMax(null));
while (allPoints.hasNext()) {
GenericPoint<Long> timeIntensityPair = allPoints.next().getKey();
time = timeIntensityPair.getCoord(0);
intensity = timeIntensityPair.getCoord(1);
System.out.println("CURRENT SENSOR" + currentSensor);
System.out.println("TIME: " + time + " " + "INTENSITY: " + intensity);
series.add(time, intensity);
}
}
}
任何帮助将不胜感激!谢谢!
编辑:我已将我的代码更改为:
public TimeSeries series = new TimeSeries("Sensor", Date.class);
public JFreeChart chart;
private Long time;
private Long intensity;
TimeSeriesCollection xyDataset = new TimeSeriesCollection(series);
chart = ChartFactory.createTimeSeriesChart("Sensor Data", "Time", "Intensity", xyDataset, true, true, false);
我的新GetDustLevels()方法:
public void GetDustLevels() {
series.clear();
try {
currentSensor = Application.getInstance().getMinesite().getSensors().get(sensorID);
} catch (Exception e1) {
e1.printStackTrace();
}
if (currentSensor != null) {
sensorKDTree = currentSensor.getSensorData();
Iterator<Map.Entry<GenericPoint<Long>, String>> allPoints = sensorKDTree.iterator(sensorKDTree.getMin(null), sensorKDTree.getMax(null));
while (allPoints.hasNext()) {
GenericPoint<Long> timeIntensityPair = allPoints.next().getKey();
time = timeIntensityPair.getCoord(0);
intensity = timeIntensityPair.getCoord(1);
System.out.println("CURRENT SENSOR" + currentSensor);
System.out.println("TIME: " + time + " " + "INTENSITY: " + intensity);
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
series.add(new Date(time.longValue()), intensity);
}
}
}
解决方法:
没有sscce或所需的格式,我只是在猜测.
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
附录:仔细观察,您正在使用ChartFactory.createXYLineChart(),它为域创建NumberAxis.而是使用ChartFactory.createTimeSeriesChart(),它为域创建DateAxis.
附录:如果时间表示与Java Date相同的纪元的毫秒数,则可以使用新的日期(time.longValue())来构建数据集的RegularTimePeriod.有一个相关的例子here.
内容总结
以上是互联网集市为您收集整理的java – 将日期/时间添加到JFreeChart图全部内容,希望文章能够帮你解决java – 将日期/时间添加到JFreeChart图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。