How to create style condition in Jaspersoft Studio?

2

How can I put a style condition in Jasper Studio by checking if the value is positive put green in the foreground, if it is 0 or negative put red in the foreground?

I pass the following to the HashMap datasource:

JRDataSource jrDataMovimentacoes = new JRBeanCollectionDataSource(movimentacoes);
    BigDecimal jrSumLancamentos = lancamentoDao.getLancamentoSum(parameters);

    if(MoneyUtil.getStringMoneyValueWithComma(jrSumLancamentos).startsWith("-")){
        params.put("movSumValidator", "NEGATIVE");
    }else {
        params.put("movSumValidator", "POSITIVE");
    }

    params.put("movimentacoesSum", MoneyUtil.getStringMoneyValueWithComma(jrSumLancamentos));

Condition that I put in style:

<style name="MovSumValidator">
    <conditionalStyle>
        <conditionExpression><![CDATA[$P{movSumValidator}.equals("NEGATIVE")]]></conditionExpression>
        <style mode="Opaque" forecolor="#C70704" backcolor="#FFFFFF">
            <pen lineStyle="Solid" lineColor="#030302"/>
            <box>
                <topPen lineWidth="0.1"/>
                <leftPen lineWidth="0.1"/>
                <bottomPen lineWidth="0.1"/>
                <rightPen lineWidth="0.1"/>
            </box>
        </style>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[$P{movSumValidator}.equals("POSITIVE")]]></conditionExpression>
        <style mode="Opaque" forecolor="#1AB305" backcolor="#FFFFFF">
            <box>
                <topPen lineWidth="0.1" lineColor="#000000"/>
                <leftPen lineWidth="0.1" lineColor="#000000"/>
                <bottomPen lineWidth="0.1" lineColor="#000000"/>
                <rightPen lineWidth="0.1" lineColor="#000000"/>
            </box>
        </style>
    </conditionalStyle>
</style>

Field that should appear positive or negative with the given color:

<textField pattern="">
            <reportElement key="" style="MovSumValidator" positionType="Float" mode="Transparent" x="638" y="190" width="164" height="15" forecolor="#000000" uuid="1eb194f9-c922-45ea-8f6a-1d3b4fd7a425">
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
            </reportElement>
            <box>
                <topPen lineWidth="0.1" lineColor="#000000"/>
                <leftPen lineWidth="0.1" lineColor="#000000"/>
                <bottomPen lineWidth="0.1" lineColor="#000000"/>
                <rightPen lineWidth="0.0" lineColor="#000000"/>
            </box>
            <textElement textAlignment="Right" verticalAlignment="Middle">
                <font size="8"/>
                <paragraph rightIndent="2"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{movimentacoesSum}]]></textFieldExpression>
        </textField>

In summary, I check the service if the value is positive or negative and I pass an additional parameter in hashmap.

Even though nothing happens, the report is printed without validating what I did.

    
asked by anonymous 05.05.2016 / 23:32

1 answer

1

1. Create a new style

2.Selectthenewstyleyoucreated,andchangeyournameasshownbelow

3.Rightclickonthenewstylecreatedandthenclickoncreatenewconditionalstyleasfollowsintheimages...

4.Rightclickonthenewconditionalstylecreatedandthenclickshowproperties

5.Selectthe"Style" tab in conditional expression.

6.Intheexpressionyoucanputtheconditionyouwant,intheimagebelowisillustratedyourneed.

7.ChangetotheAppearancetabandselectthedesiredcolorfor"Foreground."

8. Repeat the process to create the condition for the other color.

9.Applythestylecreatedinthedesiredfieldasimagesbelow.

10.Runthereportanditwillhavetheexpectedresult.

    
06.05.2016 / 01:29