Chart repeats many times

2019-04-25 11:54发布

问题:

I have generated a report (with JasperReport). My problem that the chart repeats, I have one chart and the report contains 22 pages! What is the problem?

This is my report:

<?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="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="New Dataset 1">
    <parameter name="SQL" class="java.lang.String"/>
    <parameter name="Titre" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[$P!{SQL}]]>
    </queryString>
    <field name="nb" class="java.lang.Long"/>
    <field name="champ1" class="java.lang.String"/>
    <field name="champ2" class="java.lang.String"/>
</subDataset>
<parameter name="SQL" class="java.lang.String">
    <defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<parameter name="Titre" class="java.lang.String"/>
<queryString>
    <![CDATA[select id from jiraissue]]>
</queryString>
<field name="id" class="java.math.BigDecimal"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="46" splitType="Stretch">
        <textField>
            <reportElement x="89" y="15" width="180" height="20"/>
            <textElement/>
            <textFieldExpression class="java.lang.String"><![CDATA[$P{Titre}]]></textFieldExpression>
        </textField>
    </band>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="13" splitType="Stretch"/>
</columnHeader>
<detail>
    <band height="185" splitType="Stretch">
        <stackedBar3DChart>
            <chart>
                <reportElement x="57" y="16" width="420" height="165"/>
                <chartTitle/>
                <chartSubtitle/>
                <chartLegend/>
            </chart>
            <categoryDataset>
                <dataset>
                    <datasetRun subDataset="New Dataset 1">
                        <datasetParameter name="SQL">
                            <datasetParameterExpression><![CDATA[$P{SQL}]]></datasetParameterExpression>
                        </datasetParameter>
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                </dataset>
                <categorySeries>
                    <seriesExpression><![CDATA[$F{champ2}]]></seriesExpression>
                    <categoryExpression><![CDATA[$F{champ1}]]></categoryExpression>
                    <valueExpression><![CDATA[$F{nb}]]></valueExpression>
                </categorySeries>
            </categoryDataset>
            <bar3DPlot>
                <plot/>
                <itemLabel color="#000000" backgroundColor="#FFFFFF"/>
                <categoryAxisFormat>
                    <axisFormat>
                        <labelFont/>
                        <tickLabelFont/>
                    </axisFormat>
                </categoryAxisFormat>
                <valueAxisFormat>
                    <axisFormat>
                        <labelFont/>
                        <tickLabelFont/>
                    </axisFormat>
                </valueAxisFormat>
            </bar3DPlot>
        </stackedBar3DChart>
    </band>
</detail>
<columnFooter>
    <band height="24" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="20" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="22" splitType="Stretch"/>
</summary>

Update I have solved the problem by moving the chart to the summary part :)

回答1:

In Jasper Report, everything you place in the detail band is repeated number of times it appears in the source.

If you want to show anything (not only chart) just once, avoid the detail band. For example, you can place it in the summary section.

Update: As others pointed, I have extended my answer a little bit.



回答2:

I also had face with the same problem like this before. It is because your data will be looped which causes such redundant of data. What you need to do is go to 'Print when expression' in properties and set your variable report count to 1. This is how it looks, $V{REPORT_COUNT}==1. It will display only once in your report.



回答3:

I use $V{PAGE_NUMBER} == 1 as value of 'Print when expression' so it is forced to appear only on first page.