Strange behaviour in x-axis of TimeSeries chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
clafar
Posts: 3
Joined: Mon Sep 12, 2016 12:45 pm
antibot: No, of course not.

Strange behaviour in x-axis of TimeSeries chart

Post by clafar » Mon Sep 12, 2016 1:17 pm

Hi everyone,

Apologies if this issue has already been addressed but I am pretty new to JFreeChart.

I am currently experimenting with a TimeSeries Chart and I have a data set with the date and time and a temperature value. The date/time information does not have a uniform interval and I think this might be confusing JFreeChart. The output result is a graph with the date in the x-axis having date values which are out of bounds, eg. 59th February and 74th March. On the otherhand ToolTipText gives correct date and time information.

Can anyone help please? Is there a way I can set limits to the date values or is there a way to manually set the date ranges?

This is my output:
http://tinyurl.com/jynwwu2

This is my source code:

Code: Select all

public class ChartDemo extends ApplicationFrame {

    public ChartDemo(String title) {
        super(title);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);
    }

    private static JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Temperature Profile","Date","Temperature",dataset,true,true,false);

        XYPlot plot = (XYPlot) chart.getPlot();

        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;

            renderer.setSeriesShapesVisible(0, true);
            renderer.setSeriesShapesFilled(0, true);

        }

        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("DD MMM YY"));

        return chart;

    }

    private static XYDataset createDataset() {

        TimeSeries s1 = new TimeSeries("Part Name");

        //sec,min,hr,day,mon,yr
        s1.add(new Second(00, 01, 06, 01, 01, 2016), 104.0);
        s1.add(new Second(00, 30, 12, 29, 01, 2016), 105.0);
        s1.add(new Second(00, 15, 00, 01, 02, 2016), 37.0);
        s1.add(new Second(00, 11, 23, 02, 02, 2016), 133.0);
        s1.add(new Second(00, 12, 11, 16, 02, 2016), 20.0);
        s1.add(new Second(00, 45, 15, 01, 03, 2016), 20.0);
        s1.add(new Second(00, 01, 21, 11, 03, 2016), 86.0);
        s1.add(new Second(00, 40, 17, 26, 03, 2016), 125.0);

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);

        return dataset;

    }

    public static void main(String[] args) {

        ChartDemo demo = new ChartDemo("Temperature Profile Chart");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}

clafar
Posts: 3
Joined: Mon Sep 12, 2016 12:45 pm
antibot: No, of course not.

Re: Strange behaviour in x-axis of TimeSeries chart

Post by clafar » Mon Sep 12, 2016 4:32 pm

I changed my dummy data to have a uniform time interval just to see if it works, but the issue still persists. Any help and ideas will be greatly appreciated because I have no idea how to solve this.

Code: Select all

 //min,hr,day,mon,yr 
         //uniform intervals, one data point per month
        s1.add(new Minute( 30, 12, 01, 1, 2016), 104.0);
        s1.add(new Minute( 30, 12, 01, 2, 2016), 105.0);
        s1.add(new Minute( 30, 12, 01, 3, 2016), 37.0);
        s1.add(new Minute( 30, 12, 01, 4, 2016), 133.0);
        s1.add(new Minute( 30, 12, 01, 5, 2016), 20.0);
        s1.add(new Minute( 30, 12, 01, 6, 2016), 20.0);
        s1.add(new Minute( 30, 12, 01, 7, 2016), 86.0);
        s1.add(new Minute( 30, 12, 01, 8, 2016), 125.0);

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Strange behaviour in x-axis of TimeSeries chart

Post by paradoxoff » Tue Sep 13, 2016 10:46 am

This has nothing to do with the data, but rather with the DateFormat that is used to convert the date values of the tick labels to String.
Your "days" look like "days in year", but what you probably want are "days in month". In a SimpleDateFormat, the pattern for the former is "D" (thisd is what you have apparently used), for the latter it is "d" (which you should use).

clafar
Posts: 3
Joined: Mon Sep 12, 2016 12:45 pm
antibot: No, of course not.

Re: Strange behaviour in x-axis of TimeSeries chart

Post by clafar » Tue Sep 13, 2016 11:06 am

Such a simple solution! Omg Thank you, I wasn't aware of that! I have changed my Ds to lower case ds and the graph looks perfect now.

Thanks again!

Locked