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

  • abresci
    • Features: Reports Version: v8 Product: Jaspersoft® Studio

    Conditional Formatting

    [toc on_off::hide=1]

    If you wish to apply different formats to your text fields based on different conditions (for example, a variable exceeding a certain value), you can do so using "Conditional Styles".

    Conditional Styles can be added to your JasperReport via Jaspersoft Studio or iReport when adding or modifying styles (or directly within your report). Please refer to the section in the JasperReports Ultimate Guide entitled "Conditional Styles". Once you have created a Style, right click on that Style and select "Add Conditional Style". In the properties dialog for your conditional style, you can add a condition expression (Java or Groovy), and set all your style preferences. These will be applied when the condition is met, in the report elements associated with your Style (which contains the Conditional Style).

    Note that a single Style can contain numerous conditions.

    To associate text fields or other report elements with your Style, select your element (in Jaspersoft Studio or iReport), and in the element's Properties, set the "Style" property to the name of the Style you created. When you run the report, the conditions contained within that style will be analyzed.

    A Note About Evaluation Times for Conditional Styles

    If the evaluation of the condition expression of a Style needs to be delayed (or match the evaluation time of the report elements using the Style), you must set the Report's configuration property

    net.sf.jasperreports.style.evaluation.time.enabled

    to true.

    A Note About Order of Conditional Styles

    JasperReports Library and JasperReports Server will use the FIRST condition that matches the contained value (first from top to bottom) from the list of conditional styles .

    For example, if I apply the style below (named "CustSatIndexMarkup")  to a text field, a value of 6 will be displayed in red, because it is < 8. The value 6 will not be matched a second time to the third condition, even though it is < 10, because it already matched the previous condition.

    And, for any elements using this style, any value less than 10 and >= 8 will be displayed in yellow.

    Essentially, your Style's conditional style list reads like a series of "else if" conditions from top to bottom.

    If none of the listed conditions are met, this Style will default to the style properties listed in the "<style>" tag (in this case, the forecolor is black).

    <style name="CustSatIndexMarkup"

              forecolor="black"

              pattern="###0.00;-###0.00"

              isBlankWhenNull="false"

              fontSize="10"

              isBold="false"

              isItalic="false"

              isUnderline="false"

              isStrikeThrough="false">

        <conditionalStyle>

            <conditionExpression>

                <![CDATA[new Boolean($V{CustSatIndex_Individual} == 10)]]>

            </conditionExpression>

            <style forecolor="green" isBold="true"/>

        </conditionalStyle>

        <conditionalStyle>

            <conditionExpression>

                <![CDATA[new Boolean($V{CustSatIndex_Individual} < 8)]]>

            </conditionExpression>

            <style forecolor="red" isBold="true"/>

        </conditionalStyle>

        <conditionalStyle>

            <conditionExpression>

                <![CDATA[new Boolean($V{CustSatIndex_Individual} < 10)]]>

            </conditionExpression>

            <style forecolor="yellow" isBold="true"/>

        </conditionalStyle>

    </style>

     


    User Feedback

    Recommended Comments

    I just want to leave this hear to hopefully help other newbies like me. If your conditional expression compares a string you cannot use the == syntax as in the example above. Instead you must use .equals( "string" ) or .equalsIgnoreCase( "string" ).

    For example, I have a field called "PassQC" so my conditional expression in my conditional style was $F{PassQC}.equals( "Yes" ). 

    Regards,

    -Brendan

    Link to comment
    Share on other sites

    Brendan,

    Just wanted to say thanks for posting this comment. I would never have known otherwise.

    I will also add for other newbies that if you're doing conditional formatting involving the background color, you have to uncheck the "transparent" checkbox on the field to get it to render properly.

    Link to comment
    Share on other sites



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