【问题标题】:How to organize Frames in Jasper Report so they float flawlessly?如何在 Jasper Report 中组织框架,使其完美浮动?
【发布时间】:2018-10-06 01:37:39
【问题描述】:

在 Jasper 报告中,我有 4 个框架(下图),它们的一些属性设置如下图所示。

现在的想法是,给定某些参数 showBlueshowRedBLUERED 分别显示或隐藏,并且后续帧在前一个帧之后“浮动”(考虑以下顺序:BLUE RED GREEN) 而BLACK 应该留在同一个地方。

左边的两个框架(REDGREEN)在 BLACK 一个就位之前完美地浮起。之后,当我将参数showBlueshowRed 设置为false(隐藏BLUERED 帧)时,结果如下:

这是报告设计的jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1  -->
<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="Float_UP" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6578bc34-0c2e-4179-99da-5ec1dd90a422">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="showBlue" class="java.lang.Boolean"/>
    <parameter name="showRed" class="java.lang.Boolean"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <detail>
        <band height="211" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="400" height="12" uuid="54cb1704-59d8-4272-9ec1-db4cea913cd3"/>
                <text><![CDATA[Header 1 (right before conditional frame)]]></text>
            </staticText>
            <frame>
                <reportElement x="0" y="15" width="400" height="66" isRemoveLineWhenBlank="true" uuid="0140ba9b-f2f0-494c-82bc-caf6b5efc63e">
                    <printWhenExpression><![CDATA[$P{showBlue}]]></printWhenExpression>
                </reportElement>
                <box>
                    <pen lineWidth="3.0" lineColor="#2E0DD4"/>
                </box>
                <staticText>
                    <reportElement x="1" y="1" width="379" height="59" uuid="a8b7d505-a6ad-4359-9263-23ac087b19ff"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <text><![CDATA[BLUE: `isRemoveLineWhenBlank=true` and `printWhenExpression` set]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement positionType="Float" x="0" y="85" width="400" height="60" isRemoveLineWhenBlank="true" uuid="c80dd879-ce81-4921-b17e-9882763a3f61">
                    <printWhenExpression><![CDATA[$P{showRed}]]></printWhenExpression>
                </reportElement>
                <box>
                    <pen lineWidth="3.0" lineColor="#F50A25"/>
                </box>
                <staticText>
                    <reportElement x="0" y="0" width="380" height="50" uuid="d0cadbd4-b436-47f4-a32d-2b00f0c6b147"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <text><![CDATA[RED: `isRemoveLineWhenBlank=true`, `printWhenExpression` set and `positionType="Float"`]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement positionType="Float" x="2" y="151" width="398" height="60" uuid="e0a5ed13-d8f2-4acd-ac14-1f5633099542"/>
                <box>
                    <pen lineWidth="3.0" lineColor="#22B002"/>
                </box>
                <staticText>
                    <reportElement x="1" y="1" width="377" height="39" uuid="482471b4-4c3b-42c6-892d-f8c42ca320bf"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <text><![CDATA[GREEN: `positionType="Float"`]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement x="420" y="15" width="130" height="70" uuid="8e057d80-72be-4f66-ae9b-ef80610daf36"/>
                <box>
                    <pen lineWidth="3.0"/>
                </box>
                <staticText>
                    <reportElement x="6" y="6" width="118" height="44" uuid="36d8e560-3af5-4edf-b9ef-9b9311877c3a"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <text><![CDATA[BLACK]]></text>
                </staticText>
            </frame>
        </band>
    </detail>
</jasperReport>

我已经用 Swagger 设置了一个Java Project,以防你想用 maven 运行它。

问:如何使帧REDGREEN 在其前一帧之后正确“上浮”而帧BLACK 保持在同一个位置?

【问题讨论】:

    标签: jasper-reports


    【解决方案1】:

    是的,绿色不会漂浮,因为黑盒子就放在它的正上方

    来自JRElement API

    Float - 如果元素被位于其上方的其他元素向下推动,则该元素会在其父部分中浮动。它试图保持它与紧邻其上方

    的相邻元素之间的距离

    解决方案

    解决方案显然是在其上方没有任何元素,实现此目的的一种方法是使用另一个框架或子报表。如果您还需要在此设计下浮动元素,您可能需要一个子报表,但我将展示父框架解决方案,因为它是解决您的直接问题的最简单方法。

    使用父框架的示例

    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="Float_UP" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6578bc34-0c2e-4179-99da-5ec1dd90a422">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <parameter name="showBlue" class="java.lang.Boolean"/>
        <parameter name="showRed" class="java.lang.Boolean"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <detail>
            <band height="96" splitType="Stretch">
                <staticText>
                    <reportElement x="0" y="0" width="400" height="12" uuid="54cb1704-59d8-4272-9ec1-db4cea913cd3"/>
                    <text><![CDATA[Header 1 (right before conditional frame)]]></text>
                </staticText>
                <frame>
                    <reportElement x="0" y="15" width="409" height="78" uuid="82076f1d-b7a9-4b63-ab4a-af54b0817a62"/>
                    <frame>
                        <reportElement x="0" y="4" width="400" height="66" isRemoveLineWhenBlank="true" uuid="0140ba9b-f2f0-494c-82bc-caf6b5efc63e">
                            <printWhenExpression><![CDATA[$P{showBlue}]]></printWhenExpression>
                        </reportElement>
                        <box>
                            <pen lineWidth="3.0" lineColor="#2E0DD4"/>
                        </box>
                        <staticText>
                            <reportElement x="1" y="1" width="379" height="59" uuid="a8b7d505-a6ad-4359-9263-23ac087b19ff"/>
                            <textElement>
                                <font size="14"/>
                            </textElement>
                            <text><![CDATA[BLUE: `isRemoveLineWhenBlank=true` and `printWhenExpression` set]]></text>
                        </staticText>
                    </frame>
                    <frame>
                        <reportElement positionType="Float" x="-1" y="75" width="400" height="60" isRemoveLineWhenBlank="true" uuid="c80dd879-ce81-4921-b17e-9882763a3f61">
                            <printWhenExpression><![CDATA[$P{showRed}]]></printWhenExpression>
                        </reportElement>
                        <box>
                            <pen lineWidth="3.0" lineColor="#F50A25"/>
                        </box>
                        <staticText>
                            <reportElement x="0" y="0" width="380" height="50" uuid="d0cadbd4-b436-47f4-a32d-2b00f0c6b147"/>
                            <textElement>
                                <font size="14"/>
                            </textElement>
                            <text><![CDATA[RED: `isRemoveLineWhenBlank=true`, `printWhenExpression` set and `positionType="Float"`]]></text>
                        </staticText>
                    </frame>
                    <frame>
                        <reportElement positionType="Float" x="0" y="140" width="398" height="60" uuid="e0a5ed13-d8f2-4acd-ac14-1f5633099542"/>
                        <box>
                            <pen lineWidth="3.0" lineColor="#22B002"/>
                        </box>
                        <staticText>
                            <reportElement x="1" y="1" width="377" height="39" uuid="482471b4-4c3b-42c6-892d-f8c42ca320bf"/>
                            <textElement>
                                <font size="14"/>
                            </textElement>
                            <text><![CDATA[GREEN: `positionType="Float"`]]></text>
                        </staticText>
                    </frame>
                </frame>
                <frame>
                    <reportElement x="420" y="15" width="130" height="70" uuid="8e057d80-72be-4f66-ae9b-ef80610daf36"/>
                    <box>
                        <pen lineWidth="3.0"/>
                    </box>
                    <staticText>
                        <reportElement x="6" y="6" width="118" height="44" uuid="36d8e560-3af5-4edf-b9ef-9b9311877c3a"/>
                        <textElement>
                            <font size="14"/>
                        </textElement>
                        <text><![CDATA[BLACK]]></text>
                    </staticText>
                </frame>
            </band>
        </detail>
    </jasperReport>
    

    输出

    其他设计说明

    正如您在输出中看到的那样,绿色和黑色框之间有 4px 的高度差,这是由于帧之间的空白空间所致。如果您需要像素完美报告,您需要在要删除/浮动的组件之间留有零空间

    【讨论】:

      【解决方案2】:

      也许您可以尝试为您的报告使用多个 Detail 波段。因此,您可以在每个波段上使用属性printWhenExpresion,而不是在每个帧中。

      Here你可以通过示例找到你的jrxml代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多