java JasperSoft Studio(6.8),pieChart元素的自定义和动态颜色

ykejflvf  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(162)

我正在使用JasperSoft Studio version 6.8。我正在尝试创建一个动态颜色的pieChart。我希望每个标签('Verificata dal cliente',' Aperta ','Situazione invariata '等...)始终具有相同的颜色。
问题是,我并不总是有所有这些标签(否则我会在图表选项中添加一个固定的颜色系列),但我需要每个标签总是有相同的颜色,不管有多少标签(例如,'Aperta'总是红色,尽管有2个标签或10个)

有什么方法可以做到吗?我不太擅长用java定制图表,但我想这就是方法...有什么帮助吗?
编辑:我设法创建了一个定制器:

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());
        }
    }        
}

从那里我不知道如何管理和操作饼图数据集,给予每个Status始终具有相同的颜色。
有什么建议吗?

rggaifut

rggaifut1#

如果有人像我一样挣扎,我设法解决了这个问题:

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);
            }
        }

    }        
}

相关问题