I’m attempting to create a bar chart that needs to use specific series colors for N number of categories and a separate color for 1 other category. Here is an example:

In the mockup, Categories 1-3 use a collection of 5 colors to render their series while Category 4 uses a single grey color. My first approach was to override the getItemPaint() method using a customizer class, but I can only figure out how to define a color on the category level, not the series level. Is it possible to define colors on category and/or series levels? Something like,
If category != "Category4"
Use colors A, B, C, D, and E in category's series
Else
Use color F in category's series
Another thought I had was to combine iReport’s Series Colors bar chart property and the getItemPaint() override; that way I could define the 5 colors to use in iReport and use the getItemPaint() override only in the case where the category equals “Category 4”. So far I’ve had no luck combining the two; if the override is defined it overrides iReport’s Series Color property.
Update with code:
import java.awt.Color;
import java.awt.Paint;
import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
import net.sf.jasperreports.engine.JRChart;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
public class CustomSeriesColors extends JRAbstractChartCustomizer {
static class CustomRenderer extends BarRenderer {
private final Paint[] colors1;
private final Paint[] colors2;
public CustomRenderer(Paint[] colors1, Paint[] colors2) {
this.colors1 = colors1;
this.colors2 = colors2;
}
//Set custom colors for series according to category
@Override
public Paint getItemPaint(int row, int column) {
CategoryDataset l_jfcDataset = getPlot().getDataset();
String l_rowKey = (String)l_jfcDataset.getRowKey(row);
String l_colKey = (String)l_jfcDataset.getColumnKey(column);
if ("Insufficient Data".equalsIgnoreCase(l_colKey)) {
return this.colors2[row % this.colors1.length];
} else {
return this.colors1[row % this.colors1.length];
}
}
}
public void customize(JFreeChart chart, JRChart jasperChart) {
// Get plot data.
CategoryPlot categoryPlot = chart.getCategoryPlot();
// Define colors to be used by series.
Color[] color1 = new Color[]{new Color(238,0,0), new Color(255,153,0), new Color(211,190,91), new Color(153,204,102), new Color(0,170,0)};
Color[] color2 = new Color[]{new Color(200,200,200)};
// Call CustomRenderer using defined color sets.
categoryPlot.setRenderer(new CustomRenderer(color1,color2));
}
}
You will need to write a custom renderer to select a ‘Paint’ based on the series and category by overriding
getItemPaintThe use your renderer update for the plot:
Here is the code in full