1.0 CREATING MAIN REPORT

1) Create a Main Report and save it in your workgroup (In my case : C:\Users\MYName\JaspersoftWorkspace\MyReports\Main Report.jrxml)

SRImg1

Click Next
2) In Data Adapter : Select Sample DB

SRImg2

Click Finish
3) This will create a basic Report Template. Right Click on the Bands Report Header, Detail…etc except Title and Summary Band and delete them. Summary and Title are the only two bands where the data appears once in a report.

SRImg3

4) Create a parameter called COUNTRY with Class java.lang.String

SRImg4

5) Click Dataset and Query Editor

SRImg5

6) Using Sample DB create a simple SQL Query

SELECT ORDERS.SHIPCOUNTRY
FROM ORDERS
WHERE ORDERS.SHIPCOUNTRY = ‘$P!{COUNTRY}’

SRImg6

7) From The Fields Tab Drag $F{SHIPCOUNTRY} and Place it in the Title Bar.

2.0 CREATING SUBREPORT 1

1) Create a New Jasper Report and name it as SubReport1 and save it in the same repository.

2) Create the report template as shown in the previous section and also delete all the bands except Summary Band

3) Create a Parameter called COUNTRY in this report as similar to the main report.

4) Go to Query Editor and Type the below SQL
SELECT COUNT( ORDERS.ORDERID ) AS TOTAL_ORDERS,
MONTH( ORDERS.SHIPPEDDATE ) AS MONTHS,
ORDERS.SHIPCITY

FROM ORDERS
WHERE ORDERS.SHIPCOUNTRY = ‘$P!{COUNTRY}’
GROUP BY ORDERS.SHIPPEDDATE, ORDERS.SHIPCITY

SRImg7

5) Drag the Map Element from the Palette. And edit the Chart elements as follows

SRImg8

6) Save the SubReport1

3.0 CREATING SUBREPORT 2

1) For the ease of build save the SubReport1 as SubReport2 and Change the Map to HTML Column Map.

2) With Chart Properties as follows

SRImg9

3) Save SubReport2

4.0 JOINING THE SUBREPORTS TO MAIN REPORT

This is a tricky part, please follow each step.
1) Open the main Report and Create a new parameter field called SUBREPORT_DIR with following properties.

SRImg10

P.S : Is for prompting is unchecked
Class is java.lang.String
Default Value Expression should be the Workspace location where we saved all our reports. In my case C:\Users\181415\JaspersoftWorkspace\MyReports\
Note that in Java application we need to use “\\” as shown below

SRImg11

2) Drag Subreport Element from the Palette and Place it where you need this report to be displayed.

3) It will open the subReport wizard. In the first window

SRImg12

a. choose “Select an existing Report”
b. Click Select a report
c. Choose SubReport1 from the box

SRImg13

d. Click Next
e. In the Connection window use the option “ Use same connection to fill the master Report”

SRImg14

Click Next

f. In the SubReport Parameter Window, select “Copy from main Report”

SRImg15

g. Delete the Parameter SUBREPORT_DIR from the list. Click Finish

4) Repeat the same steps from 1 to 3 to all the SubReports you want to join to main report.

5) At this Point, if we click on Preview to run the report, we will get the below mentioned error.

SRImg16

This error is due to the fact that the Jaspersoft Engine is not able to find your subreport file in the specified location. By Default the location expression value is just “SubReport1.Jasper”

SRImg17

6) This is where we need to use the parameter we created SUBREPORT_DIR to specify the location. This is shown in the image below. (Note the “SubReport1.jasper” has been edited to “SubReport.jrxml” as the Report engines reads it as JRXML File.)

SRImg18

This will also generate the same error , because the compile manager has not been called in.

7) Hence the complete Subreport Expression is as follows
net.sf.jasperreports.engine.JasperCompileManager.compileReport($P{SUBREPORT_DIR} + “SubReport1.jrxml”)

SRImg19

5.0 RUNNING THE MAIN REPORT

1) Now Click Preview to Run the report

2) Report will ask to Provide the Parameter Value. In my case its COUNTRY and I have provide the value “Brazil”

SRImg20

SRImg21

3) This Parameter value internally gets added to the Sub Reports at the runtime and two charts with Brazil data will be displayed.

SRImg22

5.0 FINAL NOTES (IMP)

1) Saving the Main Report and Sub Reports in the same path
2) Creating the correct Sub Report Expression as shown above.