È abbastanza facile. È possibile creare un gruppo sul campo prodotto e due variabili su questo gruppo: per il conteggio della somma dei risultati e della somma media. Con l'aiuto della variabile integrata puoi calcolare e visualizzare il numero di elementi in questo gruppo.
Questo esempio sta lavorando:
<?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>
Questo campione prende file CSV come origine dati. È possibile modificare la query - per ottenere dati con query SQL.
Il design report in iReport è:
Il risultato sarà (tramite anteprima in iReport):
Si dovrebbe leggere questo articolo su using variables in JasperReports e questo post su using groups in JasperReports.
Si prega di di ricordare che è necessario ordinare i dati prima di utilizzare il gruppo. La citazione dal post sul raggruppamento e ordinamento dal 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.
fonte
2012-07-17 14:43:40
Grazie! Il trucco che mi mancava era la band di gruppo. La parte di iReport con cui continuo a lottare sono le band. La stessa variabile mostrerà risultati diversi a seconda della banda in cui entra. Perché è importante se i dati sono ordinati o no? – Marianna
Come addendum al mio problema originale ... C'è un modo per usare un moltiplicatore per la mia somma totale? Ho una colonna aggiuntiva, stato, che indica lo stato in cui è stato acquistato. Ho una tabella STATE_SALES_TAX che voglio interrogare per ogni record. Se compri il mio latte in New Jersey, l'imposta sulle vendite è 1,06. Compro un altro gallone a New York ed è 1,08.C'è un modo per riassumere il totale speso per il latte usando questo moltiplicatore da un altro tavolo? Speso totale per latte = somma (eachMilkPx * salesTax (stato Acquistato)) – Marianna
Sì, è possibile creare l'espressione di qualsiasi variabile necessaria. –