DateAxis tick labels for regular intervals

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
conwaypm
Posts: 11
Joined: Wed Dec 21, 2011 2:49 pm
antibot: No, of course not.

DateAxis tick labels for regular intervals

Post by conwaypm » Wed Dec 21, 2011 3:05 pm

Hello! I have a TimeSeriesChart with a DateAxis as the domain axis. I am trying to get the axis to display tick labels for constant intervals of time. The input data (as an XYDataset) consists of numbers for each month for 5 years. Ie:

01/01/2001 5
01/02/2001 8
01/03/2001 21
......
01/01/2002 6
01/02/2002 8
.......

and so on for fives years worth of values for each month. What I wish to do is only display the tick labels for same month each year. Ie;

JAN 2001 ------ JAN 2002 ------ JAN 2003------

I was wondering how I would achieve this? Currently it seems to just pick intervals based on what it can fit on the axis. I am working on code written by someone else and the client is wanting this done pretty quickly so hopefully there's an easy way to make this work. If any more information is required then please ask. I have looked at PeriodAxis but I'm not sure if this is what I'm after (plus it might not be too easy to just drop this in instead of the DateAxis depending on what else depends on this code). Thanks for any help you can give me!

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: DateAxis tick labels for regular intervals

Post by matinh » Wed Dec 21, 2011 8:11 pm

Have a look at the PeriodAxis.

hth,
- martin

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Re: DateAxis tick labels for regular intervals

Post by mhilpert » Thu Aug 09, 2012 3:28 pm

I have the same problem for a line chart! The lines should show all month values but the axis only should show each year. Unfortunately, JFreeChart shows as many (same) years as there is space for it, e.g.

2008 2008 2008 2009 2009 2009 2010 2010 2010

As the tick label only has format 'yyyy' to only show the years. What I would like to have (for same time period 'month' for values:

2008 2009 2010

Is this possible?
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: DateAxis tick labels for regular intervals

Post by matinh » Thu Aug 09, 2012 3:38 pm

To my knowledge, this can't be done with a DateAxis. But did you have a look at the PeriodAxis?

hth,
- martin

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Re: DateAxis tick labels for regular intervals

Post by mhilpert » Thu Aug 09, 2012 3:45 pm

PeriodAxis ... never heard of it. Do you have an example of its usage?
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: DateAxis tick labels for regular intervals

Post by matinh » Thu Aug 09, 2012 3:58 pm

Start up the demo and open "TimeSeries Charts". There are a few demos of the PeriodAxis.
I don't have code at hand right now, but search the forum and use the JavaDoc. It's quite easy.

- martin

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Re: DateAxis tick labels for regular intervals

Post by mhilpert » Thu Aug 09, 2012 4:10 pm

I wrote a test chart:

Code: Select all

        JFreeChart result = null;
        
        result = ChartFactory.createTimeSeriesChart("Bar Chart 2", "X-Title", "Y-Title", getTimeSeriesCollection1(), true, true, false);
        
        //time series chart has a line renderer as default, so change it to a bar renderer:
        final XYPlot xyp = result.getXYPlot();
        final XYBarRenderer r = new XYBarRenderer();
        r.setMargin(0.2); //10 % space between bars
        xyp.setRenderer(r);
        
        //x axis label format:
        final DateAxis da = (DateAxis) xyp.getDomainAxis();
        da.setStandardTickUnits(getTickUnitSource(new Month(), "yyyy", null)); //show x axis dates only in years
        
        //replace axis with PeriodAxis to have only 1 x tick label per year:
        PeriodAxis pa = new PeriodAxis("PeriodAxisLabel", new Month(ICUtils.toDate("20040101", "yyyyMMdd")), new Month(ICUtils.toDate("20121231", "yyyyMMdd")));
        xyp.setDomainAxis(pa);
But with this code, the x axis now shows 2 (!) lines, the first containing the months and the second conatining the years (like I expecetd them to appear). Now I want to get rid of the first line with the month names.

In the javadoc (http://www.jfree.org/jfreechart/api/javadoc/) of PerioAxis I can't see a method to disable the months.
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: DateAxis tick labels for regular intervals

Post by matinh » Thu Aug 09, 2012 4:23 pm

It's been a while since I used this...
What if you replace "new Month()" with "new Year(...)"?

- martin

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Re: DateAxis tick labels for regular intervals

Post by mhilpert » Thu Aug 09, 2012 4:25 pm

Finally got it:

Code: Select all

        //replace axis with PeriodAxis to have only 1 x tick label per year:
        PeriodAxis pa = new PeriodAxis("PeriodAxisLabel", new Month(ICUtils.toDate("20040101", "yyyyMMdd")), new Month(ICUtils.toDate("20121231", "yyyyMMdd")));
        PeriodAxisLabelInfo[] info = new PeriodAxisLabelInfo[1];
        info[0] = new PeriodAxisLabelInfo(Year.class, new SimpleDateFormat("yyyy")); //must be Year.class!
        pa.setLabelInfo(info);
        xyp.setDomainAxis(pa);
Thank you very much!
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

Locked