Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate percentage for rows based on total sums?

Price is represented by $F{price} and I created a variable that sums the prices of each item to get the total price $V{total_price}, both of them are java.Math.BigDecimal.

I read a few posts that suggested changing the Evaluation Time property to AUTO for the total text field which I've done but percentage is calculated on the cumulative sum at each item hence I get 100 for the percentage of the first item and so on. How can I get the total price at once at when evaluating for the percentage of each item?

Example

like image 730
Jickery Avatar asked Oct 21 '25 04:10

Jickery


1 Answers

Yes you are close, auto should be used on percentage textField.

As far as I have understood you have this variable, to sum up the price's

<variable name="total_price" class="java.math.BigDecimal" calculation="Sum">
    <variableExpression><![CDATA[$F{price}]]></variableExpression>
</variable>

To get your percentage field just add a textField with evaluationTime="auto" where you divide the $F{price} with $V{total_price}

Auto - Each variable participating in the text field expression is evaluated at a time corresponding to its reset type. Fields are evaluated Now. This evaluation type should be used for expressions that combine values evaluated at different times, like the percentage out of a total.

Example

<textField evaluationTime="Auto" pattern="#,##0.00 %">
    <reportElement x="200" y="0" width="100" height="20" uuid="e88223c7-12b6-401d-80b7-6c8341773027"/>
    <textElement textAlignment="Right" verticalAlignment="Middle"/>
    <textFieldExpression><![CDATA[$F{price}.doubleValue()/$V{total_price}.doubleValue()]]></textFieldExpression>
</textField>
like image 81
Petter Friberg Avatar answered Oct 25 '25 03:10

Petter Friberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!