Tarek
Tarek

Reputation: 3798

Conditional Horizontal line in jasperreport between results

I'm using jasperreport 4.7.0

I have a query where I order by name then date .

Now the clients wants that when the name changes that we add an horizontal line (see attached img - red line)

Is there a way to accomplish this without duplicating the query and the fields ?

enter image description here

Result :

enter image description here

Upvotes: 3

Views: 9342

Answers (1)

Alex K
Alex K

Reputation: 22857

For solving your task you can use Data Grouping.

The possible steps (there are a lot of way to reach your target) for adding line are:

  • Create the report Group (via context menu Add Report Group in iReport) on field
  • Add Group Footer Band
  • Add Rectangle element to the Group Footer Band
  • Set Height, Forecolor and Backcolor for this Rectangle - drawing "red pencil line"

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="line_in_group" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f1394ead-7ad6-4371-979d-5a13d1bdde4d">
    <queryString>
        <![CDATA[SELECT id, city, street FROM address ORDER BY city]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="CITY" class="java.lang.String"/>
    <field name="STREET" class="java.lang.String"/>
    <group name="cityGroup">
        <groupExpression><![CDATA[$F{CITY}]]></groupExpression>
        <groupFooter>
            <band height="2">
                <rectangle>
                    <reportElement uuid="6564e743-2a45-4b51-89a5-e3ec6aee291f" x="0" y="0" width="300" height="2" forecolor="#FF0000" backcolor="#FF0000"/>
                </rectangle>
            </band>
        </groupFooter>
    </group>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <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"/>
                </textElement>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <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"/>
                </textElement>
                <text><![CDATA[City]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <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"/>
                </textElement>
                <text><![CDATA[Street]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The result will be (via preview in iReport):

The result in iReport

In this sample I've create a group for the field CITY.

Upvotes: 6

Related Questions