hepzibahr Posted May 4, 2010 Share Posted May 4, 2010 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 More sharing options...
chill_work Posted May 5, 2010 Share Posted May 5, 2010 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. CalvinCode:<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 More sharing options...
chill_work Posted May 5, 2010 Share Posted May 5, 2010 One other thing, if you force a color on the rectangle without using a style, does it work? Calvin Link to comment Share on other sites More sharing options...
Teodor Danciu Posted May 6, 2010 Share Posted May 6, 2010 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 More sharing options...
damiankober Posted September 28, 2010 Share Posted September 28, 2010 We have tried Teodor's scriptlet approach on 3.7.2 and it doesn't workwe 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 More sharing options...
damiankober Posted October 1, 2010 Share Posted October 1, 2010 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 Link to comment Share on other sites More sharing options...
Teodor Danciu Posted October 5, 2010 Share Posted October 5, 2010 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 More sharing options...
damiankober Posted October 5, 2010 Share Posted October 5, 2010 Thank you TeodorCould 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 Link to comment Share on other sites More sharing options...
anjanas Posted October 13, 2010 Share Posted October 13, 2010 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???????ThanksanjanasPost Edited by anjanas at 10/14/2010 03:49 Link to comment Share on other sites More sharing options...
bwalker Posted October 14, 2010 Share Posted October 14, 2010 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. Link to comment Share on other sites More sharing options...
andifukuchi Posted January 24, 2011 Share Posted January 24, 2011 yes, you eventually will encounter with 1. Too many constants, the constant pool for DynamicReport_1294328174158_991822 would exceed 65536 entriespublic class DynamicReport_1294328174158_991822 extends JREvaluator<-------------------------------->1 errorsnet.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 More sharing options...
shelferns Posted May 11, 2011 Share Posted May 11, 2011 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 More sharing options...
monikasood Posted August 3, 2012 Share Posted August 3, 2012 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 More sharing options...
curbish Posted August 30, 2012 Share Posted August 30, 2012 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 regardsCurbish Link to comment Share on other sites More sharing options...
lisadesouza89 Posted May 22, 2014 Share Posted May 22, 2014 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. Link to comment Share on other sites More sharing options...
lisadesouza89 Posted May 22, 2014 Share Posted May 22, 2014 It helps, but it doesn't solve my problem of having to achieve conditional coloring with dynamic colors! :'( Link to comment Share on other sites More sharing options...
Ka Lai Posted October 7, 2014 Share Posted October 7, 2014 HelloI 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?ThanksKind regards Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now