Search code examples
jasper-reports

Weird positioning of elements inside a Jasper Report preview


I'm using TIBCO Jaspersoft® Studio 6.8.0 and I'm having some issues positioning things. I thought that the positioning would be kinda automatic following the GUI, but apparently it doesn't work like that.

I have a SubReport (no need for the master report right now) that calls other 2 SubReports, one for a pie chart and one for a crosstab. They both working pretty fine using subdataset from an external json (local json).
The problem is that I can't position them properly: I would need the pie chart to be positioned in the top part of the page, and the crosstab immediatly below.

Here's the subreport (used as master report now). You can see I'm using a mock (xml that calls a json), I will add the json at the end of the question.

<?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="ReportAnomalies_subReport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0a8e5b1c-8d35-4290-a283-c7fb4e197131">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="report\mock\TestTemp.xml"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <style name="DefaultStyle" isDefault="true" fontName="SinaFont" fontSize="8"/>
    <style name="DefaultTableStyle" style="DefaultStyle">
        <box>
            <pen lineWidth="0.5" lineColor="#888888"/>
        </box>
    </style>
    <style name="DefaultTableHeaderStyle" style="DefaultTableStyle" mode="Opaque" backcolor="#F0F0F0"/>
    <style name="Table_H" style="DefaultTableHeaderStyle"/>
    <style name="Table_T" style="DefaultTableStyle"/>
    <style name="Table_D" style="DefaultTableStyle"/>
    <field name="NomeAnomalia" class="java.lang.String"/>
    <field name="GruppiAnomalie" class="java.lang.Integer"/>
    <field name="Total" class="java.lang.String"/>
    <detail>
        <band height="290">
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <subreport>
                <reportElement key="" x="10" y="0" width="535" height="290" uuid="37faa740-d0e0-457e-bb66-a9dbfc252683"/>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("pieChartData")]]></dataSourceExpression>
                <subreportExpression><![CDATA["ReportAnomalies_pieChart.jasper"]]></subreportExpression>
            </subreport>
        </band>
        <band height="80">
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <subreport>
                <reportElement key="" style="DefaultTableStyle" x="10" y="0" width="535" height="80" uuid="ca3470c0-044b-4905-9524-5c0a948961a5">
                    <property name="com.jaspersoft.studio.unit.x" value="px"/>
                    <property name="com.jaspersoft.studio.unit.y" value="px"/>
                </reportElement>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("anomalyList")]]></dataSourceExpression>
                <subreportExpression><![CDATA["ReportAnomalies_crosstab.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

The pie chart subreport (I use the workaround <printWhenExpression><![CDATA[$F{print} == 1]]></printWhenExpression> because it will print the pie chart 3 times if not used)

<?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="ReportAnomalies_pieChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0a8e5b1c-8d35-4290-a283-c7fb4e197131">
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <style name="DefaultStyle" isDefault="true" fontName="SinaFont" fontSize="8"/>
    <style name="DefaultTableStyle" style="DefaultStyle">
        <box>
            <pen lineWidth="0.5" lineColor="#888888"/>
        </box>
    </style>
    <style name="DefaultTableHeaderStyle" style="DefaultTableStyle" mode="Opaque" backcolor="#F0F0F0"/>
    <style name="Table_H" style="DefaultTableHeaderStyle"/>
    <style name="Table_T" style="DefaultTableStyle"/>
    <style name="Table_D" style="DefaultTableStyle"/>
    <field name="print" class="java.lang.Integer"/>
    <field name="severityName" class="java.lang.String"/>
    <field name="severityValue" class="java.lang.Integer"/>
    <field name="severityLabel" class="java.lang.String"/>
    <detail>
        <band height="285" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <pieChart>
                <chart evaluationTime="Report">
                    <reportElement style="DefaultTableStyle" x="40" y="0" width="420" height="280" uuid="65c16b4b-d856-4d7f-bb7b-a0c0aa0f3a3a">
                        <property name="com.jaspersoft.studio.unit.height" value="px"/>
                        <printWhenExpression><![CDATA[$F{print} == 1]]></printWhenExpression>
                    </reportElement>
                    <box>
                        <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                        <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                        <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                        <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    </box>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <pieDataset maxCount="3">
                    <keyExpression><![CDATA[$F{severityName}]]></keyExpression>
                    <valueExpression><![CDATA[$F{severityValue}]]></valueExpression>
                    <labelExpression><![CDATA[$F{severityLabel}]]></labelExpression>
                </pieDataset>
                <piePlot>
                    <plot>
                        <seriesColor seriesOrder="0" color="#80E31E"/>
                        <seriesColor seriesOrder="1" color="#F09918"/>
                        <seriesColor seriesOrder="2" color="#DE3700"/>
                    </plot>
                    <itemLabel/>
                </piePlot>
            </pieChart>
        </band>
    </detail>
</jasperReport>

Now the crosstab subreport

<?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="Prova" pageWidth="595" pageHeight="842" orientation="Landscape" whenNoDataType="NoPages" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isSummaryNewPage="true" isSummaryWithPageHeaderAndFooter="true" uuid="1916d28f-c2ed-4735-a541-6dfbd82fe676">
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <style name="DefaultStyle" isDefault="true" fontName="SinaFont" fontSize="8"/>
    <style name="DefaultTableStyle" style="DefaultStyle">
        <box>
            <pen lineWidth="0.5" lineColor="#888888"/>
        </box>
    </style>
    <style name="DefaultTableHeaderStyle" style="DefaultTableStyle" mode="Opaque" backcolor="#F0F0F0"/>
    <style name="Table_H" style="DefaultTableHeaderStyle"/>
    <style name="Table_T" style="DefaultTableStyle"/>
    <style name="Table_D" style="DefaultTableStyle"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="NomeAnomalia" class="java.lang.String"/>
    <field name="Value" class="java.lang.String"/>
    <field name="Code" class="java.lang.String"/>
    <field name="Severity" class="java.lang.String"/>
    <field name="GruppiAnomalie" class="java.lang.String"/>
    <variable name="VI" class="java.lang.Float">
        <variableExpression><![CDATA[Float.parseFloat($F{Value})]]></variableExpression>
    </variable>
    <variable name="GruppiAnomalieI" class="java.lang.Integer">
        <variableExpression><![CDATA[Integer.parseInt($F{GruppiAnomalie})]]></variableExpression>
    </variable>
    <group name="Group1">
        <groupExpression><![CDATA[$V{GruppiAnomalieI}]]></groupExpression>
        <groupFooter>
            <band height="80">
                <crosstab columnBreakOffset="20">
                    <reportElement x="30" y="0" width="520" height="70" uuid="af9d4537-350c-4e0b-a338-009d92c884d5">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                        <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
                    </reportElement>
                    <box leftPadding="3"/>
                    <crosstabDataset>
                        <dataset resetType="Group" resetGroup="Group1"/>
                    </crosstabDataset>
                    <rowGroup name="Severity1" width="30">
                        <bucket class="java.lang.String">
                            <bucketExpression><![CDATA[$F{Severity}]]></bucketExpression>
                        </bucket>
                        <crosstabRowHeader>
                            <cellContents mode="Opaque" style="Table_H">
                                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                                <textField>
                                    <reportElement x="0" y="0" width="30" height="20" uuid="683211f0-a5a5-4cb4-aadc-658e5185baca">
                                        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                                    </reportElement>
                                    <textElement textAlignment="Center" verticalAlignment="Middle">
                                        <font size="8"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[$V{Severity1}.substring(4)]]></textFieldExpression>
                                </textField>
                            </cellContents>
                        </crosstabRowHeader>
                        <crosstabTotalRowHeader>
                            <cellContents mode="Opaque" style="Table_T">
                                <staticText>
                                    <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="b709de74-24d7-42d1-975c-3ff583a2f091"/>
                                    <text><![CDATA[Totale Severity1]]></text>
                                </staticText>
                            </cellContents>
                        </crosstabTotalRowHeader>
                    </rowGroup>
                    <columnGroup name="Code1" height="20">
                        <bucket class="java.lang.String">
                            <bucketExpression><![CDATA[$F{Code}]]></bucketExpression>
                        </bucket>
                        <crosstabColumnHeader>
                            <cellContents mode="Opaque" style="Table_H">
                                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                                <textField>
                                    <reportElement x="0" y="0" width="25" height="20" uuid="8e897853-1771-4c8e-af55-24e5d5e3708a"/>
                                    <textElement textAlignment="Center" verticalAlignment="Middle">
                                        <font size="7"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[$V{Code1}]]></textFieldExpression>
                                </textField>
                            </cellContents>
                        </crosstabColumnHeader>
                        <crosstabTotalColumnHeader>
                            <cellContents mode="Opaque" style="Table_T">
                                <staticText>
                                    <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="0423ca98-83a4-48b4-828b-ecc91f72c6df"/>
                                    <text><![CDATA[Totale Code1]]></text>
                                </staticText>
                            </cellContents>
                        </crosstabTotalColumnHeader>
                    </columnGroup>
                    <measure name="Value_MEASURE1" class="java.lang.Float" calculation="Sum">
                        <measureExpression><![CDATA[$V{VI}]]></measureExpression>
                    </measure>
                    <measure name="NA" class="java.lang.String">
                        <measureExpression><![CDATA["n/a"]]></measureExpression>
                    </measure>
                    <crosstabCell width="25" height="20">
                        <cellContents mode="Opaque" style="Table_D">
                            <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                            <textField pattern="#,##0.##">
                                <reportElement x="0" y="0" width="25" height="20" uuid="8f7c6a83-bec1-4fdd-995b-c15df0712202"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font size="7"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$V{Value_MEASURE1} == -1 ? $V{NA} : $V{Value_MEASURE1}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                </crosstab>
            </band>
        </groupFooter>
    </group>
</jasperReport>

And finally the json file, very simple

[   
    {
        "NomeAnomalia": "MIT Impalcati,Travi,Traversi CA CAP",
        "GruppiAnomalie": 201,
        "Total": "6%",
        "anomalyList": [
            {
                "GruppiAnomalie": 201,
                "Code": "cacap1",
                "Name": "Macchie di umidita passiva",
                "Severity": "0 - Lieve",
                "Value": 3.2736806021763885
            },
            {
                "GruppiAnomalie": 201,
                "Code": "cacap12",
                "Name": "Macchie di umidita passiva",
                "Severity": "0 - Lieve",
                "Value": 1.2736806021763885
            },
            {
                "GruppiAnomalie": 201,
                "Code": "cacap16",
                "Name": "Macchie di umidita passiva",
                "Severity": "2 - Forte",
                "Value": 0.7736806021763885
            },
            {
                "GruppiAnomalie": 201,
                "Code": "DifGen1",
                "Name": "Macchie di umidita passiva",
                "Severity": "1 - Media",
                "Value": 1.571812154
            }
        ],
        "pieChartData": [
            {"print": 1, "severityName": "Lieve", "severityValue": 25, "severityLabel": "25%"},
            {"print": 0, "severityName": "Media", "severityValue": 15, "severityLabel": "15%"},
            {"print": 0, "severityName": "Forte", "severityValue": 38, "severityLabel": "38%"}
        ]
    },
    {
        "NomeAnomalia": "MIT Pile,Spalle,Fondazioni CLS",
        "GruppiAnomalie": 202,
        "Total": "6%",
        "anomalyList": [
            {
                "GruppiAnomalie": 202,
                "Code": "cacap1",
                "Name": "Macchie di umidita passiva",
                "Severity": "0 - Lieve",
                "Value": 3.2736806021763885
            },
            {
                "GruppiAnomalie": 202,
                "Code": "cacap12",
                "Name": "Macchie di umidita passiva",
                "Severity": "0 - Lieve",
                "Value": 1.2736806021763885
            },
            {
                "GruppiAnomalie": 202,
                "Code": "cacap16",
                "Name": "Macchie di umidita passiva",
                "Severity": "2 - Forte",
                "Value": 0.7736806021763885
            }
        ],
        "pieChartData": [
            {"print": 1, "severityName": "Lieve", "severityValue": 25, "severityLabel": "5%"},
            {"print": 0, "severityName": "Media", "severityValue": 15, "severityLabel": "87%"},
            {"print": 0, "severityName": "Forte", "severityValue": 38, "severityLabel": "47%"}
        ]
    }
]

I don't think the problem is to be found in subreports...at least not in the operational part, but I still don't know why it appears like this (3 pages instead of 2 and weird positioning)...they are in order, first, second and third page

enter image description here

enter image description here

enter image description here

Any suggestion?


Solution

  • The workaround you use in the pie chart subreport is causing the empty space in the generated report.

    You set the print when expression for the pie chart element. That makes the pie chart not print for the second and third records, but the detail band itself will still consume its declared height even if the chart doesn't print.

    A simple way to fix this is to set the print when expression for the band instead of the chart element. That will make the whole band not printing for the second and third records.

    But a more normal solution would be to place the chart element in the summary band (with no print when expression), and remove the detail band altogether.