Conditional Horizontal line in jasperreport between results

蹲街弑〆低调 提交于 2019-12-07 16:26:58

问题


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 ?

Result :


回答1:


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):

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



来源:https://stackoverflow.com/questions/16568499/conditional-horizontal-line-in-jasperreport-between-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!