问题描述
我必须使用相同的图表报告模板创建具有不同数据集的多个XY线图,我还必须使用JRBeanCollectionDatasource。
I have to create multiple XY-line charts with different dataset using same chart report template and I also have to use JRBeanCollectionDatasource for it.
要求:
1)应使用JRBeanCollectionDatasource完成。
1) Should be done using JRBeanCollectionDatasource.
2)必须使用相同的图表报告模板以创建多个图表。
2) Have to use the same chart report template to create multiple charts.
3)图表数量不固定(这里我有问题在java中为报告参数命名)。因为在ReportParametersMap中,它们只能有唯一的键名。
3) Number of charts are not fixed (Here I have problem giving names to Report Parameter in java). Because in ReportParametersMap, they can only have unique key name .
Java:
Coordinates.java
Coordinates.java
private Number series;
private Number xCoordinate;
private Number yCoordinate;
//Getters & Setters
GenerateReport.java
GenerateReport.java
我是使用报表簿和报表簿的每个报表模板都被视为子报表。所以我将 XYChartDataSource
(java.util.List)传递给主报表,我将使用
将此参数映射到子报表new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ P {XYChartDataSource})
作为数据源表达式。
I am working with Report Book and each report template of the report book is considered as a sub-report. So I am passing XYChartDataSource
(java.util.List) to master report book and I would map this parameter with the subreport by using new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{XYChartDataSource})
as a Datasource Expression.
并在子报表中,我在MainDataset中创建了一个参数 XYChartDataSource
(java.util.List)并创建了字段(series,xCoordinate,yCoordinate)
and in Subreport, I have created a parameter XYChartDataSource
(java.util.List) and created fields (series,xCoordinate,yCoordinate) in MainDataset ( used in chart)
List<List<Coordinates>> allchartData = new ArrayList<>();
List<Coordinates> chartData = new ArrayList<>();
chartData.add(new Coordinates(2.08, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.08, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.08, xCoordinate, yCoordinate));
allchartData.add(chartData);
.
.
.
chartData.add(new Coordinates(2.12, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.12, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.12, xCoordinate, yCoordinate));
allchartData.add(chartData);
.
.
.
for (int i = 0; i < baselineChartData.size(); i++) {
parameters.put("XYChartDataSource", allchartData.get(i));
}
main_report_book.jrxml
<parameter name="XYChartDataSource" class="java.util.List"/>
<part uuid="5e668430-9acd-4835-be21-f4e2902ce33d">
<p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
<subreportParameter name="REPORT_DATA_SOURCE">
<subreportParameterExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{XYChartDataSource})]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR}+"/sub_chart.jasper"]]></subreportExpression>
</p:subreportPart>
</part>
sub_chart.jrxml
<parameter name="XYChartDataSource" class="java.util.List"/>
<field name="xCoordinate" class="java.lang.Double"/>
<field name="yCoordinate" class="java.lang.Double"/>
<field name="series" class="java.lang.Double"/>
<summary>
<band height="405">
<xyLineChart>
<chart evaluationTime="Report" bookmarkLevel="1">
<reportElement x="30" y="98" width="525" height="230" uuid="627d87d6-b675-409c-accb-b2bb3ffb9c80">
<property name="net.sf.jasperreports.chart.domain.axis.tick.interval" value="1"/>
</reportElement>
<chartTitle/>
<chartSubtitle/>
<chartLegend position="Right"/>
</chart>
<xyDataset>
<xySeries autoSort="true">
<seriesExpression><![CDATA[$F{series}]]></seriesExpression>
<xValueExpression><![CDATA[$F{xCoordinate}]]></xValueExpression>
<yValueExpression><![CDATA[$F{yCoordinate}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot isShowShapes="false">
<plot/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</linePlot>
</xyLineChart>
</textField>
</band>
</summary>
当前输出:
仅打印一张图表,使用常规方法。
Current Output:only one chart is being printed, using the regular method.
预期输出:
这里我显示了两个图表(实际输出可能更多),需要从相同的报告模板生成报告:
Here I am showing two charts(could be more in actual output) , which needs to be generated from same report template in the SAME PDF REPORT:
推荐答案
你的问题在这里:
for (int i = 0; i < baselineChartData.size(); i++) {
parameters.put("XYChartDataSource", allchartData.get(i));
}
您的参数XYChartDataSource
将包含列表
中的最后一个条目,每次循环时都会替换)
Your parameter "XYChartDataSource"
will contain last entry in your List
, you replace each time in loop see Map.put(K key,V value))
我们需要的是整个列表
parameters.put("XYChartDataSource", allchartData);
但是现在我们无法直接访问列表<坐标>
However now we can't access directly the List<Coordinates>
在不更改当前子报表的解决方案是在中间插入另一个子报表,它将迭代 List< List<协调>>
详细信息区。
On solution to not change your current subreport is to insert another subreport in the middle which will iterate your List<List<Coordinates>>
in detail band.
结构将
-
传递
列表< List< Coordinates>> allchartData
作为此新子报表的数据源(sub_charts.jrxml)
Pass
List<List<Coordinates>> allchartData
as datasource to this new subreport (sub_charts.jrxml)
定义字段 _THIS
在子报表中是列表<坐标>
(因此它正在迭代您的列表<列表<坐标>>
)
Define the field _THIS
which is List<Coordinates>
in subreport (hence it's iterating your List<List<Coordinates>>
)
详细信息包括当前的sub_chart.jrxml并传递 $ F {_THIS}
作为数据源
In detail band include current sub_chart.jrxml and pass $F{_THIS}
as datasource
sub_charts.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<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="sub_charts" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc8c76ba-0b85-4522-bf67-4c62ae87202b">
<field name="_THIS" class="java.util.List">
<fieldDescription>_THIS</fieldDescription>
</field>
<detail>
<band height="63" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="550" height="60" uuid="b0e761bf-fe02-4a0a-bafb-32d6831b7a13"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{_THIS})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR}+"/sub_chart.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
请记住在 main_report_book.jrxml 中调用这个新的子报告。
Remember to call this new subreport in your main_report_book.jrxml.
<subreportExpression><![CDATA[$P{SUBREPORT_DIR}+"/sub_charts.jasper"]]></subreportExpression>
OP 为这个答案做了这个伟大的图形表示。
The OP Dhruvil Thaker did this great graphical representation of this answer.
这篇关于如何使用Jasperreports中的JRBeanCollectionDatasource创建相同类型但具有不同数据的多个图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!