Jasper Reports - calculation of total using CalculationEnum.System - jasper-reports

I have 3 columns (obtained from the query along with the data)
| A | B | C |
| 5 | 15 | 20 |
| 15| 25 | 40 |
The net total for these columns are to be calculated as below :-
20 40 0.5 (i.e , the total of C = total of A / total of B) . How do I get the total calculated as required in jasper reports using java .
For calculating the total of column 'C' , I have set the calculation enum constant to be calculationEnum.SYSTEM . On setting the expression to be
expression.setText("new Double($F{" +A + "/" +B +"}.doubleValue())"); , it shows that the corresponding field , new Double($F{A/B}.doubleValue()) does not exist . To eliminate this , I have added the field in the list of field columns . But then it shows that the column for the corresponding field doesnot exist .
Is what I have done until now correct ? Or is there any better way in which I can get the total to be calculated as required .

Field A and B need to be of class="java.lang.Number" or one of its sub classes as java.lang.Double
To get the sum of them the expression is:
$F{A}.doubleValue() + $F{B}.doubleValue()
To calculate the SUM($F{A}) / SUM($F{B}) you need to use two variables
<variable name="sumOfA" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{A}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<variable name="sumOfB" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{B}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
And then in the textField do the division, remember to set evaluationTime="Report" on the textField so that the variables have been calculated before displaying.
<textField evaluationTime="Report">
<reportElement x="208" y="17" width="100" height="20" uuid="414e6633-58c5-4081-a04b-fe3973f29d96"/>
<textFieldExpression><![CDATA[$V{A}.doubleValue()/$V{B}.doubleValue()]]></textFieldExpression>
</textField>

Create two variables, one for sum of A and one for sum of B.
And then add a text field with the following expression:
($V{sumOfA}/$V{sumOfB})

Related

How to Check param from iReport with database?

Currently I'm doing report using iReport 3.0.0
I have this one field which is Indicator (A, B and C).
I group by this field so that All records will print in different page based on Indicator. (I use iReport Group By function)
However, I have certain time that when it query my sql, it will return only A and C.. How if I wanna generate Ind = B too with text NO RECORD FOUND
This is my current expression:
($F{IND}.equals("A")) ? "SMS MESSAGE NOT FOUND" : (
($F{IND}.equals("B")) ? "CW MESSAGE NOT FOUND" : (
($F{IND}.equals("C")) ? "STATUS MIS-MATCHED" : null
)
)
How can I check if IND = B not exist in my database, then print NO RECORD FOUND ?
Thanks in advance for your help !
You can use a special variable to count the rows in the group B.
Either use the built-in variable ${YOUGROUP_COUNT} (source)
Or increment a variable yourself : see more information here
When you have this variable set up, you can have a textField containing NO RECORD FOUND. Use the attribute printWhenExpression to display this textField only when the group count is equal to zero.
<staticText>
<reportElement x="234" y="10" width="100" height="30" uuid="05895bf2-3ce1-4d88-82fe-ff3fd650eaf6">
<printWhenExpression><![CDATA[${YOURGROUP_COUNT} == 0]]></printWhenExpression>
</reportElement>
<text><![CDATA[NO RECORD FOUND]]></text>
</staticText>
Don't display this static text element in a detail band, because it won't be displayed if there is no record. Use any other band to display it.

Jasper - How to remove complete row when 1 perticular field is null?

I want to print pdf something like this
Name Class RollNo
------- ---------- -----------
John 5 <null>
Mark 5 103
Robert 6 104
I need to add condition if RollNo is null then remove that row in 'detail' band.
You can use the report's filter expression or the detail band's print when expression. The filter expression completely skips the record, which is not counted and does not participate in aggregations, while the band's print when expression simply inhibits the band from printing.
<filterExpression>$F{RollNo} != null</filterExpression>
...OR...
<detail>
<band height="x">
<printWhenExpression>$F{RollNo} != null</printWhenExpression>
<textField>
...

How to get sum of entire field?

I'm going to get the sum of a entire field in Jasper Report. I'm passing JRTableModelDataSource from a Java code.
Table
Unit Price | Quantity
100.00 | 5
150.00 | 2
200.00 | 4
I'm printing these values in my report using field.
Field Expression
$F{Unit Price}*$F{Quantity}(This is the variable named '$V{MUL}')
and it prints
500.00
300.00
800.00
And now I need to prints the sum of all these values such as,
500.00
300.00
800.00
------
1600.00
I need to print this 1600.00(Sum of entire Field) in my report.
I tried with this,
SUM($V{MUL})
$V{MUL} is the variable that of multiplied values(500.00,300.00,800.00)
But I got an error. Is this possible to do or How can I do this?
You can do as shown below
<variable name="GrpAmount" class="java.math.BigDecimal" resetType="Group" resetGroup="keyType" calculation="Sum">
<variableExpression><![CDATA[$F{Unit Price}*$F{Quantity}]]></variableExpression>
</variable>

Jasper Reports: page x of y within one record

I have report with multiple records, where one record is consisting of 1-5 pages. How to display "page x of y", where x is a number of page for actual record and y is total pages for actual record ? I have something like below for x variable (reset on new record, incremet by page), but it doesn't work (on each page x have 1 value):
<variable name="x" class="java.lang.Integer" resetType="Group" resetGroup="report_count" incrementType="Page" calculation="Count">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[new Integer(1)]]></initialValueExpression>
</variable>
<!-- group by record -->
<group name="report_count" isStartNewPage="true">
<groupExpression><![CDATA[$V{REPORT_COUNT}]]></groupExpression>
</group>
<textField evaluationTime="Now" evaluationGroup="report_count">
<reportElement x="141" y="5" width="156" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{x}+" of"]]></textFieldExpression>
</textField>
The problem is that calculation="count" does not do what you are expecting it to. It returns the count of non-null values that your variableExpression returns. As your variableExpression only returns a single value, the variable is always set to 1.
An easy solution is to set the calculation type to "Nothing", and the variableExpression to $V{x}+1
i.e:
<variable name="x" class="java.lang.Integer" resetType="Group" resetGroup="report_count" incrementType="Page" calculation="Nothing">
<variableExpression><![CDATA[$V{x} + 1]]></variableExpression>
<initialValueExpression><![CDATA[new Integer(1)]]></initialValueExpression>
</variable>
Edit: Alternative solution
The group tag can have the attribute isResetPageNumber. When set to true it will reset the built-in variable PAGE_NUMBER at the start of every group. As you are already grouping by each record, I think this should give you the effect you are looking for.

How can I increment a variable with value of another variable in JasperReports?

I need to make a grand total of the items I'm counting in a subReport. To do that, I think I need to add the value of that variable to another variable for each iteration, or "increment" it by that value. The subReport gets called for each group, and I get a total for that group. I need to add the variable values, rather than database columns/fields.
I'm receiving an integer returnValue from the subReport, which is itself the count of rows in the sub-report. I want to get the grand total, since that subReport is called multiple times for the different results (each for a GROUP) from my main SQL query. I want to add up all the results, but I'm getting a null value. I tried adding an operation to the subReport as a new returnValue and choosing Sum as the operation, but that also yielded a null.
<variable name="itemCount" class="java.lang.Integer" resetType="None"/>
<variable name="grandCount"
class="java.lang.Integer"
incrementType="Group"
incrementGroup="ITEM_BUNDLE">
<variableExpression><![CDATA[$V{itemCount}]]></variableExpression>
</variable>
...
<returnValue subreportVariable="countItems" toVariable="itemCount"/>
Add attribute calculation="Sum" to variable name="grandCount"
or pass grandCount to subreport as parameter
<subreportParameter name="grandCount">
<subreportParameterExpression><![CDATA[$P{grandCount}]]></subreportParameterExpression>
</subreportParameter>
in subreport declare variable countItems with initialValue of parameter grantCount
<variable name="countItems" .... >
<variableExpression><![CDATA[$P{itemCount} + $P{grandCount}]]></variableExpression>
<initialValueExpression><![CDATA[$P{grandCount}]]></initialValueExpression>
</variable>
and return
<returnValue subreportVariable="countItems" toVariable="grandCount" calculation="Sum"/>
Im not exactly shure how to write it in JRXML since i use iReport.
In iReport, i create a new Variable, with class type "Integer", and calculation type "System"
The calculation type is important here.
In the variable expression, you will need something like $V{grandCount} = $V{grandCount} + $V{itemCount}
NOTE: JasperReports render band by band, so you wont be able to use the grandCount variable in a band before the subreport band.
Hope im not too late
You might try to increment your variable (I named it totalSum) only when the band (group) is equal to the one on which the subreport is. For this you would need a field in the report to give you the current band (group).
<variable name="totalSum"
class="java.lang.Integer"
resetType="Report"
incrementType="Group"
incrementGroup="ITEM_BUNDLE"
calculation="Nothing">
<variableExpression>
<![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : $V{totalSum}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
I'm not sure if this works, I don't have the context to test it. But you might also try a second solution - with three variables. For example, you keep the value returned from the subreport (let's say returnValue) in a variable and you use another two variables to hold the sum - one until the subreport is called (let's say partialSum) and the second to store the sum between the returnValue and the partialSum. Let's call it totalSum. Then you would have something like this for totalSum:
<variable name="totalSum"
class="java.lang.Integer"
resetType="Report"
incrementType="Group"
incrementGroup="ITEM_BUNDLE"
calculation="Nothing">
<variableExpression>
<![CDATA[$V{returnValue} + $V{partialSum}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
For partialSum, you'll have something like this:
<variable name="partialSum"
class="java.lang.Integer"
resetType="Report"
calculation="Sum"
incrementType="None">
<variableExpression>
<![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : new Integer(0)]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
I hope this helps a bit. It would be easier to make all these settings from iRport directly on the report you want to use.