为什么图表使用最后的记录数据?

时间:2016-03-05 05:57:01

标签: java charts jasper-reports

我有ChartField

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class ChartField {

    private int x;
    private int y;

}

和MyChart类有一个ChartField列表

import java.util.List;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class MyChart {

    private List<ChartField> chartFields;

}

这是我生成报告的代码。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;

public class TestReport {

    public static void main(String[] args) {

        String source = "reports/test_report.jasper";

        try {

            List<MyChart> listMyCharts = new ArrayList<>();
            {
                MyChart myChart = new MyChart();
                List<ChartField> chartFields = new ArrayList<>();
                myChart.setChartFields(chartFields);
                {
                    chartFields.add(new ChartField(2, 3));
                    chartFields.add(new ChartField(5, 6));
                    chartFields.add(new ChartField(7, 9));
                    chartFields.add(new ChartField(11, 12));
                    chartFields.add(new ChartField(12, 16));
                    chartFields.add(new ChartField(17, 22));
                }
                listMyCharts.add(myChart);
            }
            {
                MyChart myChart = new MyChart();
                List<ChartField> chartFields = new ArrayList<>();
                myChart.setChartFields(chartFields);
                {
                    chartFields.add(new ChartField(5, 4));
                    chartFields.add(new ChartField(7, 8));
                    chartFields.add(new ChartField(12, 5));
                    chartFields.add(new ChartField(15, 18));
                    chartFields.add(new ChartField(18, 21));
                    chartFields.add(new ChartField(34, 55));
                }
                listMyCharts.add(myChart);
            }

            JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(listMyCharts);

            Map<String, Object> map = new HashMap<>();
            JasperPrint jp = JasperFillManager.fillReport(source, map, dataSource);
            JasperViewer.viewReport(jp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

这是test_report.jrxml的设计 你可以看到我把表格和图表放在详细的乐队中。我的期望是细节乐队可以重复我的listMyCharts对象(参见TestReport类)

the test_report.jrxml design

表的JRDatasource表达式是

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{chartFields})

正如您所看到的,我使用的是表格中使用的相同JRDatasource。

the chart Wizard setting

这是test_report.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-03-05T12:51:11 -->
<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="master_report_template" 
    pageWidth="595" pageHeight="842" columnWidth="555" 
    leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" 
    uuid="a488f074-1b9d-4cc7-95d4-51323412d4b2">

    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <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="Dataset1" uuid="8c5a7d5e-8685-4dae-b33b-b816e12907c4">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="x" class="java.lang.Integer"/>
        <field name="y" class="java.lang.Integer"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="chartFields" class="java.util.List"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="202" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <componentElement>
                <reportElement x="10" y="10" width="220" height="76" uuid="41604712-0359-4058-8a74-30677eb774df">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </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="Dataset1" uuid="6ad6bedb-dae4-4eb4-9f4e-57a0c04e73d5">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{chartFields})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="6a31d692-e928-497f-a168-a31c12620680">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="897b63dd-e6ee-4329-9382-495c9b9a4286"/>
                                <textElement>
                                    <font size="14"/>
                                </textElement>
                                <text><![CDATA[x]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="f6b7583f-e131-4ce4-bc8c-fdc83f4515dc"/>
                                <textElement>
                                    <font size="14"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{x}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="19cfc888-0988-42a7-a729-dd673a6c078d">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="08a3e523-4c8b-4374-ab60-16c3cc577060"/>
                                <textElement>
                                    <font size="14"/>
                                </textElement>
                                <text><![CDATA[y]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="99c2e10d-1cf3-4975-99b9-b1f1f6c55571"/>
                                <textElement>
                                    <font size="14"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{y}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
            <xyLineChart>
                <chart evaluationTime="Report">
                    <reportElement x="260" y="0" width="294" height="200" uuid="19cc8a02-7aa3-4518-96f8-71a636b626cf"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <xyDataset>
                    <dataset>
                        <datasetRun subDataset="Dataset1" uuid="32ba4e38-1819-47f9-bbd6-4a4381e367a5">
                            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{chartFields})]]></dataSourceExpression>
                        </datasetRun>
                    </dataset>
                    <xySeries autoSort="true">
                        <seriesExpression><![CDATA["SERIES 1"]]></seriesExpression>
                        <xValueExpression><![CDATA[$F{x}]]></xValueExpression>
                        <yValueExpression><![CDATA[$F{y}]]></yValueExpression>
                    </xySeries>
                </xyDataset>
                <linePlot>
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                </linePlot>
            </xyLineChart>
        </band>
    </detail>
</jasperReport>

这是我成功生成的报告的结果。你可以看到我的表重复。

the report result

我的问题是

但为什么图表不跟随表而不是使用最后的数据呢?

1 个答案:

答案 0 :(得分:2)

在您使用的chart标记上evaluationTime="Report",这意味着

  

报告:一个常量,指定应在填充过程结束时评估表达式。

在填充过程结束时,$F{chartFields}是最后一条记录。

<强>解决方案

将其替换为evaluationTime="Now"或将其删除(这是默认设置)

  

现在一个常量,指定应在中计算表达式   遇到填充过程中的确切时刻。