How to perform subtraction between Text Field and Variable - jasper-reports

I am very new with ireport-5.6.0.
I have one text field(Sanctioned_intake) and one i declare variable(ROPORT_COUNT).Sanctioned_intake is nothing but total number of Student per Department and REPORT_COUNT is return total no of rows which is enter into the report
For Example:
Sanctioned Intake:140 (Total Student per Department)
Actual Admitted: 10 (Actual Admission Taking)
So Difference Should Be:
Vacancy : 130(Remaining Vacancy )
now my question is how to make difference between this two
I declare One Variable
and set the following properties
variable class=java.math.BigDecimal
Calculation Sum
Reset Type Report
Variable Expression: $F{sanctioned_intake}.substract($V{REPORT_COUNT})
But i got this error
Error filling print... Error evaluating expression :      Source text : $F{sanctioned_intake}.substract$V{REPORT_COUNT}
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      
Source text : $F{sanctioned_intake}.substract$V{REPORT_COUNT} 
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:327) 
Caused by: groovy.lang.MissingMethodException: 
No signature of method: java.lang.String.substract() is applicable for argument types: (java.lang.Integer) values: [1]
Possible solutions: substring(int), substring(int, int) 

Data Type of $V{REPORT_COUNT} is integer.
Check data types of each field and variable.
Or if datatype of $F{sanctioned_intake} == BigDecimal then change variable expression to below
$F{sanctioned_intake}.substract(new BigDecimal($V{REPORT_COUNT}))

The error is related to that $F{sanctioned_intake} is declared as java.lang.String.
<field name="sanctioned_intake" class="java.lang.String"/>
However I do not think you will achieve your excepted result changing it to java.math.BigDecimal since jasper report has something called EvalutationTime, the $V{REPORT_COUNT} will start with 0 and reach the total report count only when all records have been displayed.
To the display the difference of 2 fields, declare them as Integer (if you can not, you need to parse them before calculating difference) and then use a text field.
example consider that $F's are java.lang.Integer
<textField>
<reportElement x="163" y="16" width="100" height="20" uuid="4196bd23-306b-44f6-8e3e-4d637facacf6"/>
<textFieldExpression><![CDATA[$F{sanctioned_intake}.intValue()-$F{actual_admitted.intValue()}]]></textFieldExpression>
</textField>
If one needs to be calculated use a variable, calculating (count) and resetting it as needs and then set correct evaluation time on your textField (so that the variables have time to be calculated)
example
<textField evaluationTime="Report">
<reportElement x="163" y="16" width="100" height="20" uuid="4196bd23-306b-44f6-8e3e-4d637facacf6"/>
<textFieldExpression><![CDATA[$F{sanctioned_intake}.intValue()-$V{actual_admitted}.intValue()]]></textFieldExpression>
</textField>
Note: you are using language="groovy" maybe you like to use language="java" on your jasperReport tag.

Related

jasper report table show previous page total

I am using Jaspersoft® Studio 6.8.0.
I want to have a table which can show previous page total, take following table for example, assume this
table has 3 pages:
grade
10
20
sub total 30
page 1
grade
previous page total 30
40
50
sub total 90
page 2
grade
previous page total 90
60
70
sub total 130
page 3
that is show previous page total from 2nd page to last page.
How to achieve that? thanks!
One way to do it is to create a variable that holds a mutable value set as a side effect by an expression of the text that displays the total on the previous page.
E.g. something like this:
<variable name="TotalVariable" class="java.lang.Integer" calculation="Sum" resetType="Page">
<variableExpression>$F{Column}</variableExpression>
</variable>
<variable name="PreviousTotalHolder" class="java.util.concurrent.atomic.AtomicInteger" calculation="System">
<initialValueExpression>new java.util.concurrent.atomic.AtomicInteger()</initialValueExpression>
</variable>
...display the previous total in the new page
<textField>
<reportElement .../>
<textFieldExpression>"Previous total " + $V{PreviousTotalHolder}.get()</textFieldExpression>
</textField>
...use a dummy property in the page footer to set the total into the holder object
<textField>
<reportElement x="0" y="0" width="90" height="15" uuid="a1ab288e-ae4b-4f12-83a5-e30486bb30d5">
<propertyExpression name="foo">Integer.toString($V{PreviousTotalHolder}.getAndSet($V{TotalVariable}))</propertyExpression>
</reportElement>
<textFieldExpression>"Total " + $V{TotalVariable}</textFieldExpression>
</textField>
There might be other ways to do it depending on the exact design of you report (for instance depending on whether you have a table component or a tabular band report).

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.

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>

Null condition checking in report for float

I am using iReport designer for designing report. Here data type of column data is real. In report corresponding data type is Float.
If the float value is empty or null i need to display that as a blank value.
The textField's attribute expression isBlankWhenNull="true" is not working for Float
Here is my code
<textField isBlankWhenNull="true">
<reportElement x="342" y="314" width="78" height="18"/>
<textElement/>
<textFieldExpression class="java.lang.Float"><![CDATA[$F{ws1}]]></textFieldExpression>
</textField>
I'm using iReport 3.7.6.
The result of my report is:
The cells with 0.0 should be blank.

How to display date in HH:mm:ss format in JasperReports?

I am using following code to generate chart in JasperReports.
<xyLineChart>
<chart evaluationTime="Band">
<reportElement x="0" y="0" width="555" height="500"/>
</chart>
<xyDataset>
<dataset incrementType="None"/>
<xySeries>
<seriesExpression><![CDATA["CpuUsageGraph"]]></seriesExpression>
<xValueExpression><![CDATA[new Long($F{time}.getTime())]]></xValueExpression>
<yValueExpression><![CDATA[$F{cpuUsage}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
</linePlot>
</xyLineChart>
I am printing the date on the X-axis, but it is displaying in milliseconds.
How do I display it in hh:mm:ss format?
You can use following code in Java:
new SimpleDateFormat("MM-dd-yyyy HH:mm:ss z").format($V{VAR_DATE})
where $V{VAR_DATE} is the date variable to be converted into the format.
Or you could just put the date variable in a text field and then, go the the properties view, and write this in the Pattern field: HH:mm:ss. It could be useful to also check the "Blank when null" checkbox
Simply, You can add in pattern property of the text field of date.
the field should be in java.sql.Date format
You can write in pattern HH:mm:ss
The property you are looking for is the "time axis tick label mask". There is no "pattern" field for a time series chart.
<xyLineChart>
<chart evaluationTime="Band">
<reportElement x="0" y="0" width="555" height="500"/>
</chart>
<xyDataset>
<dataset incrementType="None"/>
<xySeries>
<seriesExpression><![CDATA["CpuUsageGraph"]]></seriesExpression>
<xValueExpression><![CDATA[new Long($F{time}.getTime())]]></xValueExpression>
<yValueExpression><![CDATA[$F{cpuUsage}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
</linePlot>
</xyLineChart>
public static final String DATE_TIME_FORMAT ="yyyy-dd-MM'T'HH:mm:ss.SSS";
DateFormat df = new
SimpleDateFormat(DATE_TIME_FORMAT);
df.format($V{VAR_DATE})
Specify the "Time Period" as "minute" under the 'details' section of the chart details.
timePeriod="Minute"
From JasperReports Ultimate Guide: Time Period Expression
This expression returns a java.util.Date value from which the engine
will extract the corresponding time period depending on the value set
for the timePeriod attribute mentioned earlier in the Time Series
dataset. For instance, if the chart is about yearly data, the engine
will extract only the year from the date value, or if you are
gathering monthly data, the engine will use only the month value from
the date object returned by this expression.