我目前正在使用Jaspersoft Studio 6.3.0和jasperreports-6.2.0.jar从Java应用程序生成JasperReports的报告。
我必须使用JRBeanCollectionDataSource创建多个xy-line图表(不仅仅是一行)。
如果我只使用一个xy数据列表List<Coordinates>
,它只包含一行的数据,那么我将得到正确的XY线图,但只有一行。
但我需要在图表上绘制多条线,并且要绘制的线条数是动态的。
我希望有经验的JasperReports能够轻松解决它。我尝试使用JRBeanCollectionDataSource搜索多个xy-line图表,但他们都使用数据库查询进行相同的示例。
任何人都可以为jasper建议一些好的阅读材料,因为这只是我的开始,在后期我必须生成文档,使用jasper报告,我相信,我将不得不使用jasper提供的大部分功能(图表,表格,静态数据,目录等)。
Bean类:
public class Coordinates {
public Coordinates(Number xCoordinate, Number yCoordinate) {
super();
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
}
public Coordinates() {
}
private Number xCoordinate;
private Number yCoordinate;
public Number getxCoordinate() {
return xCoordinate;
}
public void setxCoordinate(Number xCoordinate) {
this.xCoordinate = xCoordinate;
}
public Number getyCoordinate() {
return yCoordinate;
}
public void setyCoordinate(Number yCoordinate) {
this.yCoordinate = yCoordinate;
}
这是我创建报告的方式: 我创建了
List<List<Coordinates>> coordinatesList = new ArrayList<>();
这样我就可以在其中保存多行数据。
JRBeanCollectionDataSource coordiantesListBean = new JRBeanCollectionDataSource(coordinatesList);
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream projectInputStream = classloader.getResourceAsStream("jasper/xyLineChart.jasper");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("XYChartDataSource", coordiantesListBean);
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(projectInputStream, parameters,
new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint,FILE_LOCATION);
} catch (JRException e) {
e.printStackTrace();
}
这是我的xyLineChart.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="test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f5d035d9-630a-476c-b3d1-32985d774cb3">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="XYdataset" uuid="276f43e3-9d6c-4df1-85cb-5ea8bef1c28c">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="XYChartDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="xCoordinate" class="java.lang.Number"/>
<field name="yCoordinate" class="java.lang.Number"/>
</subDataset>
<parameter name="XYChartDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
<![CDATA[]]>
</queryString>
<summary>
<band height="283" splitType="Stretch">
<xyLineChart>
<chart evaluationTime="Report">
<reportElement x="0" y="0" width="555" height="283" uuid="9c9ff114-81b4-433f-bf71-17e21ea1fd3e"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<dataset>
<datasetRun subDataset="XYdataset" uuid="a8b4a706-dfaf-4347-9410-3c7018fce5f2">
<dataSourceExpression><![CDATA[$P{XYChartDataSource}]]></dataSourceExpression>
</datasetRun>
</dataset>
<xySeries autoSort="true">
<seriesExpression><![CDATA["SERIES 1"]]></seriesExpression>
<xValueExpression><![CDATA[$F{xCoordinate}]]></xValueExpression>
<yValueExpression><![CDATA[$F{yCoordinate}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</linePlot>
</xyLineChart>
</band>
</summary>
</jasperReport>
答案 0 :(得分:0)
实际上,多行代表JasperReports中的多个系列。
为了解决这个问题,我为每个系列创建了一个单独的bean。我不确定这是否是最好的解决方案,但对我有用。
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream projectInputStream = classloader.getResourceAsStream("jasper/xyLineChart.jasper");
Map<String, Object> parameters = new HashMap<String, Object>();
for(int i = 0 ; i< coordinatesList.size() ; i++){
coordiantesListBean = new JRBeanCollectionDataSource(coordinatesList.get(i));
parameters.put("XYChartDataSource", coordiantesListBean);
}
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(projectInputStream, parameters,
new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint,FILE_LOCATION);
} catch (JRException e) {
e.printStackTrace();
}
我甚至在BEAN类中创建了一个额外的系列变量
public class Coordinates {
public Coordinates() {
}
public Coordinates(Number series, Number xCoordinate, Number yCoordinate) {
super();
this.series = series;
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
}
private Number series;
private Number xCoordinate;
private Number yCoordinate;
/** getters & setters **/
}
xyLineChart.jrxml
此处,我添加了新的系列变量
<![CDATA[$F{series}.doubleValue()]]>
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0 -->
<!-- 2016-07-22T09:37:19 -->
<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="test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f5d035d9-630a-476c-b3d1-32985d774cb3">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="XYdataset" uuid="276f43e3-9d6c-4df1-85cb-5ea8bef1c28c">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="XYChartDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="xCoordinate" class="java.lang.Double"/>
<field name="yCoordinate" class="java.lang.Double"/>
<field name="series" class="java.lang.Double"/>
</subDataset>
<parameter name="XYChartDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="283" splitType="Stretch">
<xyLineChart>
<chart evaluationTime="Report">
<reportElement x="0" y="0" width="555" height="283" uuid="78aa19cc-8293-4bdd-b9af-5c429db047d2"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<dataset>
<datasetRun subDataset="XYdataset" uuid="d121d53d-5698-4914-953e-03df6bf2af6e">
<dataSourceExpression><![CDATA[$P{XYChartDataSource}]]></dataSourceExpression>
</datasetRun>
</dataset>
<xySeries autoSort="true">
<seriesExpression><![CDATA[$F{series}.doubleValue()]]></seriesExpression>
<xValueExpression><![CDATA[$F{xCoordinate}.doubleValue()]]></xValueExpression>
<yValueExpression><![CDATA[$F{yCoordinate}.doubleValue()]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</linePlot>
</xyLineChart>
</band>
</summary>
</jasperReport>
输出: