Java 将日期/时间添加到JFreeChart图表
我目前有一种方法,可以查询数据库中的值,并将它们绘制成图形。唯一的问题是,Java 将日期/时间添加到JFreeChart图表,java,jfreechart,Java,Jfreechart,我目前有一种方法,可以查询数据库中的值,并将它们绘制成图形。唯一的问题是,time变量是一个长变量,导致我的图形如下所示: 我想将其转换为日期格式,然后将其添加到图表中 我该怎么做 这是我的图形代码: private Long time; private Long intensity; public XYSeries series = new XYSeries("Sensor"); private XYDataset xyDataset; public JFreeChart chart; x
time
变量是一个长变量,导致我的图形如下所示:
我想将其转换为日期格式,然后将其添加到图表中
我该怎么做
这是我的图形代码:
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);
}
}
}
还有我的新GetDustLevels()方法:
public void GetDustLevels(){
series.clear();
试一试{
currentSensor=Application.getInstance().getMinesite().getSensors().get(传感器ID);
}捕获(异常e1){
e1.printStackTrace();
}
如果(currentSensor!=null){
sensorKDTree=currentSensor.getSensorData();
迭代器allPoints=sensorKDTree.Iterator(sensorKDTree.getMin(null)、sensorKDTree.getMax(null));
while(allPoints.hasNext()){
GenericPoint timeIntensityPair=allPoints.next().getKey();
时间=timeIntensityPair.getCoord(0);
强度=timeIntensityPair.getCoord(1);
System.out.println(“电流传感器”+电流传感器);
System.out.println(“时间:+时间+”+“强度:+强度”);
XYPlot=(XYPlot)chart.getPlot();
DateAxis=(DateAxis)plot.getDomainAxis();
axis.setDateFormatOverride(新的SimpleDateFormat(“dd-MMM-yyyy”);
series.add(新日期(time.longValue()),强度);
}
}
}
我只是猜测,没有一个或所需的格式
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
附录:仔细看,您使用的是ChartFactory.createXYLineChart()
,它为域创建了一个NumberAxis
。相反,请使用ChartFactory.createTimeSeriesChart()
,它会为域创建一个DateAxis
附录:如果
time
表示与Java相同历元的毫秒数,则可以使用新日期(time.longValue())
来构造数据集的RegularTimePeriod
。这里有一个相关的例子。可能重复我之前对这些主题进行的研究,但正如您所看到的,我的设置不同,似乎无法理解。我看不出您在哪里设置了格式。请编辑您的问题,以包括一个显示您当前方法的问题。我已对其进行了编辑以适应,请帮助!谢谢:)很遗憾,它无法为您编译,因为它使用了您无法访问的数据库查询。我认为您不应该将以下代码放入循环XYPlot=(XYPlot)chart.getPlot();DateAxis=(DateAxis)plot.getDomainAxis();axis.setDateFormatOverride(新的SimpleDateFormat(“dd-MMM-yyyy”);这会引发一个错误:org.jfree.chart.axis.NumberAxis无法强制转换为org.jfree.chart.axis.DateAxis
系列是否必须是不同的类型?我尝试了你的建议,但我得到了错误无法解决系列添加(新日期(time.longValue()),强度)行上的方法“add Date,Long”
代码>我已经用我尝试过的新方法更新了我的问题!对,日期
不是一个正常时间段
;您必须选择一个,例如新日期(新日期(time.longValue())
。不再猜测;张贴一个完整的例子。
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);
}
}
}
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));