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.
There's a discussion at the JasperForge forums on the topic, this is probably the most relevant part:
First drag and drop the field that you want to have dyanamic color twice. Change background color for first field to Yellow and the second field to Red. Now right click Yellow field, click Properties, select Common tab and write this code in the 'Print when expression' box: (make sure to replace myconditionalfield with your actual field name) $F{MyConditinalField}.intValue()>= 5 && $F{MyConditinalField}.intValue()<10?Boolean.TRUE:Boolean.FALSE //if >=5 and <10 then show Yellow field //Code for Red field at the same place just like above $F{MyConditinalField}.intValue()>= 10?Boolean.TRUE:Boolean.FALSE Hope this helps.
It's not exactly what you need, but might likely be close enough to push you in the right direction.
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.
Example to color the even row and odd row is below , hope that help
<style name="BackgroundStyle" mode="Opaque" backcolor="#FFFFFF">
<conditionalStyle>
<conditionExpression><![CDATA[Boolean.valueOf($V{REPORT_COUNT}.intValue() % 2 == 0)]]></conditionExpression>
<style backcolor="#CCFFCC"/>
</conditionalStyle>
</style>
And assign style to each column , worked with me :)