Using variables in Conditional Style - jasper-reports

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.

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.

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>

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

Related

How to avoid print footer in last page of JasperReport? [duplicate]

I need to enable the reports with 3 types of the page headers: "first page header", "middle pages header" and "last page header". All of these headers can have different number of report elements and on different positions. Same goes for the footers.
NOTE: The report must support the report elements like jr:table or jr:list in its Detail band. These element must be supplied with the data via xml datasources.
I was able to simulate first page header by using Title band, but I'm having problems enabling "middle pages header" and "last page header". This is what I'm trying to do:
<pageHeader>
<band height="100" splitType="Stretch">
<frame>
<reportElement x="0" y="0" height="100" width="555" uuid="f788c74b-6853-4bc7-8ed7-5e3d538287c9" isRemoveLineWhenBlank="true">
<printWhenExpression>
<![CDATA[new Boolean($V{PAGE_NUMBER} == $V{PAGE_COUNT})]]>
</printWhenExpression>
</reportElement>
<staticText>
<reportElement x="1" y="1" width="100" height="20" uuid="9777208b-5323-4045-aa49-a849d5c00c89"/>
<text><![CDATA[LAST PAGE HEADER TEXT 1]]></text>
</staticText>
<staticText>
<reportElement x="50" y="10" width="200" height="50" uuid="15313676-09e6-4d17-ac75-2df99f61bfee"/>
<text><![CDATA[LAST PAGE HEADER TEXT 2]]></text>
</staticText>
</frame>
<frame>
<reportElement x="0" y="0" height="100" width="555" uuid="65577abd-8717-477a-f27e-c70e9eba46af" isRemoveLineWhenBlank="true">
<printWhenExpression>
<![CDATA[new Boolean($V{PAGE_NUMBER} != 1) && new Boolean($V{PAGE_NUMBER} != $V{PAGE_COUNT})]]>
</printWhenExpression>
</reportElement>
<staticText>
<reportElement x="70" y="0" width="300" height="100" uuid="0a866eb3-85cf-4376-d6a4-21b534d36df0"/>
<text><![CDATA[MIDDLE PAGE HEADER TEXT 1]]></text>
</staticText>
</frame>
</band>
</pageHeader>
I'm using frame blocks along with printWhenExpression. Depending on the printWhenExpression condition, only one of two frame blocks should be printed (with all of its content). First frame block should render "last page header", and second one should render "middle pages header".
The problem lies within the first frame ("last page header") printWhenExpression:
<printWhenExpression>
<![CDATA[new Boolean($V{PAGE_NUMBER} == $V{PAGE_COUNT})]]>
</printWhenExpression>
In this case, $V{PAGE_NUMBER} always equals $V{PAGE_COUNT} because of the evaluation time.
Is there any way to check if the current page is the last page? Also, is there any other way to enable "middle pages" and "last page" headers?
NOTE: I was able to implement similar logic for the footers by using the frames and Last Page Footer band!
First of all: $V{PAGE_COUNT} is
the number of records that were processed when generating the current page. Hence the number of records you have in the page, not a page number count...
In pageFooter and lastPageFooter the reportElement can have negative Y coordinates (if IDE does not allow this then you need to edit manually the jrxml) es. y="-700", which allows you to put information in pageHeader and last pageHeader...
FIRST PAGE
in frame use:
<printWhenExpression><![CDATA[new Boolean($V{PAGE_NUMBER}.intValue()==1)]]></printWhenExpression>
pageHeader use title band or put frame in pageFooter and use negative y coordinate.
pageFooter, use pageFooter band
MIDDLE PAGE
on frame use:
<printWhenExpression><![CDATA[new Boolean($V{PAGE_NUMBER}.intValue()>1)]]></printWhenExpression>
pageHeader put frame in pageFooter and use negative y coordinate.
pageFooter, use pageFooter band.
LAST PAGE
pageHeader, put frame in lastPageFooter and use negative y coordinate.
pageFooter, use lastPageFooter band
NOTE: To generate space for this virtual pageHeader include an empty pageHeader with desired band height
Have Fun!

jasper report dynamic change font size

I want to know how to change font size in field expression. it's mean I have a two db fields. then now I want to merge those two fields using expression.
example :
$F{type1} + " Balance is SLR : " + $F{type2}
I write this simple code in field expression.
but now problem is i want to set 10px font size to $F{type1} field and
12px font size to $F{type2} field.
Thank You
Use styled text:
<textField>
<reportElement width="300" y="0" x="0" height="200"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA["<style size=\"10\">" + $F{type1} + "</style> Balance is SLR : <style size=\"12\">" + $F{type2} + "</style>"]]></textFieldExpression>
</textField>
See some details about the feature here.

Remove Line When Blank not working for Relative to Band height stretch type

I have a report with position type float for all elements and stretch type Relative to Band Height and no stretch for different element. For one particular case I want my textField to completely vanish depending upon Print when expression, so I have Remove Line When Blank checked. But the problem is, there is small gap between element above and below it when it is gone.
This is what I expect.
This is what I get
Following is my textField
<textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
<reportElement positionType="Float" stretchType="RelativeToBandHeight" mode="Opaque" x="0" y="617" width="278" height="10" isRemoveLineWhenBlank="true" forecolor="#000000" backcolor="#00FFFF" uuid="b8a09e5a-b3ff-4ddf-b833-3cf854f1108e">
<printWhenExpression><![CDATA[($F{SOME_VALUE}!=null)]]></printWhenExpression>
</reportElement>
<textElement textAlignment="Justified" verticalAlignment="Middle">
<font fontName="Arial" size="4" isBold="true" isItalic="false" pdfFontName="Helvetica-Bold"/>
<paragraph leftIndent="1" rightIndent="1"/>
</textElement>
<textFieldExpression><![CDATA[$F{SOME_VALUE}]]></textFieldExpression>
</textField>
If the stretch type is No stretch it works as expected and no gap between elements, but it is not working for Relative to Band Height. How do I solve this issue?
EDIT
I tried keeping elements inside the frame but result is the same (Like I have said, it works when stretch type is No Stretch, but there is gap for Relative to Band Height).
LAST EDIT
I had to keep the Middle dynamic element on it's own band to make that work.My detail band was really complicated and there were other dynamic elements which might have resulted on growth of space on the band.

How to enable different page headers on last and middle pages in jasper reports?

I need to enable the reports with 3 types of the page headers: "first page header", "middle pages header" and "last page header". All of these headers can have different number of report elements and on different positions. Same goes for the footers.
NOTE: The report must support the report elements like jr:table or jr:list in its Detail band. These element must be supplied with the data via xml datasources.
I was able to simulate first page header by using Title band, but I'm having problems enabling "middle pages header" and "last page header". This is what I'm trying to do:
<pageHeader>
<band height="100" splitType="Stretch">
<frame>
<reportElement x="0" y="0" height="100" width="555" uuid="f788c74b-6853-4bc7-8ed7-5e3d538287c9" isRemoveLineWhenBlank="true">
<printWhenExpression>
<![CDATA[new Boolean($V{PAGE_NUMBER} == $V{PAGE_COUNT})]]>
</printWhenExpression>
</reportElement>
<staticText>
<reportElement x="1" y="1" width="100" height="20" uuid="9777208b-5323-4045-aa49-a849d5c00c89"/>
<text><![CDATA[LAST PAGE HEADER TEXT 1]]></text>
</staticText>
<staticText>
<reportElement x="50" y="10" width="200" height="50" uuid="15313676-09e6-4d17-ac75-2df99f61bfee"/>
<text><![CDATA[LAST PAGE HEADER TEXT 2]]></text>
</staticText>
</frame>
<frame>
<reportElement x="0" y="0" height="100" width="555" uuid="65577abd-8717-477a-f27e-c70e9eba46af" isRemoveLineWhenBlank="true">
<printWhenExpression>
<![CDATA[new Boolean($V{PAGE_NUMBER} != 1) && new Boolean($V{PAGE_NUMBER} != $V{PAGE_COUNT})]]>
</printWhenExpression>
</reportElement>
<staticText>
<reportElement x="70" y="0" width="300" height="100" uuid="0a866eb3-85cf-4376-d6a4-21b534d36df0"/>
<text><![CDATA[MIDDLE PAGE HEADER TEXT 1]]></text>
</staticText>
</frame>
</band>
</pageHeader>
I'm using frame blocks along with printWhenExpression. Depending on the printWhenExpression condition, only one of two frame blocks should be printed (with all of its content). First frame block should render "last page header", and second one should render "middle pages header".
The problem lies within the first frame ("last page header") printWhenExpression:
<printWhenExpression>
<![CDATA[new Boolean($V{PAGE_NUMBER} == $V{PAGE_COUNT})]]>
</printWhenExpression>
In this case, $V{PAGE_NUMBER} always equals $V{PAGE_COUNT} because of the evaluation time.
Is there any way to check if the current page is the last page? Also, is there any other way to enable "middle pages" and "last page" headers?
NOTE: I was able to implement similar logic for the footers by using the frames and Last Page Footer band!
First of all: $V{PAGE_COUNT} is
the number of records that were processed when generating the current page. Hence the number of records you have in the page, not a page number count...
In pageFooter and lastPageFooter the reportElement can have negative Y coordinates (if IDE does not allow this then you need to edit manually the jrxml) es. y="-700", which allows you to put information in pageHeader and last pageHeader...
FIRST PAGE
in frame use:
<printWhenExpression><![CDATA[new Boolean($V{PAGE_NUMBER}.intValue()==1)]]></printWhenExpression>
pageHeader use title band or put frame in pageFooter and use negative y coordinate.
pageFooter, use pageFooter band
MIDDLE PAGE
on frame use:
<printWhenExpression><![CDATA[new Boolean($V{PAGE_NUMBER}.intValue()>1)]]></printWhenExpression>
pageHeader put frame in pageFooter and use negative y coordinate.
pageFooter, use pageFooter band.
LAST PAGE
pageHeader, put frame in lastPageFooter and use negative y coordinate.
pageFooter, use lastPageFooter band
NOTE: To generate space for this virtual pageHeader include an empty pageHeader with desired band height
Have Fun!

How to print EAN-13 barcodes in iReport?

I'm trying to add a barcode to my report template, its' an EAN13 barcode here's the code :
<componentElement>
<reportElement uuid="af782895-f1d3-4e1d-b200-1f2ecf18b4fa" x="67" y="17" width="478" height="81"/>
<jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="EAN13" drawText="true" checksumRequired="false" barWidth="3" barHeight="10">
<jr:codeExpression><![CDATA[142155363254]]></jr:codeExpression>
</jr:barbecue>
</componentElement>
When i generate the report i on ly get the code expression , no barcode drawed
Here's the template design :
I think that the EAN13 is not installed with IReport.
So How can i add it to IReport?.
the solution is to use the Barcode4J library instead of the Barbecue library to print the barcode.
barHeight is too low, try something like 50 or don't specify any bar height.