Jump to content
Changes to the Jaspersoft community edition download ×

Conditional color for numbers in report


vanhuet

Recommended Posts

Hi,

 

I'm pretty new to iReports and have been searching on the internet now for a long time. I found solutions for most of my "problems", but one remains.

 

I want to display the numbers in my report in black if they are positive. When they're negative I want them red. I already format them with patterns etc and this all works. Only thing is the color.

 

It sounds like a really easy thing, but I've been trying for ages now with conditions at the style for the table cell. I really am lost now. Can somebody give me an example how to do this?

 

Thanks!!

Link to comment
Share on other sites

  • Replies 10
  • Created
  • Last Reply

Top Posters In This Topic

vanhuet,

 

i saw your post and thought of having some fun by making a sample with your requirements. check out the attached .jrxml file and modify/experiment on it as much as you like. just take note of the conditional expressions for each the two fields and the variable that i used.

basically i used the <style> </style> tags and the forecolor attribute to control the text color.

i just used hex values for the color (#0000ff is blue and #ff0000 is red). any questions... just post it here. cool? Have fun.

 

Edwin [file name=conditional_coloring.jrxml size=6002]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/conditional_coloring.jrxml[/file]

Link to comment
Share on other sites

mauri,

 

I got it! :)

I modified the file i previously posted and saved it as

conditional_coloring_java.jrxml

It does the same thing as the previous one but this one uses Java instead of Groovy.

 

Code for one of the text elements:

Code:
$P{number2}.intValue() < 0 
? "<style forecolor='#ff0000'>" + $P{number2}.toString() + "</style>"
: "<style forecolor='#0000ff'>" + $P{number2}.toString() + "</style>"

I had to look up how to do it but thankfully, the Java5 API was a lot of help.

:)

 

I need to go so just look at the file.

Note how the parameters are "unboxed" from an Integer to an int and "boxed" back from an int to an Integer type.

 

Let me know if this helped or if you have any questions about what I changed.

Enjoy!

 

edwin [file name=conditional_coloring_java.jrxml size=5923]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/conditional_coloring_java.jrxml[/file]

 

Post edited by: edwin, at: 2007/05/18 13:26

Post edited by: edwin, at: 2007/05/18 13:29

Link to comment
Share on other sites

I make how below get Data from Database:

 

$F{EMPLOYEE_SALARY}.doubleValue() > 50000?

"<style forecolor='#ff0000'>" + $F{EMPLOYEE_SALARY}.toString() + "</style>" :

"<style forecolor='#0000ff'>" + $F{EMPLOYEE_SALARY}.toString() + "</style>"

 

When show the report it put how image in attached !

 

 

 

IReport 1.3.3

 

What is wrong ?

Link to comment
Share on other sites

i'm thinking you didn't set the "Is styled text" property to true by checking that option for both your textFields.

 

right click each of your textFields.

click Properties.

click the Font tab.

check the "Is styled text" option.

 

that might just be the thing to make it work.

let me know how it goes.

 

edwin

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...