在 JFreeChart 图表中添加日期/时间

3

我目前有一个从数据库中查询值并将其绘制成图形的方法。唯一的问题是,time变量是一个长整型,这导致我的图形看起来像这样:

graph

我想将它转换为日期格式,然后添加到图表中。

我该怎么做呢?

这是我的图表代码:

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);
        }

    }


}

可能是Display days in TimeSeriesChart的重复问题。 - trashgod
我之前对这些主题进行了研究,但正如您所看到的,我已经设置了不同的方式,似乎无法弄清楚。 - DommyCastles
我看不到你设置格式的地方。请编辑你的问题,包括一个 sscce,展示你目前的方法。 - trashgod
抱歉,这段代码中使用了你无法访问的数据库查询语句,所以它对你来说无法编译。 - DommyCastles
我认为你不应该将以下代码放在循环中:XYPlot plot = (XYPlot) chart.getPlot(); DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy")); - shivshankar
1个回答

6

没有一个符合要求的sscce或所需格式,我只能猜测适当的DateFormat

XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(DateFormat.getDateInstance());

附加说明:仔细查看您的更新,您正在使用ChartFactory.createXYLineChart(),它为域创建一个NumberAxis。相反,使用ChartFactory.createTimeSeriesChart(),它为域创建一个DateAxis

附加说明:如果time表示与Java Date相同纪元的毫秒数,则可以使用new Date(time.longValue())构造数据集的RegularTimePeriod。这里有一个相关的示例


这会抛出一个错误: org.jfree.chart.axis.NumberAxis 无法转换为 org.jfree.chart.axis.DateAxisseries 必须是不同类型的吗? - DommyCastles
我尝试了你的建议,但在这一行 series.add(new Date(time.longValue()), intensity); 上出现错误 Cannot resolve method 'add Date, Long'。我已经更新了我的问题,展示了我新的尝试方式! - DommyCastles
正确,Date 不是一个 RegularTimePeriod;你必须选择一个,例如 new Day(new Date(time.longValue()))。不要再猜了;发布一个完整的例子。 - trashgod
为什么有三个 M - O-9
这是一个任意的 SimpleDateFormat;更多内容请参见上文。 - trashgod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接