gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:40:08 25 4
gpt4 key购买 nike

我有这个 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 与 tabel 在此图表中使用的相同。

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

我的问题是

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

最佳答案

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

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

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

解决方案

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

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

关于java - 为什么图表使用最后记录数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35810662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com