价格由 $F{price}
表示,我创建了一个变量,对每个项目的价格求和以获得总价 $V{total_price}
,两者其中有 java.Math.BigDecimal
。
我阅读了一些帖子,建议将 Evaluation Time
属性更改为 AUTO
以获取总的 text field
我已经完成但百分比是根据每个项目的累计总和计算的,因此我得到第一个项目的百分比为 100,依此类推。如何在评估每件商品的百分比时一次性得到总价?
最佳答案
是的,您很接近,应该在百分比文本字段上使用自动。
据我所知,你有这个变量来总结价格
<variable name="total_price" class="java.math.BigDecimal" calculation="Sum">
<variableExpression><![CDATA[$F{price}]]></variableExpression>
</variable>
要获取您的百分比字段,只需添加一个带有 evaluationTime="auto"
的文本字段,您将 $F{price}
除以 $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.
示例
<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>
关于jasper-reports - 如何根据总和计算行的百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36653046/