本文介绍了将日期/时间添加到JFreeChart图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我目前有一个查询数据库值的方法,并将它们绘制成图形。唯一的问题是,时间变量很长,导致我的图形看起来像这样: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:我想转换它到日期格式,然后将其添加到图表中。I want to convert it to a date format and then add it to the 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);以下是我添加到图表的方法: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!编辑:我已将我的代码更改为: 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);我的新GetDustLevels()方法:And my new GetDustLevels() method: 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 或所需的格式,我只是在猜测。Without an sscce or the desired format, I'm just guessing.XYPlot plot = (XYPlot) chart.getPlot();DateAxis axis = (DateAxis) plot.getDomainAxis();axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));附录:仔细观察,你正在使用 ChartFactory.createXYLineChart(),为域创建 NumberAxis 。相反,使用 ChartFactory.createTimeSeriesChart(),为域创建 DateAxis 。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.附录:如果 time 表示与Java相同时期的毫秒 日期 ,你可以使用新日期(time.longValue())构建数据集的 RegularTimePeriod 。有一个相关的例子这里。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. 这篇关于将日期/时间添加到JFreeChart图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-04 13:01