这很容易。您可以在产品字段上创建组,并在该组上创建两个变量:用于计算结果总和和平均总和。借助内置变量,您可以计算并显示该组中的元素数量。
这是工作样本:
<?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="group_average2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<queryString>
<![CDATA[]]>
</queryString>
<field name="product" class="java.lang.String"/>
<field name="price" class="java.lang.Integer"/>
<sortField name="product"/>
<variable name="totalSum" class="java.lang.Double" resetType="Group" resetGroup="productGroup" calculation="Sum">
<variableExpression><![CDATA[$F{price}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<variable name="averageSum" class="java.lang.Double" resetType="Group" resetGroup="productGroup" calculation="Average">
<variableExpression><![CDATA[$F{price}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<group name="productGroup">
<groupExpression><![CDATA[$F{product}]]></groupExpression>
<groupFooter>
<band height="20">
<textField>
<reportElement x="0" y="0" width="122" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{product}]]></textFieldExpression>
</textField>
<textField pattern="###0.00;-###0.00">
<reportElement x="244" y="0" width="122" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{totalSum}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="122" y="0" width="122" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$V{productGroup_COUNT}]]></textFieldExpression>
</textField>
<textField pattern="###0.00;-###0.00">
<reportElement x="366" y="0" width="149" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{averageSum}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<pageHeader>
<band height="19">
<staticText>
<reportElement x="0" y="-1" width="122" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Product]]></text>
</staticText>
<staticText>
<reportElement x="122" y="-1" width="122" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Number of orders]]></text>
</staticText>
<staticText>
<reportElement x="244" y="-1" width="122" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Total sum]]></text>
</staticText>
<staticText>
<reportElement x="366" y="-1" width="149" height="20"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Average sum of order]]></text>
</staticText>
</band>
</pageHeader>
</jasperReport>
此示例将 CSV 文件作为数据源。您可以编辑查询 - 用于使用 SQL 查询获取数据。
iReport中的报表设计为:
结果将是(通过iReport中的预览):
你应该阅读这篇关于在JasperReports中使用变量的文章和这篇关于在JasperReports中使用组的文章。
请记住,您应该在分组使用之前对数据进行排序。来自jasperforge.org的关于分组和排序的帖子的引用:
In order to get an accurate data representation, the data in the data source should be already ordered according to the group expressions used in the report. One can either perform data sorting through the report query, or use the <sortField/> element.