DateAxis tick labels for regular intervals
DateAxis tick labels for regular intervals
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!
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!
Re: DateAxis tick labels for regular intervals
Have a look at the PeriodAxis.
hth,
- martin
hth,
- martin
Re: DateAxis tick labels for regular intervals
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?
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
Re: DateAxis tick labels for regular intervals
To my knowledge, this can't be done with a DateAxis. But did you have a look at the PeriodAxis?
hth,
- martin
hth,
- martin
Re: DateAxis tick labels for regular intervals
PeriodAxis ... never heard of it. Do you have an example of its usage?
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0
Re: DateAxis tick labels for regular intervals
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
I don't have code at hand right now, but search the forum and use the JavaDoc. It's quite easy.
- martin
Re: DateAxis tick labels for regular intervals
I wrote a test chart:
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.
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);
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
Re: DateAxis tick labels for regular intervals
It's been a while since I used this...
What if you replace "new Month()" with "new Year(...)"?
- martin
What if you replace "new Month()" with "new Year(...)"?
- martin
Re: DateAxis tick labels for regular intervals
Finally got it:
Thank you very much!
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);
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0