Search code examples
javajfreechart

Value is getting multiply by 100 when using percentage on value axis bar chart JfreeChart


I am using JfreeChart 1.0.15 library.
I want to show percentage score of the college and branches, so I used setNumberFormatOverride() method

final NumberAxis valueAxis = new NumberAxis("Percentage Score");
valueAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());

It is showing me the % sign on the Y-axis but all the values are getting multiplied by 100.

Actual output

See the Y-axis values. Label values on the bars are correct.

If I divide each value by 100 then Y-axis values are correct (e.g. 15%,19%,25%,27%) but label values are displaying wrong (e.g. 0.15,0.19,0.25,0.27).

Below code also won't give the desired output

DecimalFormat pctFormat = new DecimalFormat("#.0%");
valueAxis.setNumberFormatOverride(pctFormat);

I tried different solutions from

none of them worked.
If you need more information let me know.


Solution

  • Thanks @Keppil. I managed to solve from your valuable comment

    I was using below code to set label

    barPlot.getRenderer().setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    barPlot.getRenderer().setBaseItemLabelsVisible(true);
    

    The mistake was invoking default constructor StandardCategoryItemLabelGenerator().
    See question labels are not in percent % format.

    I solved by passing NumberFormat instance pctFormat to the parameterized constructor StandardCategoryItemLabelGenerator(java.lang.String labelFormat, java.text.NumberFormat formatter)

    barPlot.getRenderer().setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",pctFormat));
    barPlot.getRenderer().setBaseItemLabelsVisible(true);
    

    Correct output

    Now format has been applied to the labels also.