Imagine the following data set:
What's the proper way to get the values of TOTAL_CUMULATIVE in JasperReports?
i.e: in "VW" row it should sum all values (COST - EXPENSE) until "VW", which means: (500 - 150) + (400-200) = 550.
column xml code:
<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>
The variable:
<variable name="BALANCE_BY_ACCOUNTING" class="java.lang.Double" resetType="Group" resetGroup="groupByAccounting" calculation="Sum">
<variableExpression><![CDATA[$F{cost} - $F{expense}]]></variableExpression>
</variable>
In the "VW" row, the code sets TOTAL_CUMULATIVE column value to: 200. It should not. It should set to the cumulative sum 550. The same applies to the other rows. In "MERCEDES" row it sets value 600 (should be 11500) and so on.
For getting the cumulative sum the simple sum without resering value for every new enrty in Group can be used.
Example
The Group by car field will help us to solve the task and two types of variables:
First one for calculating sums for Group (resetType="Group" resetGroup="carGroup")
Second one for calculating cumulative sum (resetType=Report)
All data will be placed at groupFooter band.
For getting valid results the data should be ordered by car.
Datasource
The using of simple csv datasource is enough for this example.
car,cost,expense
BMW,200,50
BMW,100,50
BMW,200,50
VW,200,100
VW,200,100
MERCEDES,200,50
MERCEDES,500,50
The name of data adapter for this datasource in the example below is cars.csv. The first line from the file is skipped - it is contains the column's name.
Report template
The variable we need to show cumulative sum is:
<variable name="total" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{cost} - $F{expense}]]></variableExpression>
</variable>
The textField with $V{total} expression is placed in groupFooter.
The jrxml file:
<?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>
The same effect can be achieved with help of Detail band and evaluationTime attribute of textField element.
Output result
The result in Jaspersoft Studio (JSS):
Related
I have a customer request to split details of personal information (name, address, secondary addresses, etc) across multiple columns, if the dynamic size does not fit in the current column. As a caveat, the name field, followed by the word '(Continued)', is desired, as the title for the segments in subsequent columns.
Is this behavior possible with jasper-reports?
I have tried toggling an 'inBetween' flag with the beforeDetailEval() and afterDetailEval() hooks, checking that flag in the column header band, to optionally reprint the name + 'Continued', but the column header always calls the scriptlet method AFTER the afterDetailEval() method (does not make sense to my little mind), failing since the 'inBetween' flag is reset to false.
(Note: Detail band has been set with splitType to both 'Immediate' and 'Stretch', but neither approach has behaved as expected. The entire band just gets restarted at the beginning of the next column)
Is this possible, and if so, are there any suggestions for creative solutions for this scenario?
Also, there is strange behavior displaying where if a detail is split across multiple columns, the first part of the detail (up to the subreport element)is duplicated on the following column. This behavior is also undesirable to the customer, and needs to be adjusted so nothing (except the title) is duplicated.
This is the syntax desired by the customer:
Name 1 Name 2
Address 1 (Continued)
City 1 AL – Address 3
State 1 AL - City 3
Zip 1 AL - State 3
Additional Locations: AL - Zip 3
AL-Address 1 ...
AL-City 1
AL-State 1
AL-Zip 1
AL-Address 2
AL-City 2
AL-State 2
AL-Zip 2
Name 2
Address 2
City 2
State 2
Zip 2
Additional Locations:
AL-Address 1
AL – City 1
AL-State 1
AL – Zip 1
AL – Address 2
AL – City 2
AL – State 2
AL – Zip 2
This is what we are actually getting:
Name1 Name1
Address1 Address1
City1 City1
State1 State1
Zip1 Zip1
Additional Locations Additional Locations
AL-Name1 AL-City2
AL-Address1 AL-State2
AL-City1 AL-State2
AL-State1
AL-Zip1
AL-Name2
AL-Address2
This is a sample main jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1 -->
<!-- 2017-06-22T09:48:08 -->
<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="Blank_A4" columnCount="2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" scriptletClass="path.to.my.scriptlet" uuid="af53d807-7975-4ff7-bfc5-e438944aa795">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="Name" class="java.lang.String"/>
<field name="Address" class="java.lang.String"/>
<field name="City" class="java.lang.String"/>
<field name="State" class="java.lang.String"/>
<field name="Zip" class="java.lang.String"/>
<field name="AdditionalLocations" class="java.util.List"/>
<variable name="IN_MIDDLE_OF_DETAIL" class="java.lang.Boolean" calculation="System">
<initialValueExpression><![CDATA[Boolean.False]]></initialValueExpression>
</variable>
<columnHeader>
<band height="74" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="-10" y="0" width="100" height="30" uuid="258dce49-fb61-4887-bb30-69e80e96d8f1">
<printWhenExpression><![CDATA[$V{IN_MIDDLE_OF_DETAIL}]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="-10" y="40" width="100" height="30" uuid="c18b2fab-900b-4641-aaef-2520510a3510">
<printWhenExpression><![CDATA[$V{IN_MIDDLE_OF_DETAIL}]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA["(CONTINUED)"]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="426" splitType="Stretch">
<textField>
<reportElement x="0" y="10" width="100" height="30" uuid="853a1b0d-a49e-45c4-8183-83cfd69bf5af"/>
<textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="50" width="100" height="30" uuid="46697c6d-1c2f-422b-9170-6b2f36ce13ba"/>
<textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="90" width="100" height="30" uuid="31730cd5-0b17-452e-8a22-d16ea2061605"/>
<textFieldExpression><![CDATA[$F{City}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="120" width="100" height="30" uuid="b4a4c671-dc63-41b4-b080-9ad5b750bb58"/>
<textFieldExpression><![CDATA[$F{State}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="160" width="100" height="30" uuid="8f8bec7c-3d4a-466b-8b02-2bb82e61904d"/>
<textFieldExpression><![CDATA[$F{Zip}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="220" width="100" height="110" uuid="8ef583fd-fa23-47d8-80d0-90de4f6478a0"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{AdditionalLocations})]]></dataSourceExpression>
<subreportExpression><![CDATA["Blank_A4_1.jasper"]]></subreportExpression>
</subreport>
<staticText>
<reportElement x="10" y="198" width="100" height="30" uuid="4887e0c1-3da2-4350-9454-4a3e33c0fe71"/>
<text><![CDATA[Additional Locations]]></text>
</staticText>
</band>
</detail>
</jasperReport>
This is the sample subreport jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1 -->
<!-- 2017-06-22T10:39:43 -->
<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="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9b8a1d93-2ee6-4d29-b9ec-7c56a3917729">
<queryString>
<![CDATA[]]>
</queryString>
<field name="Address" class="java.lang.String"/>
<field name="City" class="java.lang.String"/>
<field name="State" class="java.lang.String"/>
<field name="Zip" class="java.lang.String"/>
<detail>
<band height="168" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="10" width="100" height="30" uuid="3d74e624-6b6f-40e9-87d2-ddb5022668b2"/>
<textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="50" width="100" height="30" uuid="5df31fa3-38ee-44a4-8931-e8eef45fb7a6"/>
<textFieldExpression><![CDATA[$F{City}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="89" width="100" height="30" uuid="964a282f-54c7-44a3-954d-27d1c70d3d0c"/>
<textFieldExpression><![CDATA[$F{State}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="130" width="100" height="30" uuid="e1349222-d945-4dec-8454-e2d6e3fb6a2a"/>
<textFieldExpression><![CDATA[$F{Zip}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
This is the basic scriptlet code to toggle the value of IN_MIDDLE_OF_DETAIL:
#Override
public void beforeDetailEval() throws JRScriptletException
{
setVariableValue("IN_MIDDLE_OF_DETAIL", Boolean.TRUE)
}
#Override
public void afterDetailEval() throws JRScriptletException
{
setVariableValue("IN_MIDDLE_OF_DETAIL", Boolean.FALSE)
}
Drop the scriptlet, there is no need for this, lets use the fact that subreport's has its own page count, hence when it breaks in main report it counts a page
The solution I would use is the pageHeader band in the subreport with a printWhenExpression on page number (>1).
Which means that the text that you like to display when subreport splits to new "page" (name and "Continue") goes into pageHeader of the subreport and should be printed on all subreport "pages" except the first one.
Since name does not seems to be part of the subreport datasource I will pass this as a parameter to the subreport.
Code in subreport to display header when page > 1
<pageHeader>
<band height="44">
<printWhenExpression><![CDATA[$V{PAGE_NUMBER}>1]]></printWhenExpression>
<textField>
<reportElement x="0" y="0" width="220" height="20" uuid="8cce4cab-6ccf-4ddb-b1f1-508dc97bfcfe"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="20" width="220" height="19" uuid="c18b2fab-900b-4641-aaef-2520510a3510"/>
<textElement verticalAlignment="Middle">
<font isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA["(Continued)"]]></textFieldExpression>
</textField>
</band>
</pageHeader>
Example output
I have cut down page height to reduce image
Full example
**main report 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="Blank_A4" columnCount="2" pageWidth="595" pageHeight="500" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="af53d807-7975-4ff7-bfc5-e438944aa795">
<queryString>
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="address" class="java.lang.String"/>
<field name="city" class="java.lang.String"/>
<field name="state" class="java.lang.String"/>
<field name="zip" class="java.lang.String"/>
<field name="additionalLocations" class="java.util.List"/>
<detail>
<band height="141" splitType="Immediate">
<textField isStretchWithOverflow="true">
<reportElement x="0" y="20" width="280" height="20" uuid="46697c6d-1c2f-422b-9170-6b2f36ce13ba">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="0" y="40" width="280" height="20" uuid="31730cd5-0b17-452e-8a22-d16ea2061605"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="0" y="60" width="280" height="20" uuid="b4a4c671-dc63-41b4-b080-9ad5b750bb58"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="0" y="80" width="280" height="20" uuid="8f8bec7c-3d4a-466b-8b02-2bb82e61904d"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{zip}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="120" width="280" height="20" isPrintWhenDetailOverflows="true" uuid="8ef583fd-fa23-47d8-80d0-90de4f6478a0"/>
<subreportParameter name="name">
<subreportParameterExpression><![CDATA[$F{name}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{additionalLocations})]]></dataSourceExpression>
<subreportExpression><![CDATA["C:/Users/pette/JaspersoftWorkspace/MyReports/SplitSubreport.jasper"]]></subreportExpression>
</subreport>
<staticText>
<reportElement x="0" y="100" width="280" height="20" uuid="4887e0c1-3da2-4350-9454-4a3e33c0fe71"/>
<textElement verticalAlignment="Middle">
<font isItalic="false" isUnderline="true"/>
</textElement>
<text><![CDATA[Additional Locations]]></text>
</staticText>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="0" y="0" width="280" height="20" uuid="258dce49-fb61-4887-bb30-69e80e96d8f1"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
**subreport 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="Blank_A4_1" pageWidth="280" pageHeight="200" columnWidth="280" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9b8a1d93-2ee6-4d29-b9ec-7c56a3917729">
<parameter name="name" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="address" class="java.lang.String"/>
<field name="city" class="java.lang.String"/>
<field name="state" class="java.lang.String"/>
<field name="zip" class="java.lang.String"/>
<pageHeader>
<band height="44">
<printWhenExpression><![CDATA[$V{PAGE_NUMBER}>1]]></printWhenExpression>
<textField>
<reportElement x="0" y="20" width="220" height="19" uuid="c18b2fab-900b-4641-aaef-2520510a3510"/>
<textElement verticalAlignment="Middle">
<font isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA["(Continued)"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="220" height="20" uuid="8cce4cab-6ccf-4ddb-b1f1-508dc97bfcfe"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="85" splitType="Stretch">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="0" y="-1" width="220" height="21" uuid="3d74e624-6b6f-40e9-87d2-ddb5022668b2"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="0" y="20" width="220" height="20" uuid="5df31fa3-38ee-44a4-8931-e8eef45fb7a6"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="0" y="40" width="220" height="20" uuid="964a282f-54c7-44a3-954d-27d1c70d3d0c"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="0" y="60" width="220" height="20" uuid="e1349222-d945-4dec-8454-e2d6e3fb6a2a"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{zip}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
I have an input parameter which is an ID.
I want to use a lookup table in the database to expand that ID into a long name for use in the report title.
How could I just add text into a textfield from a SQL query without needing to worry about rows of tabular data?
You can use the List component in the Title band. This List (or Table component) will be associated with another additional (non main) datasource for showing information (the Name by Id passed via Parameter in your case).
The main datasource will be used by Detail band (or another Table component with the its own datasource) for showing data filtered by parameter's value (Id in your case).
With help of textField's property isStretchWithOverflow (with true value) we can garantee that all text will be drawing with the textField.
The sample
In this sample I've used the DB distributed with the Jaspersoft Studio. The parameter addrId was used for filtering data by the id field of address table. This parameter was mapped to the List's datasource parameter with the same name (addrId). Yes, I've used the List component placed on Title band for showing information about the value of our external parameter (the city and the street of address in this sample). At the Detail band we are showing the information about documents (table document) related to our address (defined by addrId)
The report's template
<?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="growing_text" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="77f0cb04-7f4b-43dc-af12-89c25fa7c58c">
<subDataset name="dsAddrTitle" uuid="0eb7cd0c-f4f1-408d-be13-dc484fda80d5">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
<parameter name="addrId" class="java.lang.Integer"/>
<queryString>
<![CDATA[SELECT city + ', ' + street AS name FROM address WHERE id=$P{addrId}]]>
</queryString>
<field name="NAME" class="java.lang.String"/>
</subDataset>
<parameter name="addrId" class="java.lang.Integer">
<defaultValueExpression><![CDATA[33]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT ID, ADDRESSID, TOTAL FROM DOCUMENT WHERE ADDRESSID=$P{addrId} ORDER BY ADDRESSID]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="ADDRESSID" class="java.lang.Integer"/>
<field name="TOTAL" class="java.math.BigDecimal"/>
<title>
<band height="10" splitType="Stretch">
<componentElement>
<reportElement x="160" y="0" width="40" height="10" uuid="f4cb4e5c-e2d7-4927-b143-4cfcd7d99b76"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" ignoreWidth="true">
<datasetRun subDataset="dsAddrTitle" uuid="4bf3eb57-f752-4856-ac3a-fd7e3a33f434">
<parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
<datasetParameter name="addrId">
<datasetParameterExpression><![CDATA[$P{addrId}]]></datasetParameterExpression>
</datasetParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>
<jr:listContents height="30" width="100">
<textField isStretchWithOverflow="true">
<reportElement x="0" y="0" width="40" height="10" uuid="1b533c30-7868-450b-a5b9-59d5130dcb67"/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</title>
<columnHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="185" height="30" uuid="100faa3b-790d-4dc6-b86c-8911a8762207"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="185" height="30" uuid="aef6af65-f7b5-42e9-a102-aeb272f99103"/>
<text><![CDATA[ADDRESSID]]></text>
</staticText>
<staticText>
<reportElement x="370" y="0" width="185" height="30" uuid="2c176a21-6387-4505-836e-7e250751755f"/>
<text><![CDATA[TOTAL]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="185" height="30" uuid="22cbe96d-5322-40e3-bd96-d2aa9bf35dd2"/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="0" width="185" height="30" uuid="3adedcc9-f60e-4664-bbe8-6b7d7b8e13a4"/>
<textFieldExpression><![CDATA[$F{ADDRESSID}]]></textFieldExpression>
</textField>
<textField pattern="#,##0.00#">
<reportElement x="370" y="0" width="185" height="30" uuid="48041fd6-1375-4819-8ebb-ffd4aef84889"/>
<textFieldExpression><![CDATA[$F{TOTAL}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The output result
My report is showing some very odd spacing issues. It had values that were being repeated, muliple times, but i fixed that by unselecting the Print Repeated Values check box. This fixed the repeated values from displaying. Afterwards I noticed that there were some spaces that were showing up after unselecting that so I then selected the Remove Line When Blank check box. That should have fixed the spacing but for some odd reason I am still seeing some very odd spacing issues in the Preview of my report. Here are some pictures showing what I have. Does anyone have any idea why it would still be doing this; even after I have done the correct options for spacing/repeating values?
Spacing Issue:
Shows the Checked Property boxes for all 'Text Fields'
<?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="BOM Build Time" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="775a7e35-9af8-4206-a155-b05a478c35b0">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="bomNumber" class="java.lang.String"/>
<parameter name="From" class="java.util.Date"/>
<parameter name="To" class="java.util.Date"/>
<queryString>
<![CDATA[SELECT
datediff(day,MO."DATECREATED",MO."DATECOMPLETED") AS dayToBuild,
COMPANY."NAME" AS COMPANY_NAME,
BOM."NUM" AS BOM_NUM,
MO."DATECREATED" AS MO_DATECREATED,
MO."NUM" AS MO_NUM,
MO."DATECOMPLETED" AS MO_DATECOMPLETED,
MOITEM."BOMID" AS MOITEM_BOMID,
MOITEM."MOID" AS MOITEM_MOID,
MOITEM."QTYTOFULFILL" AS MOITEM_QTYTOFULFILL,
BOMITEMTYPE."ID" AS BOMITEMTYPE_ID
FROM
"BOM" BOM INNER JOIN "MOITEM" MOITEM ON BOM."ID" = MOITEM."BOMID"
INNER JOIN "MO" MO ON MOITEM."MOID" = MO."ID"
INNER JOIN "BOMITEMTYPE" BOMITEMTYPE ON MOITEM."TYPEID" = BOMITEMTYPE."ID",
"COMPANY" COMPANY
WHERE
BOM."NUM"=$P{bomNumber}
AND MO."DATECOMPLETED" BETWEEN $P{To} AND $P{From}
ORDER BY
3 ASC]]>
</queryString>
<field name="DAYTOBUILD" class="java.lang.Long"/>
<field name="COMPANY_NAME" class="java.lang.String"/>
<field name="BOM_NUM" class="java.lang.String"/>
<field name="MO_DATECREATED" class="java.sql.Timestamp"/>
<field name="MO_NUM" class="java.lang.String"/>
<field name="MO_DATECOMPLETED" class="java.sql.Timestamp"/>
<field name="MOITEM_BOMID" class="java.lang.Integer"/>
<field name="MOITEM_MOID" class="java.lang.Integer"/>
<field name="MOITEM_QTYTOFULFILL" class="java.lang.Double"/>
<field name="BOMITEMTYPE_ID" class="java.lang.Integer"/>
<variable name="timeToBuild" class="java.lang.String">
<variableExpression><![CDATA[($F{MO_DATECOMPLETED}.getTime()- $F{MO_DATECREATED}.getTime()) / (24* 60 * 60 * 1000) + " days " +
(($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 60 * 1000)) % 24 + " hour(s), " +
(($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 1000)) % 60 + " minute(s)"]]></variableExpression>
</variable>
<variable name="avgTimeToBuild" class="java.lang.String" incrementType="Report" calculation="Count">
<variableExpression><![CDATA[$V{timeToBuild}]]></variableExpression>
</variable>
<title>
<band height="33" splitType="Stretch"/>
</title>
<columnHeader>
<band height="32" splitType="Stretch">
<staticText>
<reportElement x="0" y="1" width="100" height="20" uuid="b59f6065-7fc9-482d-9e08-e381ac697304"/>
<text><![CDATA[BOM_NUM]]></text>
</staticText>
<staticText>
<reportElement x="108" y="0" width="100" height="20" uuid="4ec40990-bdfe-415d-a7ae-b50e315d00ef"/>
<text><![CDATA[MO_DATECREATED]]></text>
</staticText>
<staticText>
<reportElement x="218" y="0" width="100" height="20" uuid="032d2ae9-99fe-4c6f-8cf4-24b9c5367a44"/>
<text><![CDATA[MO_DATECOMPLETED]]></text>
</staticText>
<staticText>
<reportElement x="329" y="0" width="150" height="20" uuid="55b731d5-b826-4ee1-b7d2-4a83cabd4ef8"/>
<text><![CDATA[Time To Build]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="24" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="100" height="20" isRemoveLineWhenBlank="true" uuid="3b256693-f18d-4ef9-89ec-7890481d1855"/>
<textFieldExpression><![CDATA[$F{BOM_NUM}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement isPrintRepeatedValues="false" x="108" y="0" width="100" height="20" isRemoveLineWhenBlank="true" uuid="7c01356e-4c95-4e15-9691-632e0b84946e"/>
<textFieldExpression><![CDATA[$F{MO_DATECREATED}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement isPrintRepeatedValues="false" x="218" y="0" width="100" height="20" isRemoveLineWhenBlank="true" uuid="516bc38e-99f3-486f-ae74-c8cfe6a5b5b1"/>
<textFieldExpression><![CDATA[$F{MO_DATECOMPLETED}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement isPrintRepeatedValues="false" x="329" y="0" width="150" height="20" isRemoveLineWhenBlank="true" uuid="aa538d4a-48ec-4b72-84ed-e9e889dcaee5"/>
<textElement>
<paragraph tabStopWidth="40"/>
</textElement>
<textFieldExpression><![CDATA[$V{timeToBuild}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement isPrintRepeatedValues="false" x="479" y="0" width="76" height="20" isRemoveLineWhenBlank="true" uuid="cf252514-6297-405d-a12e-6424332e6d10"/>
<textFieldExpression><![CDATA[$F{MOITEM_QTYTOFULFILL}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="22">
<staticText>
<reportElement x="0" y="0" width="125" height="20" uuid="5ac0e1eb-dc7b-4866-88aa-3c1a9c3ef4a9"/>
<text><![CDATA[Average Time To Build:]]></text>
</staticText>
<textField>
<reportElement x="125" y="0" width="150" height="20" uuid="860dd902-9791-443e-9a9d-3dedab365b23"/>
<textFieldExpression><![CDATA[$V{avgTimeToBuild}]]></textFieldExpression>
</textField>
</band>
</summary>
There is a situation for which i have divided my page into two columns. I want pageNumber to be incremented and starting from 1. For an example first page should have 1,2 and second page 3, 4 and so on. Please tell me how i can manipulate the pagenumber field.
You can create new variable for counting number of columns.
The variable definition can be like this:
Name: variable
Increment Type: Column
Calculation: Count
Reset type: Report
Expression: $V{variable} + 1
Initial value expression: 0
The sample:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport .. columnCount="2" .. isFloatColumnFooter="true">
<queryString>
<![CDATA[SELECT id, city, street FROM address]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="CITY" class="java.lang.String"/>
<field name="STREET" class="java.lang.String"/>
<variable name="columnsCount" class="java.lang.Integer" incrementType="Column" calculation="Count">
<variableExpression><![CDATA[$V{columnsCount} + 1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<detail>
<band height="63" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="48" height="63"/>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="48" y="0" width="100" height="63"/>
<textElement/>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="148" y="0" width="128" height="63"/>
<textElement/>
<textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="50">
<textField>
<reportElement x="156" y="30" width="80" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{columnsCount}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="236" y="30" width="40" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[" " + $V{columnsCount}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
</jasperReport>
The result will be:
I have a variable datatype set up in the group footer band that generates a subtotal for the counts in each group in my report. Works great.
I would like a grand total to generate on the last page of my report, simply summing up the subtotal values. This has been harder to figure out.
Any suggestions?
You can use two variables with different resetType - for calculating sum in group (with resetType="Group" resetGroup="groupName" calculation="Sum" properties) and for calculating total sum for whole report (with resetType="Report" calculation="Sum" properties).
The sample (jrxml file):
<?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="grand_total_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[SELECT DOCUMENTID, POSITIONNO, PRODUCTID, QUANTITY FROM POSITIONS WHERE PRODUCTID < 10 AND POSITIONNO > 18 ORDER BY POSITIONNO]]>
</queryString>
<field name="DOCUMENTID" class="java.lang.Integer"/>
<field name="POSITIONNO" class="java.lang.Integer"/>
<field name="PRODUCTID" class="java.lang.Integer"/>
<field name="QUANTITY" class="java.lang.Integer"/>
<variable name="quantitySumInGroup" class="java.lang.Integer" resetType="Group" resetGroup="positionNoGroup" calculation="Sum">
<variableExpression><![CDATA[$F{QUANTITY}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<variable name="quantityTotalSum" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{QUANTITY}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<group name="positionNoGroup">
<groupExpression><![CDATA[$F{POSITIONNO}]]></groupExpression>
<groupHeader>
<band height="20">
<textField>
<reportElement x="0" y="0" width="400" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<textFieldExpression><![CDATA["Position no: " + $F{POSITIONNO}]]></textFieldExpression>
</textField>
</band>
</groupHeader>
<groupFooter>
<band height="20">
<textField>
<reportElement x="0" y="0" width="400" height="20"/>
<textElement>
<font isBold="false" isItalic="true" isUnderline="false"/>
</textElement>
<textFieldExpression><![CDATA["Sum in group: " + $V{quantitySumInGroup}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{PRODUCTID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{QUANTITY}]]></textFieldExpression>
</textField>
</band>
</detail>
<lastPageFooter>
<band height="20">
<textField>
<reportElement x="0" y="0" width="400" height="20"/>
<textElement>
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<textFieldExpression><![CDATA["Total sum: " + $V{quantityTotalSum}]]></textFieldExpression>
</textField>
</band>
</lastPageFooter>
</jasperReport>
The result will be:
Just create a new variable and in the variable expression add up all of the subtotals using their variable name
for instance:
$V{subtotal1}+$V{subtotal2}+$V{subtotal3}
After that, throw it in the 'summary' band and in the report properties check the box that says "summary on new page". That way it'll be on the last page of your report.