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

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>

Related

Dynamic content in text field in JRXML file

Below is the code snippet
<staticText>
<reportElement key="staticText-2" mode="Opaque" x="381" y="242" width="79" height="18" forecolor="#000000" backcolor="#FFFFFF" uuid="4ec23e25-c8d9-4941-bd81-777aff6c08a7">
<reportElement/>
<text><![CDATA[14.01.2019]]></text>
</staticText>
Each month we need to change the date in JRXML file manually <text><![CDATA[14.01.2019]]></text> as 14.11,14,12 etc.
How can I automate this feature? Like code will read the current system date, once it reaches 14th of the month , automatically the date will change in jrxml file as well?
Please help.
You could turn the staticText into a textField where you could use an expression based on JasperReports built-in date/time functions, like so:
<textField>
<reportElement key="staticText-2" mode="Opaque" x="381" y="242" width="79" height="18" forecolor="#000000" backcolor="#FFFFFF" uuid="4ec23e25-c8d9-4941-bd81-777aff6c08a7"/>
<textFieldExpression><![CDATA[DATEFORMAT(EDATE($P{AccountingDate}, 1), "14.MM.yyyy")]]></textFieldExpression>
</textField>

How to convert number stored as text : Using Jrxml /jasper

I use jrxml jasper to export my content to .xls file . I am having a filed named PollutantQuantity which is persisted as a string in DB.
I am fetching the same and giving those values to .jrxml . I can see the values getting populated correctly without any issue . Now my customer wants to do some SUM,MULTIPLY functions directly from the exported sheet .
In this case ,since is rendered as text , I can't do any manipultaions.
My Jrxml code snippet is like
<property name="net.sf.jasperreports.export.detect.cell.type" value="true"/>
<field name="pollutantQty" class="java.lang.String" />
<textField>
<reportElement x="0" y="0" width="100" height="20" isRemoveLineWhenBlank="true" />
<box leftPadding="10"><pen lineColor="#000000" /><topPen lineWidth="0.5" /><leftPen lineWidth="0.5" /><bottomPen lineWidth="0.5" /><rightPen lineWidth="0.5" />
</box>
<textFieldExpression ><![CDATA[$F{pollutantQty}]]></textFieldExpression>
</textField>
I am using the property field and also my field emissionQty is declared as string. How can I convert that so that in output excel the emissionQty is
interpreted as Number.
Define field as a Number es. java.lang.Double
<field name="pollutantQty" class="java.lang.Double" />
Use pattern in report to display it as you like
<textField pattern='###,##0.00'>
<reportElement x="0" y="0" width="100" height="20" isRemoveLineWhenBlank="true" />
<box leftPadding="10"><pen lineColor="#000000" /><topPen lineWidth="0.5" /><leftPen lineWidth="0.5" /><bottomPen lineWidth="0.5" /><rightPen lineWidth="0.5" />
</box>
<textFieldExpression ><![CDATA[$F{pollutantQty}]]></textFieldExpression>
</textField>
If you can't declare it as a Number (its a String in database), you need to convert the String to a Number es.
<textFieldExpression><![CDATA[Double.parseDouble($F{pollutantQty})]]></textFieldExpression>
In this case it can be wise to put a printWhenExpression using for example regex, to avoid errors.
<printWhenExpression><![CDATA[$F{pollutantQty}.matches("-?\\d+(\\.\\d+)?")]]></printWhenExpression>
or as if statement in textFieldExpression
As an extra note to the suggested solution - if you are already dealing with numerical fields (in my case java.math.BigDecimal) and you aren't able to use computation functions in the generated excel file (SUM) - check your JRXML file and see if it contains this property:
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
The OP has the property but it doesn't contain the xls in the string.

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.

Formatting/Pattern issue - Appending the Int with text is causing formatting error

So I am running into issues with JasperReports pattern field. I have created a cross tab and I need all $F{ScoreMeasure} to have two decimal places and appended with a "%" sign. So for example a cell for the $F{ScoreMeasure} in the crosstab should show 25.42%.
The issue I have come across is I appended the "%" sign by adding it to the text field expression:$V{ScoreMeasure}+"%". This works fine with whole numbers.
When I try add the two decimal places by adding the pattern #,##0.00 and run the report I get some fields have two decimals and others have multiple (up to 8).
If I drop the appended text (+"%") from the text field expression I get the right format. But I cant work out how to get the two?
Here's an extract of when it is working without the "%" appended:
<crosstabCell width="73" height="25" rowTotalGroup="Name">
<cellContents backcolor="#005FB3" mode="Opaque">
<box>
<pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField pattern="#,##0.00">
<reportElement style="Crosstab Data Text" x="0" y="0" width="73" height="25" forecolor="#FFFFFF" uuid="50401fd4-b9b1-4bf2-bd74-9a0f083e77ff"/>
<textElement verticalAlignment="Middle">
<font fontName="Arial"/>
</textElement>
<textFieldExpression><![CDATA[$V{ScoreMeasure}
]]></textFieldExpression>
</textField>
</cellContents>
And here it is appended with the text which is breaking the format:
<crosstabCell width="73" height="25" rowTotalGroup="Name">
<cellContents backcolor="#005FB3" mode="Opaque">
<box>
<pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField pattern="#,##0.00">
<reportElement style="Crosstab Data Text" x="0" y="0" width="73" height="25" forecolor="#FFFFFF" uuid="50401fd4-b9b1-4bf2-bd74-9a0f083e77ff"/>
<textElement verticalAlignment="Middle">
<font fontName="Arial"/>
</textElement>
<textFieldExpression><![CDATA[$V{ScoreMeasure}+"%"
]]></textFieldExpression>
</textField>
</cellContents>
Anyone come across this? Any suggestions on how to fix?
Okay so the solution was in the pattern I selected "Custom Format" I then added the pattern for two decimal places ###0.00 and tried appending it with the % sign. So my custom pattern looked like ###0.00%;-###0.00%.
But when this is run it multiplied the figure bu 100. It turns out "The presence of the percentage character in the pattern causes the value to be multiplied by 100 before being formatted."
So in order to avoid this you add quotes around the % sign. So your final pattern looks like this: ###0.00'%';-###0.00'%'
When run you will then get your two decimal place with the % sign
How about clicking on the field you want to customized.
Go to Properties
Go to Pattern then click on the Number -> click on Decimal for 2 spaces
Then Press OK.
Then go back to the Pattern again and go to Custom Format.
Then put a percentage in the text field for the positive and negative part
Then press ok.
EDIT
How about making the Field a string and then append the percent sign like:
$F{ScoreMeasure}.toString()+"%"

How to get current page number and total pages as variables on each page? [duplicate]

This question already has answers here:
Jasper Reports Show "Page X of Y" using a single text field
(8 answers)
Closed 4 years ago.
I need to generate OMR code on a report. I think about calling some service method on each page that get page number and total pages as parameters. Problem is: How to get that values (page number, total pages) as variables on each page ?
<background>
<band height="797">
<textField>
<reportElement x="30" y="684" width="100" height="97"/>
<textFieldExpression><![CDATA[OMRService.generateCode($V{totalPages},$V{pageNumber})]]></textFieldExpression>
</textField>
</band>
</background>
below you have sample code how to do it in 2 different textFields.
<textField evaluationTime="Page">
<reportElement x="0" y="0" width="520" height="15"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}+"/"]]> </textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="521" y="0" width="14" height="15"/>
<textElement textAlignment="Left"/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
When evaluationTime="Page" you will get page number, when "Report" there will be displayed total number of pages. I dont know how to merge it together in one textfield (dont even know if its possible - I spent a lot of time on it and couldnt solve this problem).
Select the text field.
Set Expression to: msg("Page {0} of {1}", $V{V_CURRENT_PAGE_NUMBER}, $V{PAGE_NUMBER})
Set Evalutation Time to: Auto