I’m having difficulty figuring out how to set the tick unit size for the Y-axis of a chart (or X-axis).
I am able to obtain the tick unit size, but I can’t figure out how to set it.
I found the attribute:
chart.getXYPlot().getRangeAxis().setTickUnit()
which takes 1 or 3 arguments. I assume 1 argument would be the tick size, and 3 arguments would be the lower bound, upper bound and tick size.
The EasyChart is not a ChartPanel subclass like the Classic Chart. Its ChartPanel is nested one level in. Use .getChart() on the EasyChart to get it. Or do this in the configureChart() method, where the chart variable is exposed for you.
Using the chart variable in the configureChart method, I have this code:
from org.jfree.chart.axis import NumberTickUnit
chart.getXYPlot().getRangeAxis().setTickUnit(NumberTickUnit(5))
I have broken it down piece by piece…
plot = chart.getXYPlot()
print plot prints org.jfree.chart.plot.CombinedDomainXYPlot@…
axis = plot.getRangeAxis()
print axis prints None.
I called getRangeAxisCount and it returns 1, so it knows that there is a range axis. I’ve tried calling getRangeAxis(0), getRangeAxis(1) and getRangeAxis(2), in case it’s as simple as needing to directly specify the range axis, but each time it returned none.
I also tried your code outside the configureChart method:
from org.jfree.chart.axis import NumberTickUnit
chart = trend.getComponent(0).getComponent(0)
print chart prints ‘org.jfree.chart.JFreeChart@…’
Then adding on plot = chart.getXYPlot() throws the error: org.jfree.chart.plot.PiePlot cannot be cast to org.jfree.chart.plot.XYPlot
which has me stumped, because this is an Easy Chart, not a Pie Chart.
The XYPlot you’re looking for is probably one of the subplots of the CombinedDomainXYPlot. Get the list, iterate through it, check whether one is the XYPlot you’re looking for.
I ended up creating a new easy chart object, and trying the code out in there, just to see if there was something funny going on with the existing Easy Chart. Either of the methods above worked without any further modification, so it’s obviously something to do with the configuration of my existing Easy Chart.
Ultimately I’m actually trying to modify the X axis ticks, not the Y axis ticks. I just wanted to take it one step at a time and get the Y axis working off the example, then move onto the X axis. Having made the Y axis work on a new easy chart, I modified it to get the X axis working on the new chart. Then I transposed the code across to the old easy chart, and it works there too. Y axis still doesn’t, but as I say, the Y axis ticks are managed a different way so all is under control.