如何通过JasperReports java API在页眉中添加图表?

时间:2015-12-22 07:48:45

标签: java jasper-reports

首先,我创建一个图表,并为其设置CustomizerClass。我在setNoDataMessage中使用customizerClass来检查是否从数据库中选择了任何数据。该图表在摘要中工作正常,但是当我将图表放在页眉中时,NoDataMessage显示出来。

有没有人对这个问题有任何想法?

PS。该图表在页脚,细节方面也很好。 NoDataMessage显示在标题,网页页眉,列标题中。

JasperReports java API导出的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="Sample3Report" pageWidth="700" pageHeight="3000" whenNoDataType="AllSectionsNoDetail" columnWidth="562" leftMargin="30" rightMargin="20" topMargin="30" bottomMargin="30" isTitleNewPage="true" isSummaryNewPage="true">
    <style name="titleStyle" isDefault="true" forecolor="#000000" fontName="Helvetica" fontSize="20" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" isPdfEmbedded="true"/>
    <style name="headerStyle" isDefault="true" forecolor="#FFFFFF" backcolor="#808080" fontName="Helvetica" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" isPdfEmbedded="true"/>
    <style name="detailStyle" isDefault="true" forecolor="#000000" fontName="Helvetica" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" isPdfEmbedded="true"/>
    <style name="pageheaderStyle" isDefault="true" forecolor="#000000" fontName="Helvetica" fontSize="10" isItalic="false" isUnderline="false" isStrikeThrough="false" isPdfEmbedded="true"/>
    <parameter name="IMAGE" class="java.lang.String"/>
    <queryString>
        <![CDATA[select p.personid as personId, p.name as personName, p.age as personAge, c.carid as carId, c.model as carModel, c.color as carColor, c.price as carPrice from person p join car c on p.personid=c.ownerid order by personName]]>
    </queryString>
    <field name="personName" class="java.lang.String"/>
    <field name="personAge" class="java.lang.Integer"/>
    <field name="carModel" class="java.lang.String"/>
    <field name="carColor" class="java.lang.String"/>
    <field name="carPrice" class="java.lang.Integer"/>
    <title>
        <band height="700">
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="titleTextField" style="titleStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="250" width="600" height="300"/>
                <box padding="1">
                    <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Cars report"+"\n\n.subtitle"+"\n\n.writer"]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="500">
            <textField isStretchWithOverflow="true" evaluationTime="Auto" isBlankWhenNull="true">
                <reportElement key="titleTextField" style="detailStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="570" y="0" width="600" height="14"/>
                <box padding="1">
                    <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Cars table"]]></textFieldExpression>
            </textField>
            <image>
                <reportElement x="540" y="0" width="30" height="30"/>
                <imageExpression class="java.lang.String"><![CDATA["http://static4.depositphotos.com/1029305/388/v/950/depositphotos_3889034-Stylized-cartoon-car.jpg"]]></imageExpression>
            </image>
            <areaChart>
                <chart customizerClass="customizer.AreaChartCustomizer">
                    <reportElement x="0" y="100" width="350" height="200"/>
                    <chartTitle>
                        <titleExpression><![CDATA["chart1. line chart of portA"]]></titleExpression>
                    </chartTitle>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA["test"]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{carModel}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{carPrice}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <areaPlot>
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </valueAxisFormat>
                </areaPlot>
            </areaChart>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="12">
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="headerField1" style="headerStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="0" width="150" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Owner name"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="headerField2" style="headerStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="150" y="0" width="100" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Owner age"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="headerField3" style="headerStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="250" y="0" width="150" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Car model"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="headerField4" style="headerStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="400" y="0" width="100" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Car color"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="headerField5" style="headerStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="500" y="0" width="100" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Car price"]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="12">
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="detailField1" style="detailStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="0" width="150" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{personName}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="detailField2" style="detailStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="150" y="0" width="100" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression class="java.lang.Integer"><![CDATA[$F{personAge}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="detailField3" style="detailStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="250" y="0" width="150" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{carModel}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="detailField4" style="detailStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="400" y="0" width="100" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{carColor}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="detailField5" style="detailStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="500" y="0" width="100" height="12"/>
                <box padding="2">
                    <pen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.Integer"><![CDATA[$F{carPrice}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <pageFooter>
        <band height="50">
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement key="titleTextField" style="pageheaderStyle" positionType="Float" stretchType="RelativeToTallestObject" mode="Opaque" x="30" y="0" width="600" height="14"/>
                <box padding="1">
                    <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Cars table"]]></textFieldExpression>
            </textField>
            <image>
                <reportElement x="0" y="0" width="50" height="50"/>
                <imageExpression class="java.lang.String"><![CDATA[$P{IMAGE}]]></imageExpression>
            </image>
        </band>
    </pageFooter>
    <summary>
        <band height="700">
            <lineChart>
                <chart isShowLegend="true">
                    <reportElement x="0" y="300" width="350" height="200"/>
                    <chartTitle>
                        <titleExpression><![CDATA["chart2. line chart of portB"]]></titleExpression>
                    </chartTitle>
                    <chartSubtitle/>
                    <chartLegend textColor="#FFFF00" backgroundColor="#0000FF"/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA["test"]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{carModel}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{carPrice}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <linePlot>
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </valueAxisFormat>
                </linePlot>
            </lineChart>
            <areaChart>
                <chart customizerClass="customizer.AreaChartCustomizer">
                    <reportElement x="0" y="100" width="350" height="200"/>
                    <chartTitle>
                        <titleExpression><![CDATA["chart1. line chart of portA"]]></titleExpression>
                    </chartTitle>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA["test"]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{carModel}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{carPrice}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <areaPlot>
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </valueAxisFormat>
                </areaPlot>
            </areaChart>
            <multiAxisChart>
                <chart customizerClass="customizer.MultiAxisOrderCustomizer">
                    <reportElement x="0" y="500" width="350" height="200"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <multiAxisPlot>
                    <plot/>
                    <axis>
                        <areaChart>
                            <chart customizerClass="customizer.AreaChartCustomizer">
                                <reportElement x="0" y="100" width="350" height="200" backcolor="#FFFFFF"/>
                                <chartTitle color="#000000"/>
                                <chartSubtitle color="#000000"/>
                                <chartLegend textColor="#000000" backgroundColor="#FFFFFF"/>
                            </chart>
                            <categoryDataset>
                                <categorySeries>
                                    <seriesExpression><![CDATA["test"]]></seriesExpression>
                                    <categoryExpression><![CDATA[$F{carModel}]]></categoryExpression>
                                    <valueExpression><![CDATA[$F{carPrice}]]></valueExpression>
                                </categorySeries>
                            </categoryDataset>
                            <areaPlot>
                                <plot/>
                                <categoryAxisFormat>
                                    <axisFormat>
                                        <labelFont/>
                                        <tickLabelFont/>
                                    </axisFormat>
                                </categoryAxisFormat>
                                <valueAxisFormat>
                                    <axisFormat>
                                        <labelFont/>
                                        <tickLabelFont/>
                                    </axisFormat>
                                </valueAxisFormat>
                            </areaPlot>
                        </areaChart>
                    </axis>
                    <axis>
                        <lineChart>
                            <chart>
                                <reportElement x="0" y="300" width="350" height="200" backcolor="#FFFFFF"/>
                                <chartTitle color="#000000"/>
                                <chartSubtitle color="#000000"/>
                                <chartLegend textColor="#000000" backgroundColor="#FFFFFF"/>
                            </chart>
                            <categoryDataset>
                                <categorySeries>
                                    <seriesExpression><![CDATA["test"]]></seriesExpression>
                                    <categoryExpression><![CDATA[$F{carModel}]]></categoryExpression>
                                    <valueExpression><![CDATA[$F{carPrice}]]></valueExpression>
                                </categorySeries>
                            </categoryDataset>
                            <linePlot>
                                <plot/>
                                <categoryAxisFormat>
                                    <axisFormat>
                                        <labelFont/>
                                        <tickLabelFont/>
                                    </axisFormat>
                                </categoryAxisFormat>
                                <valueAxisFormat>
                                    <axisFormat>
                                        <labelFont/>
                                        <tickLabelFont/>
                                    </axisFormat>
                                </valueAxisFormat>
                            </linePlot>
                        </lineChart>
                    </axis>
                </multiAxisPlot>
            </multiAxisChart>
        </band>
    </summary>
</jasperReport>

0 个答案:

没有答案