1
votes

I want to be able to create the following jasper report

Main report with general information and then multiple charts (dynamic number of them)

Currently i have only a single chart in this report and i supply to it a list of objects (from java) where each have a "key" (which is a Date) and a value which is a Number

For this single chart, all works fine, Now, when i want to draw multiple charts , i am not sure how to progress

From other posts i see that some use "sub-report" and on the sub report they place the chart

I tried this, charts do appear multiple times but empty This is my data set more or less

class ChartData{
   String name;
   List<ChartItem> items;
}


class ChartItem{
   Date key;
   Integer value;
}

How do i pass data from the main report to the sub report? How do i use it in the sub report? I will use the "name" member as a title above the chart

1

1 Answers

1
votes

Found it

In the sub-report you have to use the reserved field called "_THIS"

You have to add this line

<field name="_THIS" class="some.class.type"/>

for example:

<field name="_THIS" class="com.company.ChartData"/>
or 
<field name="_THIS" class="java.lang.Integer"/>

This field will be filled at run-time with the current item in the DataSet.

This is the section of the sub-report inside the main report

<subreport>
    <reportElement x="0" y="23" width="572" height="118" uuid="27e38f7e-9329-4c77-a590-fe1b7eabef85"/>
    <dataSourceExpression><![CDATA[$P{GAPS}]]></dataSourceExpression>
    <subreportExpression><![CDATA["sub_gaps.jasper"]]></subreportExpression>
</subreport>

where "GAPS" is the name of the parameter that i use to inject the data from the java code

<parameter name="GAPS" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>