即使在DynamicReports中分配了数据集,子报表字段仍为空 [英] Subreport field is still null even if the dataset is assigned in DynamicReports

查看:163
本文介绍了即使在DynamicReports中分配了数据集,子报表字段仍为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DynamicReports(4.1.0)和iReport(5.6.0)生成pdf文件。我在iReport中创建了一个子报表,它有一个饼图。通过DynamicReports运行代码后,创建的PDF不会显示此饼图。以下是代码:

I am using the DynamicReports (4.1.0) and iReport (5.6.0) to generate pdf files. I have created a subreport in iReport which has a pie chart. After I run the code via DynamicReports, the created PDF does not show this piechart. Here is the code:

HashMap<String,Object> map = new HashMap<String,Object>();

map.put("id", "157"); //Actually, this will not be used since I pass this value statically in subreport.jrxml
Object[] array =  new Object[1];
array[0] = map;
JRMapArrayDataSource ds = new JRMapArrayDataSource(array);
InputStream is = JasperTemplateDesignReport2.class.getResourceAsStream("mainreport.jrxml");

try {
    report()
    .setTemplateDesign(is)
        .setDataSource(ds)
    .show();

} catch (DRException e) {
    e.printStackTrace();
} 

subreport.jrxml:

subreport.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="templatereport2_subreport3" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isTitleNewPage="true" uuid="3dc0ebf2-660d-4d0c-af85-ccae1f01bdee">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="id" class="java.lang.String"/>
    <queryString>
        <![CDATA[SELECT
     count(status) AS count,
     case when status=TRUE then 'Succeeded'else 'Failed'end AS status
FROM
     "dbtable" dbtable
WHERE
     id = 157
GROUP BY
     status]]>
    </queryString>
    <field name="count" class="java.lang.Long"/>
    <field name="status" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="351" splitType="Stretch">
            <pie3DChart>
                <chart evaluationTime="Report">
                    <reportElement x="40" y="24" width="415" height="244" uuid="a43afa44-c4e5-4ad5-b06e-8922128bcdaf"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <pieDataset>
                    <keyExpression><![CDATA[$F{status}]]></keyExpression>
                    <valueExpression><![CDATA[$F{count}]]></valueExpression>
                </pieDataset>
                <pie3DPlot>
                    <plot/>
                    <itemLabel/>
                </pie3DPlot>
            </pie3DChart>
            <textField>
                <reportElement x="134" y="292" width="100" height="20" uuid="2726affe-1fef-45a1-abc8-8da8c9856e42"/>
                <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="20" y="292" width="100" height="20" uuid="fa4e41c0-b4f4-443c-ab9e-3cf048a3f520"/>
                <textFieldExpression><![CDATA[$F{status}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

mainreport.jrxml

mainreport.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="templatedesign2" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="575" leftMargin="10" rightMargin="10" topMargin="10" bottomMargin="10" uuid="8c9654e9-6770-46c4-a4d0-4bdf99195b70">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="229"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["D:\\DynamicReports\\src\\subreport\\"]]></defaultValueExpression>
    </parameter>
    <field name="id" class="java.lang.String"/>
    <group name="Intro">
        <groupHeader>
            <band height="203">
                <subreport isUsingCache="false">
                    <reportElement x="0" y="69" width="575" height="77" uuid="eb90e8b6-2860-4c94-ad10-3eb5031502fc"/>
                    <subreportParameter name="SUBREPORT_DIR">
                        <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportParameter name="id">
                        <subreportParameterExpression><![CDATA[$F{id}]]></subreportParameterExpression>
                    </subreportParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "templatereport2_subreport3.jasper"]]></subreportExpression>
                </subreport>
            </band>
        </groupHeader>
    </group>
    <pageFooter>
        <band height="79" splitType="Stretch"/>
    </pageFooter>
</jasperReport>

任何想法?

推荐答案

我已经修改了Java代码如下,然后饼图开始显示在创建的PDF上:

I have modified the Java code as the following, then the Pie chart started to be displayed on the created PDF:

Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/myDB", "username", "password");
HashMap<String,Object> parmsMap = new HashMap<String,Object>();
parmsMap.put("id", "157");
String sourceFileName = 
                "D://DynamicReports//src//subreport//masterreport.jasper";
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName, parmsMap, conn);
JasperViewer.viewReport(jasperPrint, false);
conn.close();

我认为关键是发送对我有用的连接。

I believe the key thing is sending the connection which worked for me.

这篇关于即使在DynamicReports中分配了数据集,子报表字段仍为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆