javajasper-reportspie-chartjfreechart

JasperSoft Studio (6.8), custom and dynamic color for a pieChart element


I'm using JasperSoft Studio version 6.8. I'm trying to create a pieChart with dynamic colours. I would want each label ('Verificata dal cliente', 'Aperta', 'Situazione invariata', etc...) to always have the same colour.
The problem is that I don't always have all those labels (otherwise I would add a fixed color Series in the chart options), but I need each label to always have the same colour, despire how many labels there are (for example, 'Aperta' always red, despite if there are 2 labels or 10)

enter image description here

Is there any way to do it? I'm not that good with chart customization in java, but I guess that's the way...any help?

EDIT: I managed to create a customizer:

import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
import net.sf.jasperreports.engine.JRChart;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.HashMap;

public class customColorSeries_pieChart  extends JRAbstractChartCustomizer  { 
    //Inside 'seriesColor' we will find an array of object like -> [{"Status": "Aperta", "Color": "#FFFF00"}, ...]

    @Override
    public void customize(JFreeChart chart, JRChart jasperChart) {
        PiePlot plot = (PiePlot) chart.getPlot();       
        HashMap<String, String> customMap = new HashMap<>();
        //This comes from a parameter passed from a main report to a subreport
        com.fasterxml.jackson.databind.node.ArrayNode seriesColor = (ArrayNode) getParameterValue("seriesColor_P");
        for (int z = 0; z < seriesColor.size(); z++) {
            //System.out.println(seriesColor.get(z).get("Status"));
            //System.out.println(seriesColor.get(z).get("Color"));
            
            customMap.put(seriesColor.get(z).get("Status").textValue(), seriesColor.get(z).get("Color").textValue());
        }
    }        
}

From there I don't know how to manage and manipulate the pie chart dataset to give each Status always the same color.
Any suggestion?


Solution

  • I managed to resolve this, if someone is struggling like I was:

    import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
    import net.sf.jasperreports.engine.JRChart;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.data.general.PieDataset;
    
    import com.fasterxml.jackson.databind.node.ArrayNode;
    
    import java.awt.Color;
    import java.util.HashMap;
    
    
    
    public class customColorSeries_pieChart  extends JRAbstractChartCustomizer  { 
        @Override
        public void customize(JFreeChart chart, JRChart jasperChart) {
            PiePlot plot = (PiePlot) chart.getPlot();
            PieDataset plot_dataset = plot.getDataset();
            
            HashMap<String, String> customMap = new HashMap<>();
            //Inside 'seriesColor' we will find an array of object like -> [{"Status": "Aperta", "Color": "#FFFF00"}, ...]
            //This comes from a parameter passed from a main report to a subreport
            com.fasterxml.jackson.databind.node.ArrayNode seriesColor = (ArrayNode) getParameterValue("seriesColor_P");
            
            for (int z = 0; z < seriesColor.size(); z++) {          
                customMap.put(seriesColor.get(z).get("Status").textValue(), seriesColor.get(z).get("Color").textValue());
            }
            
            
            for (int i = 0; i < plot_dataset.getItemCount(); i++) {
                if(customMap.containsKey(plot_dataset.getKey(i).toString())) {
                    plot.setSectionPaint(plot_dataset.getKey(i), Color.decode(customMap.get(plot_dataset.getKey(i).toString())));
                }
                else {
                    plot.setSectionPaint(plot_dataset.getKey(i), Color.BLACK);
                }
            }
    
        }        
    }