Formatting field data color based on condition

Chandu picture Chandu · Mar 29, 2010 · Viewed 27k times · Source

I'm new to JasperReports. I'm designing report using iReport. My requirement is I have two values (Fields) x,y to compare. If x < y then the data color for y should be changed to 'black' & if greater then data color of y should be changed to 'red'.

Please advice me as to how to proceed on this & where to validate.

Answer

Jacob Schoen picture Jacob Schoen · Mar 1, 2012

I know this is an old question, but in I am assuming Jasper Reports has changed. You can now accomplish this with Conditional Styles.

In iReport you create a new Style and give it a name. You can put any default settings, such as font, color, text size, etc. in the style. Then you right click the Stlye and select Add Conditional Style. Then on the field or fields you want to apply it to you set the style to one you created.

So in your example I would set the Forecolor in the main stlye to be black, set the Condition Expression in the Conditional Style to be

$F{y} > $F{x}

and the Forecolor in the conditional Stlye to red. Then in the detail section where you have the y field placed set the style to the one we created.