Saturday 3 May 2014

Change font color based on Condition in Jasper iReport – Work out with Styles


Change font color based on Condition in Jasper iReport – Work out with Styles

Some times you may get a requirement to show the values in colored based on a condition.
This we can achieve using Styles in jasper iReport.

Example : A scenario
Print values in green color if value(s)>=30  and print values in red color if value(s)<30

1) Take a simple query which gives you some numeric values( in this case it is BigDecimal).
2) From the Report Inspector, right click on Report ->Properties -> Language ->Groovy (it is by default, but you need to check).
3) From the same Report Inspector, Click on Styles and add two conditional styles.
4) In the first one write below condition in condition Expression : $F{storesales}>=30 and select fore color as green [22,180,51]
5) In the second conditional style write below condition : $F{storesales}<30 and select the fore color as red [242,41,98]
6) select the field on the detail band ( you can do the same on table component or cross tab component) and go to the properties of it and give style as style1 ( style1 is the name of the style given, you can give your own name).
7) Save the report and see the preview.

Eg JRXML file :
Click this link to download

Sample output :

 
Other References :
1) http://stackoverflow.com/questions/8754448/change-text-field-data-color-foreground-color-based-on-condition-in-jasperrepo

2) Supermart Performance Dashboard in Professional jasper server.

3) http://darkbytetechscrapbook.blogspot.in/2011/12/ireport-changing-text-colour-based-on.html

2 comments:

  1. Replies
    1. Hi Ali,

      For string fields you have to use compare string methods like "equals" or "compareTo" like this:

      $F{salesman}.equals("your string")

      Regards.

      Delete