Monday, 22 August 2016

Tip : Conditional Image display in Jasper Reports 6.2 or later

Hi,

In this post you will see how to write conditional expression to compare two fields and based on return value display images.
Lets assume you have number1 and number2 fields of BigDecimal types. Write below expression in image path expression editor itself.



($F{NUMBER1}.intValue()!=0 && $F{NUMBER2}.intValue()!=0) &&
($F{NUMBER1}.intValue()==$F{NUMBER2}.intValue() )

? "C:/Users/sadakar.p/Desktop/image.jpg": "C:/Users/sadakar.p/Desktop/image2.jpg"


True case : 
SELECT 1234.45 as number1, 1234.45 as number2 FROM dual
False case :
SELECT 1234.45 as number1, 567.32 as number2 FROM dual

NOTE : 
1) Do not give empty quotes for the false section(true section) any where in the expression. If you give, you may stop with HTML 500 error message.
2) Make sure the image paths are given with "repo:<serverpath of the image>"  when report is published.
3) This example is developed using 6.2 Pro studio and oracle queries are used for demonstration.

JRXML Code : 
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-08-22T11:43:19 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="aed847f7-ff38-4b72-9673-2ffa54e62469">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="ppmguru"/>
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<queryString>
<![CDATA[SELECT 1234.45 as number1, 1234.45 as number2 FROM dual]]>
</queryString>
<field name="NUMBER1" class="java.math.BigDecimal"/>
<field name="NUMBER2" class="java.math.BigDecimal"/>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="138" splitType="Stretch">
<image>
<reportElement x="80" y="0" width="235" height="138" uuid="fd567e43-fc8b-45fb-bbee-515dd7cb7ff5"/>
<imageExpression><![CDATA[($F{NUMBER1}.intValue()!=0 && $F{NUMBER2}.intValue()!=0) &&
($F{NUMBER1}.intValue()==$F{NUMBER2}.intValue() )

? "C:/Users/sadakar.p/Desktop/image.jpg": "C:/Users/sadakar.p/Desktop/image2.jpg"]]></imageExpression>
</image>
</band>
</summary>

</jasperReport>


Cheers.!
Sadakar Pochampalli 

No comments:

Post a Comment