如何动态设置堆叠条形图系列颜色?
问题描述:
在eclipse中我在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,颜色应该是黑色,如果1月份没有数据,则不应使用黑色。
答
正如我想你已经注意到了,你可以不做如果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>
WOOW的作品完美。你救了我的一天。我经历了很多链接,并且找不到解决方案。对不起,再次询问是否可以隐藏类别颜色栏,如果它是空的。 – joseph
@joseph隐藏类别颜色栏?,类别是一组系列?,我无法真正理解您的意思,无论如何,您可以根据您的喜好设置颜色条形和轮廓,因此将其设置为背景并设置为你不会看到它;) –
@ Petter Friberg非常感谢你。你已经正确回答了我的问题,我想设置空白类别作为背景。 – joseph