Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

jasper reports - How to show the number of Group?

I created a report to generate user which has a list of departments. I need to count the group but not the records within the group.

How to do it with JasperReports?

For example: -

**No: 1**
Name      : User1
Department: Depart1
            Depart2
            Depart3

**No: 2**
Name      : User2
Department: Depart1
            Depart2
            Depart3

**No: 3**
Name      : User3
Department: Depart1
            Depart2
            Depart3

In the above example, I need to count the count them 1, 2, 3 for 3 users. I don't want the numbering to count the records within the user, means I do not need the count to count the numbers of department for each user.

Currently, I create a group for the User and there is a variable automatically created "User_COUNT". I use that in my detail band but it seems counting each records in the user group.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the variable on your Group.

The sample:

<?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="count_groups" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7ca8f323-3cc7-4980-9222-77fb0f8f556b">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="Department" class="java.lang.String"/>
    <variable name="cntUser" class="java.lang.Integer" incrementType="Group" incrementGroup="userGroup">
        <variableExpression><![CDATA[($V{userGroup_COUNT} == 1) ? $V{cntUser} + 1 : $V{cntUser}]]></variableExpression>
        <initialValueExpression><![CDATA[1]]></initialValueExpression>
    </variable>
    <group name="userGroup">
        <groupExpression><![CDATA[$F{Name}]]></groupExpression>
        <groupHeader>
            <band height="50">
                <textField>
                    <reportElement uuid="da974bc0-323d-4169-b584-eddb4ffcfa50" x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA["No: " + $V{cntUser}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement uuid="57fcf335-26f2-44b4-89ad-11c1223c9539" x="0" y="30" width="100" height="20"/>
                    <textElement markup="none"/>
                    <text><![CDATA[Name         :]]></text>
                </staticText>
                <textField>
                    <reportElement uuid="16ccb7f0-eb39-403e-b7c4-1c6f35989f3d" x="100" y="30" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="4647a9aa-229e-4f5d-8d08-aca4cda1df2f" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Department}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="bc4bd9ae-6c2f-43c2-823e-1501b76ef39a" x="0" y="0" width="100" height="20">
                    <printWhenExpression><![CDATA[$V{userGroup_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textElement/>
                <text><![CDATA[Department:]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

The report design in iReport is looks like this:

Report's design in iReport

For this input data (I've used CSV datasource in sample):

Name,Department
User1,Depart1
User1,Depart2
User1,Depart3
User2,Depart2
User2,Depart3
User3,Depart1
User3,Depart4
User3,Depart4
User3,Depart4

The result will be (via preview in iReport):

The result in iReport

Details:
In this sample I've used variable cntUser with Increment Type equals to Group (for group named userGroup). This variable incremented (see the variableExpression) only for the first record in each Group (the check $V{userGroup_COUNT} == 1).

Note: Don't forget to sort data if you are using the Grouping


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...