Jaspersoft iReport Can we do subtotals in a list component? - jasper-reports

I just wish to know if it is possible to do subtotals in a list component? If so, is it like using a variable and putting the reset type as 'Group' and putting the expression?

There are many limitations of List component (calculations, return values, headers and footers, ...).
See section "13.1.3 List Component Issues" in ireport-ultimate-guide.
Try to use subreport instead list. It is more suited for computation on a subset of data

Yes it is possible.
First you create a variable in the dataset then open variable properties, set calculation sum
Set variable expression which field you want to sum from list component (like $F{paidAmount}) and save
Create a variable in the main report
Open report XML source then go at position in list component and assign value from dataset to main variable like this way
<returnValue fromVariable="sumOfPayment"toVariable="mainSumOfPayment"/>
Example :
<datasetRun subDataset="CreditorList" uuid="6aebc237-1aa2-47db-9435-8b133cef2b31">
<dataSourceExpression><![CDATA[$F{invoices}]]></dataSourceExpression>
<returnValue fromVariable="sumOfPayment" toVariable="mainSumOfPayment"/>
</datasetRun>
then drag the mail variable to the possible you what to show sum that list component field out side of list .and open the variable properties panel and must set Evaluation Time -Report

Related

jasper report detect if datail band or subreport is present in current page

I'm developing a report with jasper.
The main report has several subreports.
Only one of these subreport has a fixed text with a note number, something like this.
my text<sup>(2)</sup>
In main report I must print the footnote, but only in all the pages in which the text with the note is present.
An image to better explain:
Is there something that permit to detect if a subreport is present in the current page?
Or something that permit to detect if a detail band is present in the current page?
Or something that permit to detect if a particular text will be printed in current page?
Or other strategy to handle this particular situation?
. Create Variable in main report 'subReportCount'
Type = Integer, default 0
Reset Type = 'Page'
. Place this variable (hidden) somewhere after Subreport
. Create variable 'SubVar' in subreport , expression == $V{REPORT_COUNT}
. In main, go to subreport properties -> Edit return Values - >
Set From Varible= SubVar , To Variable=subReportCount, Calculation type = Sum
. Page Footer Print When Expression = $V{subReportCount} > 0
This may work if you are trying if subreport is printed atleast once in page.
Does not matter if you place subreport variable in report or not.
I tried on my local , its working for similar situation
Variable in Main report
<variable name="subReportCount" class="java.lang.Integer" resetType="Page" incrementType="Page" calculation="Sum">
<variableExpression><![CDATA[$V{subReportCount}.equals( null ) ? 0 : $V{subReportCount}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
Return Value for the subreport
<returnValue subreportVariable="SubVar" toVariable="subReportCount"/>
Print when expression for Footer element, in my case I am showing Page number
<printWhenExpression><![CDATA[$V{subReportCount} > 0]]></printWhenExpression>
This should work , if not please share your jrxml and sample data.
Good Luck!

Round up double value in jasper report

I have a textfield in jasper report as follows :
<textFieldExpression class="java.lang.Double"><![CDATA[$V{v_vcTotal}]]></textFieldExpression>
I want to round up this value using HALF_UP rounding mode.Can i achieve this in a simple way.Thanks in advance.
EDIT : I dont want to change my java class. Is there anything i can do in jasper jrxml file.
So all credit to #utkarsh their expression is so far the only working one I've seen online that converted my 'double' numbers pulled onto my report, my expression:
$f{balance}
Would produce doubles from a calculated field in my database like "-1365.00000000003"
In the Expression editor (in lieu of changing it in the source tab) I wanted to pull 2 sig figs from the number for looks. I added this expression and I stopped getting "compilation" errors and the wanted results:
new BigDecimal($F{balance}).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()
Result would be "-1365.00"
From what I've read this is a bit overkill to get the desired result but I am also under a deadline so I will try to optimize some other time.

How to dynamically order subreports in main report(jasper report)

Suppose,One main report can have 9 sub-reports.
Then user can have UI from which user can order the sub-report according to his/her choice.(like,which sub-report should be displayed first and which is next and so on).
SO,how can i achieve this ?
Follow the below steps to achieve your requirements
Create 9 parameters say, reportpath1,reportpath2,reportpath3,....reportpath9.
Prompt user to enter the order of the reports. Based on the order pass the respected report path to every parameter.
Meaning, if subreport9 is selected as 1st report, then pass the path of that subreport9 as reportpath1, if subreport8 is selected as 2nd report, then pass the path of that report as reportpath2 and so on
For example, if path of subreport9 is "report/samples/subreports/subreport9", then set parameter reportpath1 value as "report/samples/subreports/subreport9". If path of subreport8 is "report/samples/subreports/subreport8", then set paramter reportpath2 value as "report/samples/subreports/subreport8" and so on.
In report design, create 9 subreport and instead of hard-coding the path of each report, pass parameter in subreportexpression as below
For subreport1,
<subreportExpression><![CDATA[$P{ReportPath1}]]></subreportExpression>
For subreport2,
<subreportExpression><![CDATA[$P{ReportPath2}]]></subreportExpression>
and so on.
Hope this should solve your problem.
Answer for getting variable from subreport
Right click on subreport in Parent report.
Go to Properties
Under Subreport session, click "Return values"
Add the subreport variable and map it to the parentreport variable.
Then use that parentreport variable in your parent report.
Hope this should solve your another problem.

iReport sum of two measures

I'm having problem with JasperReports report by getting sum of two fields.
I have created so far variables that holds fields "TareWeight" variable that is casting values from double to float
new Float($F{EquipmentTareWeightKg})
"CargoWt" variable that is casting from string to float, etc...
Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04})
So now I have two variables which I can manipulate with.
The problem starts when I wish to do a "recap" using crosstab. I have setup two groups that is done with wizard, and added another in crosstab. (please refer to the printscreen below)
And now, I have created under measures SumOf Tare that is using variable TareWt, and SumOfCargoWt that is using variable CargoWt.
I have tried to create another Measure SumOfGrossWt that would do a sum of variables TareWt + CargoWt but I have got some nonsense values.
I have tried to do another approach by creating variable GrossWt that would basically do exact the same thing as previous attempt
$V{Tare}+$V{CargoWt}
or use an variable: GrossWt
$V{Tare}+$V{CargoWt} or new Float($F{EquipmentTareWeightKg})+(Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04}))
None of these actually worked: (Sum should be done by "Sum Of Tare"+ "Sum of CargoWt" = "Sum of GrossWt") plese refer to the second picture.
Here is a complete jrxml code/file:
jrxml file
save it as *.jrxml
Can please someone help me find a solution? how can I sum those two measure fields and get correct values?
Since CargoWt field had some null values in database, addition float value with null is equal to null, so the only value which was displayed on report are the one that had values for CargoWt, all others with null value were "ignored".

How to pass the parameter from one subreport to another subreport

I am newbie and I'm designing my report using iReport 4.5.
I have a main report (MainReport) and three subreports (Sub1, Sub2, Sub3)
In Sub1 I have three summary variables say presentPayable, presentPayment, balance
In Sub2 I have one summary variable say totalCost
I need to use the summary variables of Sub1, Sub2 in my Sub3.
How can I do this? Is this possible to pass the variables from one subreport to another?
Otherwise please provide me any alternate to do this.
To pass a value from a subreport to its parent, the parent must first have a variable to receive the value. In your case the main report should have 4 variables, one each for presentPayable, presentPayment, balance, and totalCost.
Next you need to add a returnValue element to the subreport element in the main report. This element maps a variable in the subreport to a variable in this report using the attributes subreportVariable and toVariable.
To do this in iReport, click on your subreport element in the main report. In the properties list, click on Return Values. A dialog should appear. Click on the Add button. Type the name of the subreport variable and select the variable in this report that you would like it to be transferred to. You should leave the calculation type as "Nothing", which will instruct jasper to simply overwrite the variable with the new value. Click Ok to add this, then repeat for the other variables/subreports.
Now when you run the report, each time the subreport has completed processing, the current value of the variable in the subreport is passed back to the specified variable in the main report.
To use that value in another subreport, you need to pass the variable from the main report to the other subreport as a parameter. This has two parts: Adding a subreportParameter to the subreport element in the parent report, and adding a parameter to the subreport itself.
In iReport, click on your subreport element in the main report. In the properties list, click on Parameters. In the dialog that appears, click the Add button. Give the parameter a name (e.g. presentPayable) and input a value expression that references the variable in your main report (e.g. $V{presentPayable}). Repeat this for each of the variables that you want to pass in.
Next, open your subreport. In the report inspector, right-click on Parameters. Select Add Parameter, then rename the new parameter to match the name you entered in the previous step.
In the subreport, you should now be able to reference these values like any other parameter (e.g. $P{presentPayable}).