gpt4 book ai didi

java - 如何从自定义数据源创建图表或表格

转载 作者:行者123 更新时间:2023-12-01 22:56:06 25 4
gpt4 key购买 nike

我正在使用 JasperReports 创建报告,我已经将数据从数据源传递到报告,但是我不确定如何让它与图表或表格一起使用,我确信它应该是 getdata 函数调用 Use JrDatasource 表达式,但我不确定如何调用它或包含什么内容。有人可以帮忙吗?

我尝试过使用参数,但没有使用自定义数据源的示例,我见过的其他数据源只有 JRBean,这与我实现的不同。

    private static final String[] nameArray = {"Frank", "Joseph", "Marco", "Carl", "Lenny", "Homer", "Teodor", "Leopold"};

private static final Integer[] ageArray = {50,30,40,46,44,26,32,21};

private int counter = -1;

private HashMap<String, Integer> fieldsNumber = new HashMap<String, Integer>();

private int lastFieldsAdded = 0;

@Override
public boolean next() throws JRException {
if (counter<nameArray.length-1) {
counter++;
return true;
}
return false;
}

@Override
public Object getFieldValue(JRField jrField) throws JRException {
Integer fieldIndex;
if (fieldsNumber.containsKey(jrField.getName()))
fieldIndex = fieldsNumber.get(jrField.getName());
else {
fieldsNumber.put(jrField.getName(), lastFieldsAdded);
fieldIndex = lastFieldsAdded;
lastFieldsAdded ++;
}
if (fieldIndex == 0) return nameArray[counter];
else if (fieldIndex == 1) return ageArray[counter];
return "";
}

public Object getFieldValue2(JRField jrField) throws JRException {
if (jrField.getName().equals("Name")) return nameArray[counter];
else if (jrField.getName().equals("Age")) return ageArray[counter];
return "";
}

public static JRDataSource getDataSource(){
return new Implementation();
}

Jrxml 表:

<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="LeveyJenDataset" uuid="0b1d0395-fcd9-471a-b840-e800fbc4cad8">
<dataSourceExpression><![CDATA[Implementation()]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="3211405d-427f-43db-8d48-babd275bfaa8">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="a59d1ac1-555d-487d-8844-4e75510a2ce0"/>
<text><![CDATA[Field_1]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="995816a4-8066-421b-9a41-650fe35b0a11"/>
<textFieldExpression><![CDATA[$F{Field_1}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="174224c5-0ff0-41f2-be74-2a32f32d3d5f">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="59835446-f3e1-442b-a9fb-55243ec97d10"/>
<text><![CDATA[Field_2]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="fe462425-2890-4c63-83b3-46d02fb0b7dc"/>
<textFieldExpression><![CDATA[$F{Field_2}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
<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="LeveyJenDataset" uuid="0b1d0395-fcd9-471a-b840-e800fbc4cad8">
<dataSourceExpression><![CDATA[Implementation()]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="3211405d-427f-43db-8d48-babd275bfaa8">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="a59d1ac1-555d-487d-8844-4e75510a2ce0"/>
<text><![CDATA[Field_1]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="995816a4-8066-421b-9a41-650fe35b0a11"/>
<textFieldExpression><![CDATA[$F{Field_1}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="174224c5-0ff0-41f2-be74-2a32f32d3d5f">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="59835446-f3e1-442b-a9fb-55243ec97d10"/>
<text><![CDATA[Field_2]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="fe462425-2890-4c63-83b3-46d02fb0b7dc"/>
<textFieldExpression><![CDATA[$F{Field_2}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
<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="LeveyJenDataset" uuid="0b1d0395-fcd9-471a-b840-e800fbc4cad8">
<dataSourceExpression><![CDATA[Implementation()]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="3211405d-427f-43db-8d48-babd275bfaa8">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="a59d1ac1-555d-487d-8844-4e75510a2ce0"/>
<text><![CDATA[Field_1]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="995816a4-8066-421b-9a41-650fe35b0a11"/>
<textFieldExpression><![CDATA[$F{Field_1}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="174224c5-0ff0-41f2-be74-2a32f32d3d5f">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="59835446-f3e1-442b-a9fb-55243ec97d10"/>
<text><![CDATA[Field_2]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="fe462425-2890-4c63-83b3-46d02fb0b7dc"/>
<textFieldExpression><![CDATA[$F{Field_2}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>

这里我试图将 field_1 和 field_2 获取到表中,但是它不会为表输出任何内容,并且它说方法 Implement() 对于报告未定义

最佳答案

我明白了,我只是手动将数据适配器添加到数据集中。

关于java - 如何从自定义数据源创建图表或表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58439039/

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