How to create style condition in Jaspersoft Studio?

Asked

Viewed 2,032 times

2

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

I pass the following to the datasource Hashmap:

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 I put in the 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 showing the positive or negative value with the given colour:

<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 in the service if the value is positive or negative and pass an additional parameter in the hashmap.

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

  • Have you applied style to any field? how is the result getting? always green, always red or none.

  • The report is generated with the default color, no verification happens. I believe the startsWith() method is not being read. I’ll try to check with the parole you showed in the post below.

1 answer

2


1. Create a new style

Criando Estilo

2. Select the new style created, and change its name as shown below

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

3. Right-click on the new style created and then click on create new conditional style as follows in the images...

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

4. Right-click on the new matching style created and then click on show properties

5. Select the "Style" tab in conditional expression.

inserir a descrição da imagem aqui

6. In the expression you can put the condition you want, in the image below is illustrated your need.

inserir a descrição da imagem aqui

7. Change to the Appearance tab and select the desired color to "Foreground".

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

inserir a descrição da imagem aqui

9. Apply the style created in the desired field as shown below.

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

10. Execute the report and you will get the expected result.

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • I couldn’t make it work yet, super weird, there must be something I can’t see.

  • Your parameter is of type java.math.Bigdecimal? maybe you forgot to change, by default it is String..

  • I was able to discover the problem, the condition was functional the detail is that Jasper for some reason when generating the input below was overwritten with the standard black color.

  • Thanks for the feedback, if you know the reason for the problem, it would be nice to share how you solved, editing the question or creating an answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.