2

I'm trying to format my report to only show 2 decimal places with percentage through my JRXML report, but even with a class customizer, it's not working and when it displays on PDF file.

My code (customizer class):

       import net.sf.jasperreports.engine.JRChart;
       import net.sf.jasperreports.engine.JRChartCustomizer;
       import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
       import org.jfree.chart.plot.CategoryPlot;
       import org.jfree.chart.renderer.category.BarRenderer;

       public void customize(JFreeChart chart, JRChart jasperChart){
        CategoryPlot barPlot = (CategoryPlot) chart.getPlot();
        BarRenderer renderer = (BarRenderer) barPlot.getRenderer();

        NumberFormat formatter = new DecimalFormat("#0.00%");
        formatter.setMinimumFractionDigits(2);
        StandardCategoryItemLabelGenerator scilg2 = new StandardCategoryItemLabelGenerator("{0} {2}", formatter);
        renderer.setItemLabelGenerator(scilg2);
       }

I already searched on every forum, but the answers didn't worked, most of them were applied to Pie chart, but that's not my case.

PS: I'm using JFreeChart 1.0.0 version and iReport 1.2.8.

Falion
  • 247
  • 1
  • 11
  • @AlexK, on another chart (Pie chart) with the same report, it returns correctly. But when those same variables are on the CategoryPlot chart, it happens this scenario. – Falion Aug 30 '21 at 16:06
  • 1
    It is better to post small *jrxml* with some test data (csv file, for example) to reproduce the problem – Alex K Aug 30 '21 at 16:24
  • @trashgod, I'm going to test your post from https://stackoverflow.com/questions/53963201/jfreechart-how-to-add-percentage-to-top-of-each-bar-and-format-domain-axis-x/53966609#53966609, and see what results give me. – Falion Aug 30 '21 at 16:32

2 Answers2

2

Use the ArgumentIndex value {3}, seen here, and supply a separate percentFormatter. Using v.1.5.3, I added the following to BarChartDemo1 to get the image shown:

plot.setOrientation(PlotOrientation.HORIZONTAL);
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(2);
renderer.setDefaultItemLabelGenerator(
    new StandardCategoryItemLabelGenerator(
        "{0} {1} {2} {3}", NumberFormat.getInstance(), percent));
renderer.setDefaultItemLabelsVisible(true);

HORIZONTAL

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for your help @trashgod, I tried the code and it said "The method setDefaultItemLabelGenerator(StandardCategoryItemLabelGenerator) is undefined for the type BarRenderer" and on StandardCategoryItemLabelGenerator he doesn't let me add three parameters, only two, I can either add NumberFormat.getInstance() or percent. Is there some way to make around this? – Falion Aug 30 '21 at 16:53
  • 1
    It looks like you're using a previous version; I'd [migrated](https://github.com/jfree/jfreechart#migration-from-jfreechart-10x) to v1.5.3; try `setBase…`; I'm not sure when the four-parameter constructor was added. – trashgod Aug 30 '21 at 16:57
  • 1
    He let me use setBase and on StandardCategoryItemLabelGenerator I can add the ArgumentIndex and percent, without the NumberFormat.getInstance() inside the method. I'm going to try this way and see if it works and return later with the results :) – Falion Aug 30 '21 at 16:58
  • It didn't worked :(, but thanks for your help. It kept the same result as before with long numbers, I don't know why on CategoryPlot that doesn't work and on Pie plot it does. – Falion Aug 30 '21 at 19:21
  • Sorry to hear this; I know it works with v1.5.3, shown above. – trashgod Aug 30 '21 at 23:38
2

I'm using JFreeChart 1.0.0 version and iReport 1.2.8.

So I have finally been able to format the numbers by using this code below—posting so if someone ends up with the same problem as I was, they can correct it.

NumberFormat formatter = new DecimalFormat("#0.00'%'");
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
StandardCategoryItemLabelGenerator base = new StandardCategoryItemLabelGenerator("{2}",formatter);
renderer.setBaseItemLabelGenerator(base);
barPlot.setRenderer(renderer);

You have to use the ArgumentIndex value {2}, and after that you need to use the method setRenderer() to send a change event to all registered listeners.

Thanks @trashgod (https://stackoverflow.com/users/230513/trashgod) for your help, I got to work around your code and display the results on previous versions of the JFreeChart.

Links:

CategoryPlot - setRenderer

AbstractCategoryItemLabelGenerator

Falion
  • 247
  • 1
  • 11