I am designing a template for an image which will have the product price (being taken from a datasource). If the product has a strikeout-price, the image displays it followed by the current price. If there is no strikeout-price, only the current price is displayed.
All works fine except the alignment of the prices when there is no strikeout-price. Both prices need to be horizontally together, and center-aligned in either case. But I am not able to center-align the current price textfield, if the strikeout-price is null/blank. When there is a strikeout-price, it is not a problem, as both textfields are aligned properly in the designer view.
Also, how can I put conditional formatting based on the availability of strikout-price? If the strikeout-price is not present, the font color of the current price should be different than when there is a strikeout-price.
EDIT: Images of both cases below
When Strikeout-price is available, this is how it should look:
When Strikeout-price is not available, the textfield with price shown should be center-aligned:
You can easy do it with help of Conditional Styles.
The test csv datasource:
name,price,discount_price
Shirt,"100,00","90,99"
Trousers,"80,00",
"Sun glasses","199,99","120,00"
The jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<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="two_textfields_one_on_other" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="cb5d2807-1ef9-4ffb-8df8-fcb781054bb0">
<style name="priceStyle" hAlign="Left">
<conditionalStyle>
<conditionExpression><![CDATA[$F{discount_price} == null || $F{discount_price} == 0.]]></conditionExpression>
<style forecolor="#3333FF" hAlign="Center" vAlign="Top"/>
</conditionalStyle>
</style>
<queryString>
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="price" class="java.lang.Double"/>
<field name="discount_price" class="java.lang.Double"/>
<detail>
<band height="40" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement uuid="93ad241e-5257-4664-b3ab-f6448f786775" x="0" y="0" width="114" height="20"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement markup="styled"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement uuid="6b53f6c6-5d48-4879-8934-d446150e1375" style="priceStyle" positionType="Float" x="0" y="20" width="114" height="20"/>
<box>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement markup="styled"/>
<textFieldExpression><![CDATA[($F{discount_price} != null && $F{discount_price} > 0.) ? "<style isStrikeThrough='true'>"+ $F{price} + " $</style> " + $F{discount_price} + " $" : $F{price} + " $"]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
In this sample I changed the forecolor of text (make it as Blue) and the text's horizontal alignment (make it as Central, the default is Left) in case when the discount price is absent (not set or equals 0.0).
The design in iReport looks like this:
And the result will be (via preview in iReport):
Note:
For better formatting I have used the markup syntax in my sample. You can also read about styling in this post