Jasper, 'print when' create white space,

2019-08-17 20:55发布

问题:

I've created a simple JasperReport example, using the sample DB:

StackOverflowExample.jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.8.0.final using JasperReports Library version 6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b  -->
<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="StackOverflowExample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="749cd048-9a0d-4a00-89bf-1a75a4565943">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <queryString>
        <![CDATA[SELECT DISTINCT "ORDERS"."SHIPCOUNTRY"
FROM "ORDERS"]]>
    </queryString>
    <field name="SHIPCOUNTRY" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="SHIPCOUNTRY"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ORDERS"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="183">
            <subreport>
                <reportElement x="0" y="0" width="556" height="170" isRemoveLineWhenBlank="true" uuid="4b89b974-f838-4bb7-85b6-1b0f1079c1e6"/>
                <subreportParameter name="country">
                    <subreportParameterExpression><![CDATA[$F{SHIPCOUNTRY}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["StackOverflowExampleSubReport.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

StackOverflowExampleSubReport.jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.8.0.final using JasperReports Library version 6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b  -->
<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="StackOverflowExample" columnCount="2" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="749cd048-9a0d-4a00-89bf-1a75a4565943">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <parameter name="country" class="java.lang.String">
        <defaultValueExpression><![CDATA["France"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT "ORDERS"."ORDERDATE",
    "ORDERS"."FREIGHT"
FROM "ORDERS"
WHERE  "ORDERS"."SHIPCOUNTRY" = $P{country}]]>
    </queryString>
    <field name="ORDERDATE" class="java.sql.Timestamp">
        <property name="com.jaspersoft.studio.field.label" value="ORDERDATE"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ORDERS"/>
    </field>
    <field name="FREIGHT" class="java.math.BigDecimal">
        <property name="com.jaspersoft.studio.field.label" value="FREIGHT"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ORDERS"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="160">
            <lineChart>
                <chart evaluationTime="Report">
                    <reportElement x="15" y="5" width="255" height="155" isRemoveLineWhenBlank="true" uuid="3b5f7d89-490b-468f-9112-f606f4eda437">
                        <printWhenExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()==1)]]></printWhenExpression>
                    </reportElement>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA[$P{country}]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{ORDERDATE}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{FREIGHT}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <linePlot>
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </valueAxisFormat>
                </linePlot>
            </lineChart>
        </band>
    </detail>
</jasperReport>

The problem is that I have a lot of empty useless space


Subreport
An image of the subreport, with the parameter country set to "France", it contains the first page as below and 7 other empty pages
https://community.jaspersoft.com/sites/default/files/user_uploads/ho.hince/capture.png

MainReport
For the main report, the first two page as below, there is a total of 93 pages, most of them which are empty:
https://community.jaspersoft.com/sites/default/files/user_uploads/ho.hince/capture_0.png
https://community.jaspersoft.com/sites/default/files/user_uploads/ho.hince/capture_1.png


The reason for the white space is that I print the chart in the subreport report once. I do it with the property printWhen.
printWhen => new Boolean($V{REPORT_COUNT}.intValue()==1)

I can't move the chart out of the Detail band, because I've set the subreport properties with Column Count = 2 and Print Order = Horizontal. I did that, cause I wish to have the charts in two column just like that:
https://community.jaspersoft.com/sites/default/files/user_uploads/ho.hince/capture_2.png
(The problem here is that the chart are duplicated)

回答1:

I found the solution, I was printing the chart only once, but I was printing the Detail band each time, so I had to move the print when expression to the Detail band.

<band height="160">
    <printWhenExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()==1)]]></printWhenExpression>

The main chart must have two column in vertical order, and the subreport 2 column in horizontal order.


EDIT

You can also change the main report properties :
Print Order ==> vertical and Column Count ==> 2


StackOverflowExample.jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.8.0.final using JasperReports Library version 6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b  -->
<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="StackOverflowExample" columnCount="2" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="749cd048-9a0d-4a00-89bf-1a75a4565943">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <queryString>
        <![CDATA[SELECT  "ORDERS"."SHIPCOUNTRY", 
    Max("ORDERS"."SHIPCOUNTRY") 
FROM  "ORDERS"

GROUP BY "ORDERS"."SHIPCOUNTRY" 
ORDER BY "ORDERS"."SHIPCOUNTRY" ASC]]>
    </queryString>
    <field name="SHIPCOUNTRY" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="SHIPCOUNTRY"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ORDERS"/>
    </field>
    <field name="C2" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="C2"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="170">
            <subreport>
                <reportElement x="-20" y="50" width="297" height="120" isRemoveLineWhenBlank="true" uuid="4b89b974-f838-4bb7-85b6-1b0f1079c1e6">
                    <property name="com.jaspersoft.studio.unit.width" value="px"/>
                </reportElement>
                <subreportParameter name="country">
                    <subreportParameterExpression><![CDATA[$F{SHIPCOUNTRY}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["StackOverflowExampleSubReport.jasper"]]></subreportExpression>
            </subreport>
            <textField>
                <reportElement x="43" y="0" width="185" height="50" uuid="0c170024-70ea-492f-95fe-99ff3a27cb6d"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="19"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{SHIPCOUNTRY}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>


StackOverflowExampleSubReport.jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.8.0.final using JasperReports Library version 6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b  -->
<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="StackOverflowExample" columnCount="2" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="749cd048-9a0d-4a00-89bf-1a75a4565943">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <parameter name="country" class="java.lang.String">
        <defaultValueExpression><![CDATA["France"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT "ORDERS"."ORDERDATE",
    "ORDERS"."FREIGHT"
FROM "ORDERS"
WHERE  "ORDERS"."SHIPCOUNTRY" = $P{country}]]>
    </queryString>
    <field name="ORDERDATE" class="java.sql.Timestamp">
        <property name="com.jaspersoft.studio.field.label" value="ORDERDATE"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ORDERS"/>
    </field>
    <field name="FREIGHT" class="java.math.BigDecimal">
        <property name="com.jaspersoft.studio.field.label" value="FREIGHT"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ORDERS"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="130">
            <printWhenExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()==1)]]></printWhenExpression>
            <lineChart>
                <chart evaluationTime="Report">
                    <reportElement isPrintRepeatedValues="false" x="0" y="3" width="235" height="125" isRemoveLineWhenBlank="true" uuid="3b5f7d89-490b-468f-9112-f606f4eda437"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA[$P{country}]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{ORDERDATE}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{FREIGHT}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <linePlot>
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </valueAxisFormat>
                </linePlot>
            </lineChart>
        </band>
    </detail>
</jasperReport>