changing backgroung colour base on a field value

I want the background colour of the textfield to change based on a value ..For which i have done the following by defining a style with different conditional styles one for each colour   and using the style for the rectangle element which behind the textfield.Still the backgrounf colour does not change ..Where do i go wrong


 

 

Code:
<style name="bewertungColour" isDefault="false">
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{ampel}.equalsIgnoreCase("red")]]></conditionExpression>
            <style isDefault="false" style="bewertungColour" backcolor="#FF0000"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{ampel}.equalsIgnoreCase("green")]]></conditionExpression>
            <style isDefault="false" style="bewertungColour" backcolor="#00FF00"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{ampel}.equalsIgnoreCase("yellow")]]></conditionExpression>
            <style isDefault="false" style="bewertungColour" backcolor="#FFFF00"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{ampel}.equalsIgnoreCase("grey")]]></conditionExpression>
            <style isDefault="false" style="bewertungColour" backcolor="#CCCCCC"/>
        </conditionalStyle>
    </style>
 
<band height="20" splitType="Stretch">
            <frame>
                <reportElement x="0" y="0" width="710" height="20"/>
                <rectangle>
                    <reportElement style="bewertungColour" mode="Opaque" x="0" y="0" width="81" height="20"/>
                    <graphicElement>
                        <pen lineWidth="0.25"/>
                    </graphicElement>
                </rectangle>
 
               <textField>
                    <reportElement x="0" y="0" width="81" height="20"/>
                    <box>
                        <pen lineWidth="0.25"/>
                        <topPen lineWidth="0.25"/>
                        <leftPen lineWidth="0.25"/>
                        <bottomPen lineWidth="0.25"/>
                        <rightPen lineWidth="0.25"/>
                    </box>
                    <textElement>
                        <font fontName="Arial" size="8"/>
                    </textElement>
                    <textFieldExpression class="java.lang.String"><![CDATA[$F{ampel}]]></textFieldExpression>
                </textField>
 
</frame>
</band>
</td></tr></tbody></table>
hepzibahr's picture
Joined: Apr 30 2010 - 2:35am
Last seen: 13 years 4 months ago

14 Answers:

I don't know if this will help but we have done is created a new Boolean object with in the conditional expression. Check out the code below.

 

Calvin

Code:
<conditionalStyle>
			<conditionExpression><![CDATA[new Boolean($F{ResponseMeasure}.equalsIgnoreCase( "100" ))]]></conditionExpression>
			<style isDefault="false" style="TopBoxBold" forecolor="#666666" fontName="Verdana" isBold="true"/>
		</conditionalStyle></td></tr></tbody></table>
chill_work's picture
Joined: Jan 2 2007 - 2:43am
Last seen: 16 years 8 months ago

 One other thing, if you force a color on the rectangle without using a style, does it work?

 

Calvin

chill_work's picture
Joined: Jan 2 2007 - 2:43am
Last seen: 16 years 8 months ago

Hi,

 

As you've seen already, using conditional styles is not perfect as you need to have a predefined set of possible colors to work with at design time. If the colors are completely dynamic and can even come from the database, then this approach does not work.

 

Another solution would be to change the properties of the text using markup or styled text feature, as shown in the /demo/samples/markup and /demo/samples/styledtext samples of the JR project distribution package. However, this will not work for the background property of the text element. Just for the back color of text characters.

 

So in order to have a dynamic color for the background of an element, I suggest using a scriptlet to alter this color at runtime.

Take the code below and put it in the Scriptlet.java class of the /demo/samples/scriptlet sample in the JR project. This will change the colors of elements in the title section. Note that you could locate only certain text fields that you want to alter using their user defined unique key property (lines commented out):

 

    /**
     *
     */
    public void beforeReportInit() throws JRScriptletException
    {
        System.out.println("call beforeReportInit");
        JasperReport report = (JasperReport)getParameterValue(JRParameter.JASPER_REPORT);
        JRElement[] elements = report.getTitle().getElements();
        for(int i = 0; i < elements.length; i++)
        {
            JRElement element = elements[i];
//            if ("MyKey".equals(elements[i].getKey()))
//            {
                element.setBackcolor(Color.green);
                element.setForecolor(Color.blue);
                element.setMode(ModeEnum.OPAQUE);
//                break;
//            }
        }
    }

I hope this helps.

Teodor

teodord's picture
53198
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 2 hours ago

Weren't the REPORT_TEMPLATES parameter designed for this purpose? I was attempting to work with this, but while evaluating, the wrong expression id was being evaluated. Will use this approach now.

lisadesouza89 - 9 years 4 months ago

 We have tried Teodor's scriptlet approach on 3.7.2 and it doesn't work

we have put a backcolor setting on afterDetailEval and it works only for the first color you set

 

 

public void beforeDetailEval() throws JRScriptletException {

    JasperReport report = (JasperReport)getParameterValue(JRParameter.JASPER_REPORT);

    for(JRElement element :report.getTitle().getElements()) {

_lastDetailColor  =  Color.green == _lastDetailColor ? Color.blue : Color.green;

        element.setBackcolor(_lastDetailColor );

    }

}

 

 

 

 

 



Post Edited by damiankober at 09/28/2010 16:43
damiankober's picture
Joined: Feb 27 2007 - 4:16am
Last seen: 8 years 3 months ago

 for those with this need, what we did instead is to use conditional styles whenever there was a known set of colors and styled text when colors come from an unknown source

damiankober's picture
Joined: Feb 27 2007 - 4:16am
Last seen: 8 years 3 months ago

Hi,

 

My approach with the scriptlet does not work for changing the color repeatedly during report filling (with every detail like you tried).

In my case, the color was changed only once, before anything was rendered and it is equvalent to altering the report template before filling. Once the color changed, it will apply to all instances of that element in the generated document.

 

Changing the color repeatedly during filling has no effect, or has unpredictable effects because the color value is kept only once for all instances of the element.

 

I hope this helps.
Teodor

 

teodord's picture
53198
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 2 hours ago

It helps, but it doesn't solve my problem of having to achieve conditional coloring with dynamic colors! :'(

lisadesouza89 - 9 years 4 months ago

 Thank you Teodor

Could we put a request for some "color expression" on the textfield so we can use a different color according to some expression on each row? Would it be considered?

I bet there is a lot of people that want to color cells

damiankober's picture
Joined: Feb 27 2007 - 4:16am
Last seen: 8 years 3 months ago

Just need to clarify the solution and some doubts for each .So the options for changing the background color of the text element in details band according to the content displayed at runtime can be achieved in the following ways. I have some doubts in these cases. Pls clarify these......

1) I have tried using the conditional style.. But according to the content of each column, I need to write differenet conditional styles. So if there are 10 columns in detail band, we need to write 10 different style for each. I have 10 static colors to be set accordign to data in each cell. I have >1000 columns. So Do I need to write 1000 conditional styles to apply in 1000 columns ? Will it create any issue in jrxml?

2) Setting different textboxes on top of each column? The column is more than 1000 for me ..and the color is fixed as 10. But keeping 10 textboxes on each column!!! The report works but takes about 5 minutes to get exported!!! 

Plssss tell me if  there is any other solution to achieve this ...? As Teodar mentioned , any scriplet can help in this case???????

Thanks

anjanas



Post Edited by anjanas at 10/14/2010 03:49
anjanas's picture
121
Joined: Oct 8 2007 - 2:57pm
Last seen: 15 years 11 months ago
I second this request for a 'color expression'. Otherwise a simple report can become quite complex if an element's forecolor and backcolour needs to change according to data.
bwalker's picture
74
Joined: Oct 7 2010 - 10:02pm
Last seen: 12 years 11 months ago

yes, you eventually will encounter with

1. Too many constants, the constant pool for DynamicReport_1294328174158_991822 would exceed 65536 entries
public class DynamicReport_1294328174158_991822 extends JREvaluator
<-------------------------------->
1 errors

net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:191)
net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215)

Just happened to me with a huge report. (about 400 cols and 40 conditional styles per column).


andifukuchi's picture
Joined: Sep 18 2007 - 9:54am
Last seen: 10 years 10 months ago

A color expression would be most helpful. I would like to vote in favour of adding this feature.

Currently we need to change the background color of cells (or textfield in that cell) in a column of a table depending of the value of each textfield. This is required for "heatmap" coloring on that column.

Is there any way of currently doing something like this? Or is it currently not possible in jasperreports?

shelferns's picture
Joined: Sep 23 2010 - 11:52pm
Last seen: 13 years 1 day ago

I have a same requirement for changing the color of textfield or a rectangle on the basis of the value of the field. The backcolor of textfield should allow the field name as a value and pick color from there. The field can be of java.awt.Color type so that its sure to have corret value.

Adding this feature would be very helpful.

monikasood's picture
Joined: Jul 15 2012 - 5:00pm
Last seen: 10 years 6 months ago

 I have done something similar, but using a box near the textfield and did a print when expression: MyTable.Field = 0 

 

hope this helps someone
 

kind regards

Curbish

curbish's picture
152
Joined: Oct 25 2011 - 8:30am
Last seen: 7 years 11 months ago

Hello

I have a similar requirement. But instead of textbox I need to color a crosstab. The measure cells should have a different color depending on the data.

Have someone an idea how to do this?

Thanks

Kind regards

Ka Lai's picture
58
Joined: Sep 5 2014 - 6:51am
Last seen: 8 years 11 months ago
Feedback
randomness