How to display the bar value for each column in a histogram?

There is a need to see the exact value that the bar represents on the histogram, as opposed to the approximate value along the Y axis.

How is this possible?

Thank you for reading!

+4
source share
1 answer

In iReport 3.7.6, you can simply check the Show Shortcuts box in the BarPlot properties.

In the previous version (3.1.4), I had to create the ChartCustomizer class.

If you need to format the number in the label (for example, as a percentage, add a thousands separator, etc.), you will definitely need a ChartCustomizer, even in the new version.

A simple example:

import java.text.NumberFormat; import net.sf.jasperreports.engine.JRChart; import net.sf.jasperreports.engine.JRChartCustomizer; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.AbstractCategoryItemLabelGenerator; import org.jfree.chart.labels.CategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.data.category.CategoryDataset; public class SimpleBarChartCustomizer implements JRChartCustomizer { public void customize(JFreeChart chart, JRChart jasperChart) { CategoryPlot plot = chart.getCategoryPlot(); BarRenderer renderer = (BarRenderer) plot.getRenderer(); CategoryItemRenderer catRenderer = ((CategoryItemRenderer)renderer); catRenderer.setBaseItemLabelGenerator(new NumberLabelGenerator("", NumberFormat.getInstance())); } static class NumberLabelGenerator extends AbstractCategoryItemLabelGenerator implements CategoryItemLabelGenerator { public NumberLabelGenerator(String labelFormat, NumberFormat formatter, NumberFormat percentFormatter) { super(labelFormat, formatter, percentFormatter); } protected NumberLabelGenerator(String labelFormat, NumberFormat formatter) { super(labelFormat, formatter); } private NumberFormat formatter = NumberFormat.getInstance(); public String generateLabel(CategoryDataset dataset, int series, int category) { Number b = dataset.getValue(series, category); formatter.setMaximumFractionDigits(2); return formatter.format(b); } } } 
+9
source

Source: https://habr.com/ru/post/1341621/


All Articles