Search code examples
javajasper-reportsjfreechart

BarChart set values axis increment


I am using JasperReports to generate a report with charts. One of them is a bar chart and I'd like to set the increment of the axis values in 20 units.

You can see my previous approach at this link: BarChart bar value labels are hidden by the margin

This is my customizer class:

    public class BarChartCustomizer extends JRAbstractChartCustomizer {

    @Override
    public void customize(JFreeChart jFreeChart, JRChart jrChart) {

        CategoryPlot plot = (CategoryPlot) jFreeChart.getPlot();
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);
        rangeAxis.setNumberFormatOverride(numberFormat);
        BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
        barRenderer.setItemMargin(0.0);
        rangeAxis.setUpperMargin(0.20);
        rangeAxis.setAutoRange(true);
        CategoryAxis categoryAxis = plot.getDomainAxis();
        categoryAxis.setAxisLineStroke(new BasicStroke(2f)); 
        rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAxisLineStroke(new BasicStroke(2f));
    }
}

I tried with rangeAxis.setMinorTickCount(20); with no success.

As usual, the final solution will be included in the sample code located in my GitHub repository: https://github.com/MichaelKnight/jaspertest.git


Solution

  • Try this

    rangeAxis.setTickUnit(new NumberTickUnit(20));
    

    Also see the net.sf.jasperreports.chart.range.axis.tick.interval property (documented at http://jasperreports.sourceforge.net/config.reference.html#net.sf.jasperreports.chart.range.axis.tick.interval), which does exactly that.