获取行(组)的累积总和

时间:2016-10-25 07:46:59

标签: jasper-reports

想象一下以下数据集:

Cumulative data set

在JasperReports中获取 TOTAL_CUMULATIVE 的值的正确方法是什么? 即:在“VW”行中,它应该将所有值(COST - EXPENSE)加起来直到“VW”,这意味着:(500 - 150)+(400- 200)= 550。

列xml代码:

<textField isStretchWithOverflow="true" pattern="#,##0.00" isBlankWhenNull="true">
        <reportElement x="746" y="0" width="65" height="13"/>
        <box topPadding="1" leftPadding="1" bottomPadding="1" rightPadding="1"/>
        <textElement textAlignment="Right" verticalAlignment="Middle"/>
        <textFieldExpression><![CDATA[$V{BALANCE_BY_ACCOUNTING}]]></textFieldExpression>
</textField>

变量:

<variable name="BALANCE_BY_ACCOUNTING" class="java.lang.Double" resetType="Group" resetGroup="groupByAccounting" calculation="Sum">
    <variableExpression><![CDATA[$F{cost} - $F{expense}]]></variableExpression>
</variable>

“VW”行中,代码将TOTAL_CUMULATIVE列值设置为:200。它不应该。它应设置为累积和550.这同样适用于其他行。在“MERCEDES”行中,它设置值600(应为11500),依此类推。

1 个答案:

答案 0 :(得分:1)

为了获得累积总和,可以使用没有为组中的每个新enrty设置值的简单总和。

实施例

car 字段将帮助我们解决任务和两类变量:

  1. 第一个用于计算组的总和( resetType="Group" resetGroup="carGroup"
  2. 第二个用于计算累计和( resetType=Report
  3. 所有数据都将放置在 groupFooter 频段。

    要获得有效结果,数据应按 car 订购。

    数据源

    使用简单的 csv 数据源就足够了。

    car,cost,expense
    BMW,200,50
    BMW,100,50
    BMW,200,50
    VW,200,100
    VW,200,100
    MERCEDES,200,50
    MERCEDES,500,50
    

    以下示例中此数据源的数据适配器名称为 cars.csv 。跳过文件的第一行 - 它包含列的名称。

    报告模板

    我们需要显示累积总和的变量是:

    <variable name="total" class="java.lang.Integer" calculation="Sum">
        <variableExpression><![CDATA[$F{cost} - $F{expense}]]></variableExpression>
    </variable>
    

    带有$V{total}表达式的 textField 放置在 groupFooter 中。

    jrxml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Grouping, sum" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" >
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="cars.csv"/>
        <field name="car" class="java.lang.String"/>
        <field name="cost" class="java.lang.Integer"/>
        <field name="expense" class="java.lang.Integer"/>
        <variable name="costForCar" class="java.lang.Integer" resetType="Group" resetGroup="carGroup" calculation="Sum">
            <variableExpression><![CDATA[$F{cost}]]></variableExpression>
        </variable>
        <variable name="expenseForCar" class="java.lang.Integer" resetType="Group" resetGroup="carGroup" calculation="Sum">
            <variableExpression><![CDATA[$F{expense}]]></variableExpression>
        </variable>
        <variable name="totalForCar" class="java.lang.Integer" resetType="Group" resetGroup="carGroup" calculation="Sum">
            <variableExpression><![CDATA[$F{cost} - $F{expense}]]></variableExpression>
        </variable>
        <variable name="total" class="java.lang.Integer" resetType="Report" calculation="Sum">
            <variableExpression><![CDATA[$F{cost} - $F{expense}]]></variableExpression>
        </variable>
        <group name="carGroup">
            <groupExpression><![CDATA[$F{car}]]></groupExpression>
            <groupFooter>
                <band height="15">
                    <textField>
                        <reportElement x="0" y="0" width="100" height="15" />
                        <textFieldExpression><![CDATA[$F{car}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="100" y="0" width="110" height="15" />
                        <textFieldExpression><![CDATA[$V{costForCar}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="210" y="0" width="110" height="15" />
                        <textFieldExpression><![CDATA[$V{expenseForCar}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="320" y="0" width="110" height="15" />
                        <textFieldExpression><![CDATA[$V{totalForCar}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="430" y="0" width="110" height="15" />
                        <textFieldExpression><![CDATA[$V{total}]]></textFieldExpression>
                    </textField>
                </band>
            </groupFooter>
        </group>
        <columnHeader>
            <band height="15" splitType="Stretch">
                <staticText>
                    <reportElement x="0" y="0" width="100" height="15" />
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Car]]></text>
                </staticText>
                <staticText>
                    <reportElement x="100" y="0" width="110" height="15" />
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Cost]]></text>
                </staticText>
                <staticText>
                    <reportElement x="210" y="0" width="110" height="15" />
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Expense]]></text>
                </staticText>
                <staticText>
                    <reportElement x="320" y="0" width="110" height="15" />
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Total]]></text>
                </staticText>
                <staticText>
                    <reportElement x="430" y="0" width="110" height="15" />
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Total, cumulative]]></text>
                </staticText>
            </band>
        </columnHeader>
    </jasperReport>
    

    借助详细信息频段和 textField 元素的 evaluationTime 属性,可以实现同样的效果。

    输出结果

    Jaspersoft Studio(JSS)的结果

    The result in JSS

相关问题