0
votes

For Jasper Report 4.5

How can I access bar chart label expression? I have tried ChartCustomizer, we can access label via SeriesItemLabelGenerator. But when this generator executes, it does not have formula we have set on jrxml file for <labelExpression> field e.g. [barValue$ColorCode$]. I found that <labelExpression> are been executed in CategoryLabelGenerator class, which is JasperReport class.

What I want to achieve is I am passing color code from dataset along with bar value with format [barValue$ColorCode$]. I want to pick $ColorCode$ and get that value and then change label value to [barValue]. ColorCode will be used to assign bar color from ChartCustomizer. But thing is when I access labelExpression from StandardCategoryItemLabelGenerator or AbstractCategoryItemLabelGenerator then it does returns [barValue] from generateLabelString() and generateLabel() method.

Only intention is to use ColorCode passed from dataset. If we only pass ColorCode instead of [barValue$ColorCode$], then only report shows [barValue] only. I think that we can customize CategoryLabelGenerator class then it would solve my problem. But I don't know how can we customize CategoryLabelGenerator.

Any input/help achieving this goal is greatly appreciated.

Thank you.

1
May be you are trying to get a value from the jrxml . You can extend the JRAbstractChartCustomizer class and get the value as given below public class BarChartIndustryMonitor extends JRAbstractChartCustomizer { public void customize(JFreeChart chart, JRChart jasperChart) { BarRenderer render = (BarRenderer) plot.getRenderer(); String chartColor = (String)getParameterValue("chartColor"); if("GREEN".equalsIgnoreCase(chartColor)){ // your code }else if("BLUE".equalsIgnoreCase(chartColor)){ // your code }else{ // your code } } }Navankur Chauhan
Have already explored JRAbstractChartCustomizer. Actually this does not work out because color records are different for each row. So it has to iterate through fields of dataset. Is there any way we can pass fields which are not in dataset (specifically CategoryDataset) to the ChartCustomizer?Parth Bhagat

1 Answers

1
votes

After some digging in to source code of JasperReport and jFreeChart documentations found a way to achieve this goal. Writing code down here. Implemented JRChartCustomizer interface.

public void customize(JFreeChart chart, JRChart jasperChart) {

    CategoryPlot plot = chart.getCategoryPlot();

    CategoryDataset dataset = plot.getDataset();

    CategoryLabelGenerator categoryLabelGenerator = null;

    if (jasperChart.getDataset() instanceof JRFillChartDataset) {
        JRFillChartDataset jrFillChartDataset = (JRFillChartDataset) jasperChart.getDataset();

        categoryLabelGenerator = (CategoryLabelGenerator) jrFillChartDataset.getLabelGenerator();
    }

    if (categoryLabelGenerator != null) {
        for (int i = 0; i < dataset.getRowCount(); i++) {
            for (int j = 0; j < dataset.getColumnCount(); j++) {
                String generatedLabel = categoryLabelGenerator.generateLabel(dataset, i, j);
            }
        }
    }
}