Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Coloring the rows of a table doesn't work
ReinerK
I have a table where the data rows know which background-color they should have (column "colorcode", int). So I try to set the background-color in the onCreate-Script of the table row:
hex = Integer.toHexString(this.getRowData().getColumnValue("colorcode"));
this.getStyle().backgroundColor = "0x" + hex;
This doesn't work. What am I doing wrong?
Find more posts tagged with
Comments
mwilliams
Hi ReinerK,
Try "#" + hex. If that doesn't work, make the hex integer into a string with hex = hex.toString() and try the same. Let me know.
ReinerK
Thanks, mwilliams.
"#" + hex would work if I get a String back from the first line of code. I have played around and found that this works:
hex = "ff00ff";
this.getStyle().backgroundColor = "#" + hex;
The problem is the use of class Integer. And the result type of getColumnValue is Object. So I might have to cast?
Now my first line looks like this and is still not working:
tmpInt = Integer.parseInt((String) this.getRowData().getColumnValue("colorcode"));
hex = Integer.toHexString(tmpInt);
(I have also tried in normal Java in a Spring DAO method:
Integer.toHexString(resultSet.getInt("colorcode")) works fine. The problem is: how to script it?
mwilliams
ReinerK,
Can you just call the field by saying:
hex = # + row["colorcode"].toString();
Is your field colorcode of type int in BIRT? Even with the hex characters, i.e. 'f'?
ReinerK
Michael,
unfortunately all I have is a String in the DB which denotes an int in decimal notation, example values are: "13408767", "16776960", "39423". So to get an hex number for them I have to parse them and then transform into hex. I am trying to accomplish this with the Integer.parseInt() and Integer.toHexString() methods. Since the colorcode is used in another application to color the objects it is sure that the colorcode is in the value range of a 6-digit hex number.
The DB I use is a partial copy of another DB, updated daily. So I cannot just change the values in the DB into hex strings.
If I cannot get it work with the onCreate script I see 3 alternative possibilities:
- enhancing the copy job: so that it fills a new column in my DB with the hex value of colorcode
- using a scripted source with POJOs. The report is triggered from a web application: I can do the data fetching in the DB layer and expose a list of DTOs to BIRT. This way I can transform the colorcodes either when reading them from the DB (in the DAO) or by providing a special getter in the DTO (public String getColorcodeAsHex()).
- since the number of colorcodes is limited I could hack a set of rules for the colorcodes. But every time a new colorcode is introduced I would have to adapt the set of rules...
So the easiest way would be a 2 or 3 line script in BIRT...
mwilliams
ReinerK
It seems as if this code works for changing the row color depending on the "colorcode" integer in the onCreate method of the row. It also worked when I did the conversion in a computed column in the dataSet to the #hex string and changed the background color based on that column value.
importPackage (Packages.java.lang);
hex = this.getRowData().getColumnValue("colorcode");
hex = "#" + Integer.toHexString(hex);
this.getStyle().backgroundColor = hex;
ReinerK
Wonderful, Michael.
The importPackage does the trick, it works now.
I googled importPackage and found in the mozilla pages for Rhino the warning that one should not import java.lang:
"It's important to note that Java imports java.lang.* implicitly, while Rhino does not. The reason is that JavaScript has its own top-level objects Boolean, Math, Number, Object, and String that are different from the classes by those names defined in the java.lang package. Because of this conflict, it's a good idea not to use importPackage on the java.lang package"
So I do not import java.lang but qualify Integer instead:
hex = this.getRowData().getColumnValue("colorcode");
hex = "#" + java.lang.Integer.toHexString(hex);
this.getStyle().backgroundColor = hex;
mwilliams
ReinerK,
Glad to be able to help. Thanks for sharing on the java.lang import issue. Let us know if you have any other questions.