Using variables in Conditional Style

2019-02-17 10:54发布

问题:

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.

回答1:

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.



回答2:

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>


回答3:

Modify this code based on your requirement

<style name="alternateStyle" fontName="Arial">
    <conditionalStyle>
        <conditionExpression><![CDATA[new Boolean($V{AMOUNT}.intValue() == 0)]]></conditionExpression>
        <style mode="Opaque" backcolor="#FF0000" isBold="true"/>
    </conditionalStyle>
</style>

And also refer this link : Link