为什么图表使用最近的记录数据? [英] Why is chart using last record data?

查看:33
本文介绍了为什么图表使用最近的记录数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 ChartField

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

@Getter
@Setter
@AllArgsConstructor
public class ChartField {

    private int x;
    private int y;

}

和MyChart类具有ChartField列表

and MyChart class have a List of ChartField

import java.util.List;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class MyChart {

    private List<ChartField> chartFields;

}

这是我如何生成报告的代码.

This is the code how i generate the report.

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类)

This is the design of test_report.jrxml as you can see i put table and chart in detail band. My expectation is the detail band can repeat my listMyCharts object (see TestReport class)

表的JRDatasource表达式为

JRDatasource expression of table is

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

如您所见,我使用的图表与表格中表格使用的JRDatasource相同.

as you can see i use the same JRDatasource that tabel use in this chart.

这是test_report.jrxml

This is the 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>

这是我成功生成的报告的结果.您可以看到重复的表是我想要的.

This is the result of the report that i successfully generated. You can see the table is repeated as i want.

我的问题是

但是,为什么图表没有跟随表格,而是使用了最后一个数据呢?

But why the heck the chart does not follow the table instead used the last data?

推荐答案

图表标记上,您正在使用 evaluationTime ="Report" 这表示

On the chart tag you are using evaluationTime="Report" this means

报告:一个常量,指定在填充过程结束时应对表达式进行求值.

Report: A constant specifying that an expression should be evaluated at the end of the filling process.

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

At the end of filling process the $F{chartFields} is the last record.

解决方案

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

Replace it with evaluationTime="Now" or remove it (this is default)

Now (常量),该常量指定应在以下位置对表达式求值:遇到填充过程时的确切时刻.

Now A constant specifying that an expression should be evaluated at the exact moment in the filling process when it is encountered.

这篇关于为什么图表使用最近的记录数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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