我设法至少使用嵌套列表创建了图表。我的Java对象:
public class PoolUserLoginsVO {
private int userLogins;
private String poolId;
private Date date;
//constructor + getter/setters
userLogins是y坐标值,图表是时间序列图,因此x坐标是日期。 poolId是坐标所属的系列。
我获得了每个系列所需的值列表,并且由于有多个系列,因此我有一个列表列表,该列表传递给Jasper Reports的参数映射。
我的碧玉报告:
主要碧玉(整个文件很长,所以只有相关部分):
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown -->
<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="fbaeab5e-6793-456e-9567-e0c5af982904">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="poolLogins" class="java.util.List"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="223">
<subreport>
<reportElement x="0" y="0" width="550" height="200" uuid="9f8a2d4a-1d90-4f88-a08c-1ea2604360c5"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{poolLogins})]]></dataSourceExpression>
<subreportExpression><![CDATA["sub_charts.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
$ P {poolLogins}是List<List<PoolUserLoginsVO>
。
sub_charts.jasper:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown -->
<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><![CDATA[_THIS]]></fieldDescription>
</field>
<detail>
<band height="237" splitType="Stretch">
<subreport>
<reportElement x="0" y="20" width="540" height="200" uuid="1ec1f733-ffee-46f3-859c-f774e5277d19"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{_THIS})]]></dataSourceExpression>
<subreportExpression><![CDATA["sub_chart.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
sub_chart.jasper:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown -->
<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_chart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4ce41625-c60b-4759-acbe-5fe12a013fb2">
<queryString>
<![CDATA[]]>
</queryString>
<field name="userLogins" class="java.lang.Integer"/>
<field name="poolId" class="java.lang.String"/>
<field name="date" class="java.util.Date"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="210" splitType="Stretch">
<timeSeriesChart>
<chart evaluationTime="Report">
<reportElement x="0" y="20" width="540" height="180" uuid="874eee5e-1e71-4870-906b-ef71ef91d274"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<timeSeriesDataset>
<timeSeries>
<seriesExpression><![CDATA[$F{poolId}]]></seriesExpression>
<timePeriodExpression><![CDATA[$F{date}]]></timePeriodExpression>
<valueExpression><![CDATA[$F{userLogins}.intValue()]]></valueExpression>
</timeSeries>
</timeSeriesDataset>
<timeSeriesPlot>
<plot/>
<timeAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</timeAxisFormat>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</valueAxisFormat>
</timeSeriesPlot>
</timeSeriesChart>
</band>
</detail>
</jasperReport>
PDF和图表成功生成。但是有两个问题:
1)即使只有4个系列,我也会得到30页的图表。每个系列精确地重复30次(每个系列中的坐标数)。生成的图表都是正确的。
2)我不需要单独的图表。我希望所有系列都放在一张图表中,并用不同的颜色显示。
我在这里做错了什么?
编辑:
Main:
public class testmain {
public static void main(String[] args) {
int threshold = 30;
List<List<PoolUserLoginsVO>> poolLogins = new ArrayList<>();
List<PoolUserLoginsVO> userLogins = new ArrayList<>();
for (int i = 0; i < threshold; ++i) {
LocalDate localDate = LocalDate.now().minusDays(threshold - i - 1);
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
PoolUserLoginsVO logins = new PoolUserLoginsVO(i, "pool1", date);
userLogins.add(logins);
}
poolLogins.add(userLogins);
List<PoolUserLoginsVO> userLogins2 = new ArrayList<>();
for (int i = 0; i < threshold; ++i) {
LocalDate localDate = LocalDate.now().minusDays(threshold - i - 1);
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
PoolUserLoginsVO logins = new PoolUserLoginsVO(1, "pool2", date);
userLogins2.add(logins);
}
poolLogins.add(userLogins2);
List<PoolUserLoginsVO> userLogins3 = new ArrayList<>();
for (int i = threshold; i > 0; --i) {
LocalDate localDate = LocalDate.now().minusDays(threshold - i - 1);
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
PoolUserLoginsVO logins = new PoolUserLoginsVO(i, "pool3", date);
userLogins3.add(logins);
}
poolLogins.add(userLogins3);
JRBeanCollectionDataSource vmsJRBean = new JRBeanCollectionDataSource(poolLogins);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("poolLogins", poolLogins);
File file = new File("report.pdf");
try {
FileOutputStream fop = new FileOutputStream(file);
JasperPrint jasperPrint = JasperFillManager.fillReport("test.jasper", parameters, new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(jasperPrint, fop);
fop.flush();
fop.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}