Jump to content
We've recently updated our Privacy Statement, available here ×

changing backgroung colour base on a field value


hepzibahr

Recommended Posts

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>
Link to comment
Share on other sites

  • Replies 16
  • Created
  • Last Reply

Top Posters In This Topic

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>
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

  • 4 months later...

 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
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 3 months later...

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).


Link to comment
Share on other sites

  • 3 months later...

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?

Link to comment
Share on other sites

  • 1 year later...

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.

Link to comment
Share on other sites

  • 4 weeks later...
  • 1 year later...
  • 4 months later...

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...