我在日食中的JasperSoft 6.3.1中有一个堆积条形图,我正在尝试根据系列显示颜色。图表显示随机颜色,而不是为特定系列指定单一颜色。
JRXML
<categorySeries>
<seriesExpression><![CDATA[$F{name}]]></seriesExpression>
<categoryExpression><![CDATA[$F{time}]]></categoryExpression>
<valueExpression><![CDATA[$F{value}]]></valueExpression>
</categorySeries>
</categoryDataset>
<barPlot>
<plot>
<seriesColor $F{name}.equals("JANUARY")?color="#756D72":color="" seriesOrder="0" />
<seriesColor $F{name}.equals("MARCH")?color="#4B5154":color="" seriesOrder="1" />
<seriesColor $F{name}.equals("JUNE")?color="#090A09":color="" seriesOrder="2"/>
</plot>
<itemLabel/>
<categoryAxisFormat>
....
我正在尝试使用if语句将图表系列颜色指定给特定的系列名称。我如何在碧玉报告中实现这一目标?
如果系列名称为JANUARY,颜色应为黑色,如果JANUARY没有数据,则不应使用黑色。
答案 0 :(得分:2)
正如我猜你已经注意到的那样,你可以不做xml标签中的语句,jrxml将无法编译,因为它不再是有效的xml。
解决方案是实现您自己的JRChartCustomizer
<强>的java 强>
查找不同的系列名称,并根据名称
将Paint
设置为渲染器
public class BarColorCustomizer implements JRChartCustomizer {
@Override
public void customize(JFreeChart jfchart, JRChart jrchart) {
//Get the plot
CategoryPlot plot = jfchart.getCategoryPlot();
//Get the dataset
CategoryDataset dataSet = plot.getDataset();
//Loop the row count (our series)
int rowCount = dataSet.getRowCount();
for (int i = 0; i < rowCount; i++) {
Comparable<?> rowKey = dataSet.getRowKey(i);
//Get a custom paint for our series key
Paint p = getCustomPaint(rowKey);
if (p!=null){
//set the new paint to the renderer
plot.getRenderer().setSeriesPaint(i, p);
}
}
}
//Example of simple implementation returning Color on basis of value
private Paint getCustomPaint(Comparable<?> rowKey) {
if ("JANUARY".equals(rowKey)){
return Color.BLACK;
}
return null;
}
}
<强> JRXML 强>
在图表标记
上设置包含完整包名的customizerClass
属性
<barChart>
<chart evaluationTime="Report" customizerClass="my.custom.BarColorCustomizer">
....
</barChart>