如何使用 node-jasper 库发送多个数据集?

How to send multiple datasets using node-jasper library?

我的任务是用 nodeJS 生成 PDF,所以我正在使用 node-jasper 库。

我想知道的是如何发送多个数据集以填充多个图表。我只能发送一个数据集(主数据集):/

这是我的完整代码:

const express = require('express')
var fs = require('fs')

const app = express()
const port = 5000

    
var jasper = require('node-jasper')({
    path: './lib/jasperreports-6.10.0',
    reports: {
        hw: {
            jasper: './jasperFiles/test.jasper',
            jrxml: './reports/test.jrxml',
            conn: 'in_memory_json'
        }
    },
});

app.get('/jasper1', (req, res) => {

    var report = {
            report: 'hw',
            data: {
                title: "test title",
                dataset1: jasper.toJsonDataSource(
                        {
                            dados: [
                                {
                                    c: "ar",
                                    x: 'Arabe',
                                    y: 20,
                                },
                                {
                                    c: "en",
                                    x: 'English',
                                    y: 40,
                                }
                            ]
                        },
                        'dados' 
                    ),
                dataset2: jasper.toJsonDataSource(
                        {
                            dados: [
                                {
                                    c: "eu",
                                    x: 'Euro',
                                    y: 15,
                                },
                                {
                                    c: "usd",
                                    x: 'Dollar',
                                    y: 35,
                                }
                            ]
                        },
                        'dados' 
                    )
            }   
            ,
            dataset:{},
        }
    var pdf = jasper.pdf(report);
    res.set({
        'Content-type': 'application/pdf',
        'Content-Length': pdf.length
    });
    res.send(pdf)
});

app.listen(port, () => {
    console.log('app is runing on port: ', port)
})

这是报告设计: enter image description here

这是我创建的两个数据集: enter image description here

这是我的 jrxml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown  -->
<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="test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d2376d66-daf9-486d-877e-cc7f2fe28426">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <subDataset name="dataset1" uuid="80298b05-e1fd-427e-b7b5-c6a9e571d151">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="x" class="java.lang.String"/>
        <field name="y" class="java.lang.Integer"/>
        <field name="c" class="java.lang.String"/>
    </subDataset>
    <subDataset name="dataset2" uuid="a4f9409d-a1fa-45a5-a00b-d2f95b31573e">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="x" class="java.lang.String"/>
        <field name="y" class="java.lang.Integer"/>
        <field name="c" class="java.lang.String"/>
    </subDataset>
    <parameter name="title" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <columnHeader>
        <band height="64" splitType="Stretch">
            <staticText>
                <reportElement x="50" y="0" width="103" height="64" uuid="6f999744-c4f9-48ef-a587-677dc644d77d"/>
                <textElement>
                    <font size="26"/>
                </textElement>
                <text><![CDATA[Title]]></text>
            </staticText>
            <textField>
                <reportElement x="140" y="10" width="100" height="30" uuid="e5a12f91-ddb4-445b-9f5b-57093703abaa"/>
                <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="604" splitType="Stretch">
            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
            <pieChart>
                <chart evaluationTime="Report">
                    <reportElement x="0" y="0" width="278" height="604" uuid="49052c41-6b67-42ed-9c84-c3089d3b2411"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <pieDataset maxCount="10">
                    <dataset>
                        <datasetRun subDataset="dataset1" uuid="7466bccc-a18d-4c68-801f-caac7533de69"/>
                    </dataset>
                    <keyExpression><![CDATA[$F{c}]]></keyExpression>
                    <valueExpression><![CDATA[$F{y}]]></valueExpression>
                    <labelExpression><![CDATA[$F{x}]]></labelExpression>
                </pieDataset>
                <piePlot>
                    <plot/>
                    <itemLabel/>
                </piePlot>
            </pieChart>
            <pieChart>
                <chart evaluationTime="Report">
                    <reportElement x="278" y="0" width="277" height="604" uuid="9f438f49-3f14-44b9-81a4-5f475ff6ecb2"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <pieDataset maxCount="10">
                    <dataset>
                        <datasetRun subDataset="dataset2" uuid="ce453374-bcf5-4b02-b8d9-35b263f143bc"/>
                    </dataset>
                    <keyExpression><![CDATA[$F{c}]]></keyExpression>
                    <valueExpression><![CDATA[$F{y}]]></valueExpression>
                    <labelExpression><![CDATA[$F{x}]]></labelExpression>
                </pieDataset>
                <piePlot>
                    <plot/>
                    <itemLabel/>
                </piePlot>
            </pieChart>
        </band>
    </detail>
    <summary>
        <band height="67" splitType="Stretch">
            <staticText>
                <reportElement x="180" y="10" width="151" height="51" uuid="da960c22-4947-49e0-9185-d2d39e21bcd7"/>
                <textElement>
                    <font size="26"/>
                </textElement>
                <text><![CDATA[Footer]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>

问题是当我 运行 我的代码时,图表没有出现在生成的 PDF 中,这是发送数据集值的正确方法吗? 你能帮忙吗?

您需要在报表中声明 dataset1dataset2 参数并将它们作为两个图表子数据集的数据源发送。

像这样(见参数定义和datasetRun下的dataSourceExpression):

<parameter name="title" class="java.lang.String"/>
<parameter name="dataset1" class="net.sf.jasperreports.engine.JRDataSource"/>
<parameter name="dataset2" class="net.sf.jasperreports.engine.JRDataSource"/>
...
<detail>
    <band height="604" splitType="Stretch">
        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
        <pieChart>
            <chart evaluationTime="Report">
                <reportElement x="0" y="0" width="278" height="604" uuid="49052c41-6b67-42ed-9c84-c3089d3b2411"/>
                <chartTitle/>
                <chartSubtitle/>
                <chartLegend/>
            </chart>
            <pieDataset maxCount="10">
                <dataset>
                    <datasetRun subDataset="dataset1" uuid="7466bccc-a18d-4c68-801f-caac7533de69">
                        <dataSourceExpression><![CDATA[$P{dataset1}]]></dataSourceExpression>
                    </datasetRun>
                </dataset>
                <keyExpression><![CDATA[$F{c}]]></keyExpression>
                <valueExpression><![CDATA[$F{y}]]></valueExpression>
                <labelExpression><![CDATA[$F{x}]]></labelExpression>
            </pieDataset>
            <piePlot>
                <plot/>
                <itemLabel/>
            </piePlot>
        </pieChart>
        <pieChart>
            <chart evaluationTime="Report">
                <reportElement x="278" y="0" width="277" height="604" uuid="9f438f49-3f14-44b9-81a4-5f475ff6ecb2"/>
                <chartTitle/>
                <chartSubtitle/>
                <chartLegend/>
            </chart>
            <pieDataset maxCount="10">
                <dataset>
                    <datasetRun subDataset="dataset2" uuid="ce453374-bcf5-4b02-b8d9-35b263f143bc">
                        <dataSourceExpression><![CDATA[$P{dataset2}]]></dataSourceExpression>
                    </datasetRun>
                </dataset>
                <keyExpression><![CDATA[$F{c}]]></keyExpression>
                <valueExpression><![CDATA[$F{y}]]></valueExpression>
                <labelExpression><![CDATA[$F{x}]]></labelExpression>
            </pieDataset>
            <piePlot>
                <plot/>
                <itemLabel/>
            </piePlot>
        </pieChart>
    </band>
</detail>