我正在使用
JasperSoft Studio version 6.8
。我正在尝试创建一个具有动态颜色的饼图。我希望每个标签(“Verificata dal cliente”、“Aperta”、“Situazione invariata”等)始终具有相同的颜色。 有什么办法吗?我不太擅长 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
总是相同的颜色。 如果有人像我一样挣扎,我设法解决了这个问题:
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);
}
}
}
}