java 将日期/时间添加到 JFreeChart 图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12272679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding date/time to JFreeChart graph
提问by DommyCastles
I currently have a method which queries a database for values, and plots them to a graph. The only problem is, the time
variable is a long, and results in my graph looking like this:
我目前有一种方法可以查询数据库中的值,并将它们绘制成图形。唯一的问题是,time
变量很长,导致我的图表如下所示:
I want to convert it to a date format and then add it to the graph.
我想将其转换为日期格式,然后将其添加到图表中。
How can I do this?
我怎样才能做到这一点?
Here is my graph code:
这是我的图形代码:
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);
Here is my method for adding to the graph:
这是我添加到图表的方法:
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);
}
}
}
Any help would be GREATLY appreciated! Thank you!
任何帮助将不胜感激!谢谢!
EDIT: I have changed my code to this:
编辑:我已将代码更改为:
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);
And my new GetDustLevels() method:
我的新 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);
}
}
}
回答by trashgod
Without an sscceor the desired format, I'm just guessing.
没有sscce或所需的格式,我只是猜测。
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
Addendum: Looking closer, you're using ChartFactory.createXYLineChart()
, which creates a NumberAxis
for the domain. Instead, use ChartFactory.createTimeSeriesChart()
, which creates a DateAxis
for the domain.
附录:仔细观察,您正在使用ChartFactory.createXYLineChart()
,它NumberAxis
为域创建了。相反,使用ChartFactory.createTimeSeriesChart()
,它会DateAxis
为域创建。
Addendum: If time
represents milliseconds from the same epoch as a Java Date
, you can use new Date(time.longValue())
to construct your dataset's RegularTimePeriod
. There's a related example here.
附录:如果time
代表与 Java 相同时代的毫秒数,则Date
可以使用它new Date(time.longValue())
来构建数据集的RegularTimePeriod
. 有一个相关的例子在这里。