Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables in Conditional Style

I have a Title band with a Text Field containing a calculated Variable $V{avg_perc}. The Text Field has the evaluationTime set to Report, same for the Variables resetType. Now I'm trying to set the background color of this field with a Conditional Style but I keep getting an error message saying something like:

Invalid expression: !Double.isNaN($V{avg_perc}) && $V{avg_perc} >= 0.8

I'm doing exactly the same thing with the same Conditional Style in the Column Footer and it works without any problems, even if I set the evaluationTime for this field to Report too.

After removing !Double.isNaN($V{avg_perc}) I don't get an error anymore but the expression still doesn't work. My field stays red which is the basic color when none of the conditions is valid, no matter which value $V{avg_perc} has. It still works in the Column Footer, though. This is my style:

<style name="avg_color" mode="Opaque" backcolor="#FF0000" pdfFontName="Helvetica-Bold">
    <conditionalStyle>
        <conditionExpression><![CDATA[$V{avg_perc} >= 0.8]]></conditionExpression>
        <style backcolor="#008000"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[$V{avg_perc} >= 0.6 && $V{avg_perc} < 0.8]]></conditionExpression>
        <style backcolor="#FFCC00"/>
    </conditionalStyle>
</style>

Used Fields and Variables for this:

<field name="perc" class="java.lang.Double"/>
<variable name="avg_perc" class="java.lang.Double" calculation="Average">
    <variableExpression><![CDATA[$F{perc}]]></variableExpression>
</variable>

Any idea how to get this thing to work? I'm using JasperReports and iReport in version 3.7.4.

like image 543
YingYang Avatar asked Sep 04 '12 18:09

YingYang


2 Answers

I finally found the solution for my problem. Adding

<property name="net.sf.jasperreports.style.evaluation.time.enabled" value="true"/>

at the report level causes a Conditional Style to be performed at the moment at which the element is evaluated. See this answer in the Jaspersoft Community for more information.

like image 76
YingYang Avatar answered Nov 22 '22 14:11

YingYang


in your view ireport designer.

click on the field and in the properties panel.: markup = styled selected

Right click on the fied. edit expression:

($F{fila1}.equals("c") ? "<style forecolor='red'>"+ $F{fila1}+"</style>" : $F{fila1})

or xml

<textFieldExpression><![CDATA[($F{fila1}.equals("c") ? "<style forecolor='red'>"+ $F{fila1}+"</style>" : $F{fila1})]]></textFieldExpression>
like image 29
Edy Aguirre Avatar answered Nov 22 '22 14:11

Edy Aguirre