Search code examples
plotjfreechartpercentagestackedbarseries

JFreeChart: RangeAxis in percentage stacked bar chart is in [0,1] instead of [0,100]


In JFreeChart, I am using the setRenderAsPercentage(true) option for StackedBarRenderer . Although the plot itself looks fine (all bars span the whole plot), the range axis labels are not showing percent values (i.e. 0 to 100) but probabilities (i.e. 0 to 1).

How can I achieve percentage values?


Solution

  • You need to set a NumberFormat for the rangeAxis like this:

    NumberAxis rangeAxis = new NumberAxis("Count");
    ...
    rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
    ...
    XYPlot plot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);
    ...
    

    Or you can do this if you already have a plot

    NumberAxis rangeAxis2 = (NumberAxis) plot.getRangeAxis();
    rangeAxis2.setNumberFormatOverride(NumberFormat.getPercentInstance());
    

    You should then have an chart that looks like this:

    enter image description here