Adding date/time to JFreeChart graph

java, jfreechart

Solution

Without an sscce or the desired format, I'm just guessing at a suitable `DateFormat`.

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

Addendum: Looking closer at your update, you're using `ChartFactory.createXYLineChart()`, which creates a `NumberAxis` for the domain. Instead, use `ChartFactory.createTimeSeriesChart()`, which creates a `DateAxis` for the domain.

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.

Problem

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. 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: ``` 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); } } } ```

Original source

Related problems