Jump to content

# percentage of total on detail line

## Recommended Posts

By: Alex Kotchnev - polrtex

percentage of total on detail line

2004-03-22 17:33

I was wondering.. is there a straightforward way to calculate and display on each detail line the percentage of the total for a summed field.

Example:

1. The report is grouped by field1

2. There is a variable \$V{subTotal} that is declared as a sum of field1 (reset on group field1)

I need to display what percentage of the total sum (\$V{subTotal}) each detail line is. Here is an example:

1. I create a text box with the expression

\$F{field1}.doubleValue()/\$V{subTotal}.doubleValue().

- Now, if I set the evaluation time of this text box to "Now", the \$V{subTotal} doesn't have the total sum yet - so it doesn't give me the right result

- If I set the evaluation time of the text box to group, \$V{subTotal} has the right amount; however, \$F{field1} doesn't have it (as it would make sense, it has the value of the last detail line in the group).

I was thinking about the possibility of keeping the evaluation time of the text box to "group", and on each detail line, add the value of \$F{field1} to a Hashmap - hoping that I would be able to do the calculation for the percentage textbox with the stored value at "group" evaluation time by retrieving the value from the hashmap... but that doesn't work..

Is there a good way of doing this, short of creating a scriptlet, that would retrieve (possibly with another query) the subtotal for the group on each detail line ?

By: Ralf Weinand - weinanr

RE: percentage of total on detail line

2004-04-30 06:18

Hi Alex,

I have a similar problem.

Did you find a solution now?

Ralf

By: Andrew McLaughlin - pik0

RE: percentage of total on detail line

2004-08-17 17:24

Ack!! I just posted the EXACT same problem!! Is this unsolvable?!

Piko

By: Giulio Toffoli - gt78

RE: percentage of total on detail line

2004-08-18 03:46

No, this problem has not solution in JasperReports because the percentage function requires the value of the same object calculated in different times ("now" and at the end of a group or at the end of the report).

The ONLY solution is precalculate the total to calculate the percentage.

Giulio

By: Andrew McLaughlin - pik0

RE: percentage of total on detail line

2004-08-18 10:24

Okay. I know I can run a query that will return the total value that I need to compare with. What's the easiest way to run this secondary query and get that value back as a static variable value?

Piko

By: Andrew McLaughlin - pik0

RE: percentage of total on detail line

2004-08-18 11:39

Okay, I've solved this problem. It took a little reverse thinking, but this is how I did it:

Rather than trying to pass the result of the subreport back to the main report to be used in the percentage calculation, I realized that the subreport was the PERFECT place to to actually do the percent calc.

For this reason, the subreport was passed the numerator value as a parameter. Then, as a matter of course, the subreport would then do it's query on the database to get the total:

select sum(amount) as sum_amount from auction where blah blah blah...

Then the subreport's single report field did the calc:

new Double( \$P{AMOUNT} / \$F{SUM_AMOUNT} )

Which works amazingly well. Once I set up the AMOUNT value as a parameter to the subreport from the mastereport, it was smooth sailing.

Piko

• Replies 0
• Created
• Last Reply

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×
×
• Create New...