Reputation: 2080
I'm currently using Jaspersoft Studio 6.3.0 and jasperreports-6.2.0.jar for generating JasperReports's reports from Java application.
I have to create multiple xy-line chart(not just a single line) that too using JRBeanCollectionDataSource.
If I work with just a single xy-data list List<Coordinates>
, which contains data for only one line, Then I will have the correct XY-line chart but with only one line.
but I need multiple lines plotted on a chart, And the number of lines to be drawn are dynamic.
I hope someone with good experience in JasperReports can easily solve it.I have tried searching for multiple xy-line chart with JRBeanCollectionDataSource, but they all have the same example using database query.
Can anyone suggest some good reading material for jasper, because this is just beginning for me ,and on later stage I have to generate document, using jasper reports and I believe, I will have to use most of the functionalities provided by jasper(charts, tables, static data,Table of Content, etc.).
Bean Class:
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;
}
this is how I create report: I have created
List<List<Coordinates>> coordinatesList = new ArrayList<>();
so that I can hold multiple line data in it.
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();
}
Here is my 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>
Upvotes: 1
Views: 2659
Reputation: 2080
Actually, multiple lines mean multiple series in JasperReports.
And to solve it I created a separate bean for each series.I am not sure if this is the best solution, but kind of worked for me.
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();
}
I even created an extra series variable in BEAN class
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
Here Also, I have added new Series variable
<![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>
output:
Upvotes: 1