How to set the time series of a series of histograms?
I have a complex histogram in JasperSoft 6.3.1 in eclipse and I am trying to display colors based on a series. The chart displays random colors instead of assigning one color to a specific series.
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>
....
I am trying to assign the color of a chart series to a specific series name using an if statement. How can I achieve this in jasper reports ?.
If the series name is JANUARY, the color should be black, and if there is no data for JANUARY, black should not be used.
source to share
As I understand it, you notice that you can not do if the manual tags xml, jrxml will simply not compile because it is invalid xml.
The solution is to implement your own JRChartCustomizer
Example
Java
Find a different series name and set Paint
to render based on name
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
Set the customizerClass
fully qualified package name attribute in the plot tag
<barChart>
<chart evaluationTime="Report" customizerClass="my.custom.BarColorCustomizer">
....
</barChart>
source to share