JasperReports图表未按JRBeanCollectionDataSource填充

时间:2019-03-07 11:12:27

标签: java charts jasper-reports

我有一个简单的jasper报告,其中只有一个表格和一个图表。在我的表中填充数据时,即使使用相同的JRBeanCollectionDataSource,我的图表也不会显示。

我将数据作为参数传递给JasperReports,如下所示;

    // Parameters for report
    Map<String, Object> params = new HashMap<>();
    List<AccountRate> accountRates = getAccountRates(accountInfo);
    params.put("accountRateList", new JRBeanCollectionDataSource(accountRates));

这就是我将参数用作表和图表的数据源的方式;

<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="ChartSample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2dd48b15-239b-403e-9616-b740c6c8adea">
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<subDataset name="dsAccountRate" uuid="21e57a6c-f945-4fd9-abb4-1bae216e731f">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="tradeTypeName" class="java.lang.String"/>
    <field name="totalRate" class="java.math.BigDecimal"/>
</subDataset>
<parameter name="accountRateList" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
    <![CDATA[]]>
</queryString>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="402" splitType="Stretch">
        <staticText>
            <reportElement x="300" y="30" width="230" height="50" uuid="036cf83b-fbe6-4f7f-a250-d60d71422b02"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="20" isBold="true"/>
            </textElement>
            <text><![CDATA[Sample Chart]]></text>
        </staticText>
        <componentElement>
            <reportElement x="10" y="100" width="197" height="59" uuid="cc6ef81b-44a9-4635-a7f7-51d124b98453">
                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
            </reportElement>
            <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                <datasetRun subDataset="dsAccountRate" uuid="8c2d1341-9ebb-4b85-9a32-5b75aa72655b">
                    <dataSourceExpression><![CDATA[$P{accountRateList}]]></dataSourceExpression>
                </datasetRun>
                <jr:column width="100" uuid="c3070f47-bc37-4467-b357-2f8bba72db23">
                    <jr:columnHeader style="Table_CH" height="30">
                        <staticText>
                            <reportElement x="0" y="0" width="100" height="30" uuid="e28b47a7-62b6-4aef-b255-5eeb9898ee1d"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font isBold="true"/>
                            </textElement>
                            <text><![CDATA[tradeTypeName]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table_TD" height="30">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="d3400acf-28a5-47df-9d60-a653dd60f249"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle"/>
                            <textFieldExpression><![CDATA[$F{tradeTypeName}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="100" uuid="6fc447e1-4817-4e13-9eb0-e011bb70921a">
                    <jr:columnHeader style="Table_CH" height="30">
                        <staticText>
                            <reportElement x="0" y="0" width="100" height="30" uuid="91e14b19-e4d8-4774-a1a6-de8468962343"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font isBold="true"/>
                            </textElement>
                            <text><![CDATA[totalRate]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table_TD" height="30">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="0d51e863-95ec-42c3-bbd4-0f85f953284e"/>
                            <textElement textAlignment="Right" verticalAlignment="Middle">
                                <paragraph rightIndent="5"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{totalRate}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>
        <barChart>
            <chart evaluationTime="Report">
                <reportElement x="230" y="100" width="320" height="200" uuid="670a0f51-a0c2-4214-bbc5-7317b79ed82a"/>
                <chartTitle/>
                <chartSubtitle/>
                <chartLegend/>
            </chart>
            <categoryDataset>
                <dataset>
                    <datasetRun subDataset="dsAccountRate" uuid="ef1e7b48-1f55-415e-995b-36edfc85cd1f">
                        <dataSourceExpression><![CDATA[$P{accountRateList}]]></dataSourceExpression>
                    </datasetRun>
                </dataset>
                <categorySeries>
                    <seriesExpression><![CDATA[$F{tradeTypeName}]]></seriesExpression>
                    <categoryExpression><![CDATA[$F{tradeTypeName}]]></categoryExpression>
                    <valueExpression><![CDATA[$F{totalRate}.doubleValue()]]></valueExpression>
                </categorySeries>
            </categoryDataset>
            <barPlot>
                <plot/>
                <itemLabel/>
                <categoryAxisFormat>
                    <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                </categoryAxisFormat>
                <valueAxisFormat>
                    <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                </valueAxisFormat>
            </barPlot>
        </barChart>
    </band>
</title>

我不知道问题是什么,为什么我可以填写表格但不能填写图表?

有帮助吗?

1 个答案:

答案 0 :(得分:0)

请定义子报表的数据源为字段,

<field name="accountRateList" class="java.util.ArrayList">
    <fieldDescription><![CDATA[accountRateList}]]></fieldDescription>
</field>

然后将其传递给子报告

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{accountRateList})]]></dataSourceExpression>