How to create dynamic color for text field?
Asked Answered
Y

3

5

Background: Jaspersoft studio 6.2

How to change forecolor of a text field based on an expression.

How can I apply a expression so the font color (forecolor) will be based on the value in that field? I can't find anywhere to set a expression for the forecolor property.

Yasmineyasu answered 21/4, 2016 at 11:6 Comment(4)
I don't think this is possible with JasperSoftStudio. It is not clean but you can put two text fields (for example with red and green color) and show one depending on an expression.Bohemianism
Why this hasn't been built in? I understand this is open source and free, but this is a very basic feature and all the reporting tools I used have this feature...Yasmineyasu
I changed my opinion, have a look at my answer or the one from Petter (which is even cleaner as mine)Bohemianism
Possible duplicate of Change text field data color (Foreground color) based on condition in JasperReportsBrad
B
6

Maybe there is an option:

You need to set markup to style. And then use an expression in the text field:

F{value1}=="GREEN"?$F{value1}:"<style backcolor='red'>"+$F{value1}+"</style>"

You can also add conditions and set multiple colors:

F{value1}=="GREEN"?"<style backcolor='green'>"+$F{value1}+"</style>":"<style backcolor='red'>"+$F{value1}+"</style>"

Maybe this will work for you.

Bohemianism answered 21/4, 2016 at 11:24 Comment(2)
Your actually works (second one is what I am looking for), although yours changed background instead of font color. I changed markup to html, and in the text expression enter something similar to yours (except it's <font color=''> some text </font>) and it works perfectly.Yasmineyasu
Worked like a charm. ThxSirois
S
5

Another possibility would be to use a style property expression in your textfield:

<textField ...>
  <reportElement ...>
      <propertyExpression name="net.sf.jasperreports.style.forecolor">
         <![CDATA["#00FF00"]]>
      </propertyExpression>
  </reportElement>
  ...
</textField>

This setting will override the current forecolor attribute for the textfield, and a green text will be printed out.

Here are listed all dynamic style properties available for report elements.

Seiter answered 6/7, 2016 at 14:44 Comment(2)
This is not dynamic. So the original question is not answered properly. Maybe there is a way to hava a conditional propertyExpression or something similarHungnam
@shertage: you should have mentioned that it is possible to use <![CDATA[$F{color}]]>Rojo
P
4

You use conditionalStyle, to achieve this

Example

<style name="myStyle" forecolor="#0000FF">
    <conditionalStyle>
        <conditionExpression><![CDATA[$F{myField}<0]]></conditionExpression>
        <style forecolor="#CC0000"/>
    </conditionalStyle>
</style>

Then set the style to the textField when you like to use it

<textField>
    <reportElement style="myStyle" x="448" y="5" width="100" height="20" uuid="b75e4497-e952-4051-8640-2f6b498dd152"/>
    <textFieldExpression><![CDATA["Hello world"]]></textFieldExpression>
</textField>

In JasperSoft Studio, right click on style in outline to create "Conditional Style" and set the properties in the properties tab

JasperSoft Studio

Pomerleau answered 21/4, 2016 at 11:22 Comment(6)
What are the codes for and how to use the code? Are we talking Jaspersoft studio 6.2? And don't forget the background is 'I am new'.Yasmineyasu
@Yasmineyasu The code is the jrxml code (universal for all IDE),if you click source in IDE you will see it. I have included a screen shot so you can find where the styles are also in IDE.Pomerleau
Hence when you use IDE it generates the jrxml code : ).Pomerleau
I see what you mean now, yours will work but the first one is easier imho and comes before yours, so, but thanks for the explanation.Yasmineyasu
@Yasmineyasu my is the standard way, you can change all the properties of your text from (background, foreground, font, bold ecc), comes before mine??, what do you mean??, but no problem you choose what ever you likePomerleau
I meant his/her is the first answer, yours is second so comes before yoursYasmineyasu

© 2022 - 2024 — McMap. All rights reserved.