Custom cell coloring in the JasperReports crosstab cell

In our application we have a set of common colours which are used on the reports. These colours can be rather dynamic and are stored in the properties file. Once some chart is rendered – the colour is loaded from the file and the used to create chart.

It’s rather trivial to do this for the chart. Just specify JRChartCustomizer and do everything you want.
It’s not so trivial to change the background colour of the cells in the JasperReports crosstab. So let me describe how it was done in the version 3.0.1 of JasperReports.

Unfortunately conditional styles could not be used in our case. We are not going to specify the smae scope of colours across multiple report templates, it will become a huge pain in case of changes.

May be you know that generating JasperReports report includes three steps:
1. Compilation of the JRXML file into binary JASPER file.
2. Filling compiled JASPER file with some data for example DB data. As result you get JasperPrint object.
3. Exporting JasperPrint into some format like PDF/RTF/HTML/etc…

So the main idea here is to update JasperPrint object generated on step 2. JasperPrint contains all of the report contents and you can work with it.
Lets get deeper into the code

I guess that code has enough comments, so let see how the result looks like in the reality.

Of course this grid looks like a bit weird and I’ll continue making more beautiful, but it works. 🙂