Adding date/time to JFreeChart graph
Asked Answered
S

1

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:

graph

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

    }


}
Suiter answered 4/9, 2012 at 23:34 Comment(6)
Possible duplicate of Display days in TimeSeriesChartPaynter
I did research into these topics previously, but as you can see I have set mine up differently, and can't seem to figure it out.Suiter
I don't see where you set the format. Please edit your question to include an sscce that shows your current approach.Paynter
I have edited it to suit, please help! Thank you :) Unfortunately, it will not be compilable for you, as it uses database queries not accessible to you.Suiter
I think you shouldn't put following code in loop XYPlot plot = (XYPlot) chart.getPlot(); DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));Granlund
Cross-posted here.Paynter
P
6

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.

Paynter answered 5/9, 2012 at 0:40 Comment(5)
This throws an error: org.jfree.chart.axis.NumberAxis cannot be cast to org.jfree.chart.axis.DateAxis Does series have to be of different type?Suiter
I have tried your suggestion but I get the error Cannot resolve method 'add Date, Long' on the line series.add(new Date(time.longValue()), intensity); I have updated my question to the new way I've tried!Suiter
Right, Date is not a RegularTimePeriod; you must choose one, e.g. new Day(new Date(time.longValue())). No more guessing; post a complete example.Paynter
why is there three MsFroh
It's an arbitrary SimpleDateFormat; more above.Paynter

© 2022 - 2024 — McMap. All rights reserved.