Setting the y-axis tick interval

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
whitegoose
Posts: 2
Joined: Thu Feb 04, 2016 7:23 am
antibot: No, of course not.

Setting the y-axis tick interval

Post by whitegoose » Thu Feb 04, 2016 7:50 am

Hi all,

My issue:
I am building a bar chart in Pentaho report designer, where depending on the parameters, the max value in the chart can be huge (say 1,000) or very small (say 3). Currently when the max range is huge, the auto-tick interval works nicely. However, when the max value is very small, I get Y Axis ticks of, for example, 0.5, 1.0, 1.5, 2.0 etc. The customer is not happy with this, as decimal (non-whole number) values do not make sense. I need to find a way to control the minimum tick interval on the Y Axis.

My question:
Can any one please provide some code which I can use to solve this problem? It would be great if you could also clarify what language the code is in (some exmaples I see are in BeanShell and others in Javascript and I can't tell the difference, and I have to select which language to use) and if there are any packages I need to import - the complete solution.

I have tracked down a few examples, like the following:

Code: Select all

NumberAxis range = (NumberAxis)plot.getRangeAxis(); 
range.setTickUnit(new NumberTickUnit(20)); 
But I can't get it to work - and as I'm a complete beginner in this area, I don't know what to put before/after/around this code (or what language it is). If I could just get this basic example working, I'm sure I could figure out a way to calculate an appropriate value for the tick interval, from the min and max values in the data set. Or maybe just round up, just to make sure the minimum interval is always >= 1.

Thanks for any help!

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Setting the y-axis tick interval

Post by david.gilbert » Thu Feb 04, 2016 12:10 pm

By default the axis will use fractional tick intervals if necessary. If you want to restrict the tick interval to integer units, you can configure the axis using:

Code: Select all

axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

whitegoose
Posts: 2
Joined: Thu Feb 04, 2016 7:23 am
antibot: No, of course not.

Re: Setting the y-axis tick interval

Post by whitegoose » Fri Feb 05, 2016 1:55 am

Fantastic - that's done it, thank you so much!

To close the loop, here is my entire code snippet:

Code: Select all

import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.axis.*;
// only use whole numbers on the Y Axis
CategoryPlot chartPlot = chart.getCategoryPlot();
ValueAxis yAxis = chartPlot.getRangeAxis();
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

Locked