I have this ChartField class
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class ChartField {
private int x;
private int y;
}
and MyChart class have a List of ChartField
import java.util.List;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MyChart {
private List<ChartField> chartFields;
}
This is the code how i generate the report.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
public class TestReport {
public static void main(String[] args) {
String source = "reports/test_report.jasper";
try {
List<MyChart> listMyCharts = new ArrayList<>();
{
MyChart myChart = new MyChart();
List<ChartField> chartFields = new ArrayList<>();
myChart.setChartFields(chartFields);
{
chartFields.add(new ChartField(2, 3));
chartFields.add(new ChartField(5, 6));
chartFields.add(new ChartField(7, 9));
chartFields.add(new ChartField(11, 12));
chartFields.add(new ChartField(12, 16));
chartFields.add(new ChartField(17, 22));
}
listMyCharts.add(myChart);
}
{
MyChart myChart = new MyChart();
List<ChartField> chartFields = new ArrayList<>();
myChart.setChartFields(chartFields);
{
chartFields.add(new ChartField(5, 4));
chartFields.add(new ChartField(7, 8));
chartFields.add(new ChartField(12, 5));
chartFields.add(new ChartField(15, 18));
chartFields.add(new ChartField(18, 21));
chartFields.add(new ChartField(34, 55));
}
listMyCharts.add(myChart);
}
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(listMyCharts);
Map<String, Object> map = new HashMap<>();
JasperPrint jp = JasperFillManager.fillReport(source, map, dataSource);
JasperViewer.viewReport(jp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is the design of test_report.jrxml as you can see i put table and chart in detail band. My expectation is the detail band can repeat my listMyCharts object (see TestReport class)

JRDatasource expression of table is
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{chartFields})
as you can see i use the same JRDatasource that tabel use in this chart.

This is the test_report.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0 -->
<!-- 2016-03-05T12:51:11 -->
<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="master_report_template"
pageWidth="595" pageHeight="842" columnWidth="555"
leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
uuid="a488f074-1b9d-4cc7-95d4-51323412d4b2">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Dataset1" uuid="8c5a7d5e-8685-4dae-b33b-b816e12907c4">
<queryString>
<![CDATA[]]>
</queryString>
<field name="x" class="java.lang.Integer"/>
<field name="y" class="java.lang.Integer"/>
</subDataset>
<queryString>
<![CDATA[]]>
</queryString>
<field name="chartFields" class="java.util.List"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="202" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<componentElement>
<reportElement x="10" y="10" width="220" height="76" uuid="41604712-0359-4058-8a74-30677eb774df">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</reportElement>
<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="Dataset1" uuid="6ad6bedb-dae4-4eb4-9f4e-57a0c04e73d5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{chartFields})]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="6a31d692-e928-497f-a168-a31c12620680">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="897b63dd-e6ee-4329-9382-495c9b9a4286"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[x]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="f6b7583f-e131-4ce4-bc8c-fdc83f4515dc"/>
<textElement>
<font size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{x}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="19cfc888-0988-42a7-a729-dd673a6c078d">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="08a3e523-4c8b-4374-ab60-16c3cc577060"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[y]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="99c2e10d-1cf3-4975-99b9-b1f1f6c55571"/>
<textElement>
<font size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{y}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
<xyLineChart>
<chart evaluationTime="Report">
<reportElement x="260" y="0" width="294" height="200" uuid="19cc8a02-7aa3-4518-96f8-71a636b626cf"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<dataset>
<datasetRun subDataset="Dataset1" uuid="32ba4e38-1819-47f9-bbd6-4a4381e367a5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{chartFields})]]></dataSourceExpression>
</datasetRun>
</dataset>
<xySeries autoSort="true">
<seriesExpression><![CDATA["SERIES 1"]]></seriesExpression>
<xValueExpression><![CDATA[$F{x}]]></xValueExpression>
<yValueExpression><![CDATA[$F{y}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</linePlot>
</xyLineChart>
</band>
</detail>
</jasperReport>
This is the result of the report that i successfully generated. You can see the table is repeated as i want.

My question is
But why the heck the chart does not follow the table instead used the last data?
On the chart tag you are using evaluationTime="Report" this means
Report: A constant specifying that an expression should be evaluated at the end of the filling process.
At the end of filling process the $F{chartFields} is the last record.
Solution
Replace it with evaluationTime="Now" or remove it (this is default)
Now A constant specifying that an expression should be evaluated at the exact moment in the filling process when it is encountered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With