René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Jasper - Variables

This example demonstrates the use of variables in order to display sub totals on group levels. Look out for the variable named $V{VAL_1}.
<?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         =  "Variables" 
  pageWidth    =        "595" 
  pageHeight   =        "845"
  columnWidth  =        "595" 
  leftMargin   =          "0" 
  rightMargin  =          "0" 
  topMargin    =          "0" 
  bottomMargin =          "0"
>

	<queryString>
		<![CDATA[
    
    ---------------

select color, taste, val from (
  select 'green' color, 'sweet'  taste, 2 val from dual union all
  select 'green' color, 'sweet'  taste, 3 val from dual union all
  select 'green' color, 'sweet'  taste, 4 val from dual union all
  select 'green' color, 'salty'  taste, 7 val from dual union all
  select 'green' color, 'salty'  taste, 9 val from dual union all
  select 'red'   color, 'sweet'  taste, 1 val from dual union all
  select 'red'   color, 'bitter' taste, 2 val from dual union all
  select 'red'   color, 'bitter' taste, 5 val from dual union all
  select 'blue'  color, 'greasy' taste, 6 val from dual
)
order by color, taste

    ---------------

]]>
	</queryString>

	<field name="COLOR" class="java.lang.String">     <fieldDescription><![CDATA[]]></fieldDescription> </field>
	<field name="TASTE" class="java.lang.String">     <fieldDescription><![CDATA[]]></fieldDescription> </field>
	<field name="VAL"   class="java.math.BigDecimal"> <fieldDescription><![CDATA[]]></fieldDescription> </field>

	<variable name="VAL_1" 
            class="java.math.BigDecimal" 
            resetType="Group" 
            resetGroup="TASTE" 
            calculation="Sum"> 
              <variableExpression><![CDATA[$F{VAL}]]></variableExpression> 
  </variable>

	<group name="COLOR">
		<groupExpression><![CDATA[$F{COLOR}]]></groupExpression>

		<groupHeader>
			<band height="13" splitType="Stretch">

				<staticText>
					<reportElement x="30" y="0" width="40" height="13"/>
					<text><![CDATA[Color:]]></text>
				</staticText>

				<textField>
					<reportElement x="72" y="0" width="40" height="13" />
					<textFieldExpression class="java.lang.String"><![CDATA[$F{COLOR}]]></textFieldExpression>
				</textField>

			</band>
		</groupHeader>

		<groupFooter> <band/> </groupFooter>
	</group>



	<group name="TASTE">
		<groupExpression><![CDATA[$F{TASTE}]]></groupExpression>

		<groupHeader>
			<band height="13">

				<staticText>
					<reportElement x="60" y="0" width="40" height="13"/>
					<text><![CDATA[Taste:]]></text>
				</staticText>

				<textField>
					<reportElement mode="Opaque" x="105" y="0" width="60" height="13" />
					<textFieldExpression class="java.lang.String"><![CDATA[$F{TASTE}]]></textFieldExpression>
				</textField>

			</band>
		</groupHeader>

		<groupFooter>
			<band height="13" splitType="Stretch">

				<staticText>
					<reportElement x="120" y="0" width="130" height="13"/>
					<text><![CDATA[Total for Color and taste:]]></text>
				</staticText>
        
				<textField>
					<reportElement x="250" y="0" width="100" height="13"/>
					<textElement/>
					<textFieldExpression class="java.math.BigDecimal"><![CDATA[$V{VAL_1}]]></textFieldExpression>
				</textField>
			</band>
		</groupFooter>
	</group>

	<title>        <band/> </title>
	<pageHeader>   <band/> </pageHeader>
	<columnHeader> <band/> </columnHeader>

	<detail>

		<band height="13" splitType="Stretch">
			<staticText>
				<reportElement x="150" y="0" width="100" height="13"/>
				<text><![CDATA[Value:]]></text>
			</staticText>
			<textField>
				<reportElement x="250" y="0" width="100" height="13"/>
				<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{VAL}]]></textFieldExpression>
			</textField>
		</band>
	</detail>

	<columnFooter> <band/> </columnFooter>
	<pageFooter>   <band/> </pageFooter>
	<summary>      <band/> </summary>

</jasperReport>