How to make dynamic font property based on condition for textField in jasper report? [duplicate] - jasper-reports

This question already has answers here:
Set background color in textfield from condition using iReport
(3 answers)
How to create property with condition / dynamic foreground color?
(2 answers)
How to change font name dynamically?
(2 answers)
JasperReports: Change font size by Param value
(3 answers)
Style a text field in JasperReports
(1 answer)
Closed 3 months ago.
I would like to make a dynamic change font property based on variable condition.
I have already searched on the stackoverflow's questions but I didn't find anything for this detail.
Below my snippet code:
<textField>
<reportElement style="DetailsPrice" x="370" y="5" width="56" height="15" uuid="2f0dc291-6a8b-48d3-b7d1-fe931b88e859">
<property name="com.jaspersoft.studio.unit.y" value="px"/>
<property name="com.jaspersoft.studio.unit.x" value="mm"/>
<property name="com.jaspersoft.studio.unit.height" value="mm"/>
<printWhenExpression>
<![CDATA[$F{product_unformatted_discount_amount} != null && $F{product_unformatted_discount_amount} != 0]]>
</printWhenExpression>
</reportElement>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true" isStrikeThrough="true"/>
</textElement>
<textFieldExpression><![CDATA["+" + $F{product_price_formatted_amount}]]></textFieldExpression>
</textField>
How can I change isStrikeThrough property value depending on $F{product_unformatted_discount_amount} value ?
In a nutshell I would like to apply condition in line in the font tag.
Thanks

I resolved via markup tag with styled value.
Markup documentation
code snippet:
<textElement textAlignment="Left" verticalAlignment="Middle" markup="styled"/>
<textFieldExpression>
<![CDATA[$F{product_unformatted_discount_amount} != null && $F{product_unformatted_discount_amount} != 0 ? "<style isStrikeThrough='true' isBold='true'> +" + $F{product_price_formatted_amount} + "</style>" : "<style isStrikeThrough='false' isBold='true'> +" + $F{product_price_formatted_amount} + "</style>"]]>
</textFieldExpression>
I put the markup tag with styled value into textElement tag, while isStrikeThrough tag in the textFieldExpression tag implementing if inline
markup=styled This markup is capable to format the text using a set of
HTML-like tags and it is pretty popular in the Java environments. It
allows to set a specific font for chunks of text, color, background,
style and so on. It's often good enough to format the text
programmatically.

Related

Jasper report XLS export shows value as String instead of Currency

I have a similar issue as described here but with the value type instead of value pattern. I have a value called 'Balance'
<parameter name="Balance" class="java.lang.String"/>
and corresponding field
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="400" y="55" width="117" height="7"/>
<textElement textAlignment="Right" verticalAlignment="Top">
<font size="7" isBold="true" pdfFontName="Helvetica" isPdfEmbedded="false"/>
</textElement>
<textFieldExpression><![CDATA[( $P{Balance}.equals(null) ? "" : $P{Balance} )]]></textFieldExpression>
</textField>
When I export my Jasper report as XLS, 'Balance' value comes in correct pattern (ie. $400.20) but Excel consider this as a String instead of Currency.
Any idea how to fix it would be much appreciated.

Add group of text fields but show only one group field which has data in Jasper

I have two groups text fields.
1st group (Stock Code) is :
$F{VW_STOCKOPNAME_S1_KL}
$F{VW_STOCKOPNAME_S2_JB}
$F{VW_STOCKOPNAME_S3_NB}
$F{VW_STOCKOPNAME_S4_SB}
2nd group (Asset Code) is :
$F{VW_STOCKOPNAME_A1_GOLONGAN}
$F{VW_STOCKOPNAME_A2_BIDANG}
$F{VW_STOCKOPNAME_A3_KELOMPOK}
$F{VW_STOCKOPNAME_A4_SUBKELOMPOK}
$F{VW_STOCKOPNAME_A5_SUBSUBKEL}
and I have field called Product :
$F{VW_STOCKOPNAME_PRODUCT}
Each product has either Asset Code / Stock Code.
If a product has Asset Code, then Stock Code field is automatically blank,
it works the other way around.
Code || Product
----------------------
1-2-1-1 || Stock Goods
0-2-3-1 || Asset Goods
How can I make it automatically show each product with matching code?
for all the textfields in group add property "Blank When Null"(isBlankWhenNull="true")
update: Added PrintWhenExpression
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="143" y="29" width="260" height="20" isRemoveLineWhenBlank="true">
<printWhenExpression><![CDATA[($F{VW_STOCKOPNAME_S1_KL}!=null && $F{VW_STOCKOPNAME_S2_JB}!=null && $F{VW_STOCKOPNAME_S3_NB}!=null && $F{VW_STOCKOPNAME_S4_SB}!=null)]]></printWhenExpression>
</reportElement>
<box leftPadding="2"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{VW_STOCKOPNAME_S1_KL}]]></textFieldExpression>
or you can also use print when expression

Print a field when certain value is not present Jasper reports

I am creating a JasperReports's report using iReport. I have a date field where once instance is 01-JAN-1900. I need any time the date field has this value then null value is printed. I have tried this:
<textField pattern="dd-MMM-yyyy" isBlankWhenNull="true">
<reportElement uuid="4dd05795-8363-4cf3-ad30-239aac3a086f" x="3" y="0" width="63" height="15">
<printWhenExpression><![CDATA[$F{TRAN_DATE} != "01-JAN-1900"]]></printWhenExpression>
</reportElement>
<textElement>
<font size="9"/>
</textElement>
<textFieldExpression><![CDATA[$F{TRAN_DATE}]]></textFieldExpression>
</textField>
But the value is still getting printed. How do I achieve this?
You cannot compare dissimilar types. Drop the String literal and compare datetime types.
If your field $F{TRAN_DATE} is in java.util.Date type, change it to java.lang.String and your condition should work.

Report's text field's "stretch with over flow" option add empty row in PDF export

I have set the "Stretch With Overflow" option True to some text fields , when the data over flow in the jasper viewer is working correctly, the text is on 2 rows but in PDF document the second row is empty: it just adds an empty row.
Exemple xml code :
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="165" y="0" width="35" height="10" isRemoveLineWhenBlank="true" uuid="a5d7ff35-a2dc-480e-80a7-9246d8618b27"/>
<textElement textAlignment="Left">
<font fontName="Futura Std Light" size="7"/>
<paragraph lineSpacing="Single"/>
</textElement>
<textFieldExpression><![CDATA[$F{montant} == null && $F{numeroAppelant} != null ? $F{numeroAppele}.toString():
$F{duree} != null ? $F{duree}.toString() :
$F{volumeKo} != null ? null : $F{montant}.toString()]]>
</textFieldExpression>
I found the solution,
I just set the line spacing property to 1.5 !
I found that when I change the font, it works wright. The problem that the same font on another report works fine with stretch with overflow option.

How to mark cells red in a table when its value is larger than a pre-defined parameter in Jasper Studio 6.0.1

I put a table in the detail band of a report. The table has four columns. What I want to realize is, set a parameter as threshold value and the parameter is dynamic, all cells in the table which contains value larger than the parameter will be marked as red. How to realize this in JasperReport Studio? My version is 6.0.1
You should use the conditional style on your text fields.
1. Create Style:
<style name="Detail" fontName="Arial" fontSize="10">
<conditionalStyle>
<!-- This is condition, when rows should become red-->
<conditionExpression><![CDATA[$F{MY_FIELD} > $F{MY_PARAM}]]></conditionExpression>
<style forecolor="#FF0000"/>
</conditionalStyle>
</style>
Use this style in your text fields:
<textField>
<!-- style="Detail"-->
<reportElement style="Detail" x="0" y="0" width="143" height="20"/>
<textFieldExpression><![CDATA[$F{MY_FIELD}]]></textFieldExpression>
</textField>