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.
– Elyel Rubens da Rosa
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.
– Calebe Machado