Many times you need to display only a summary of your report data and not the actual nitty-gritty details of the data. JasperReports provides you with an easy-to-use variety of aggregation functions to use on your data. This helps you to quickly build robust summary reports.
This recipe teaches you how to use this feature of aggregation to build a simple summary report.
Refer to the installPostgreSQL.txt
file included in the source code download of this chapter to install and run PostgreSQL, which should be up and running before you proceed.
The source code of this chapter also includes a file named copySampleDataIntoPGS.txt
, which helps you to create a database named jasperdb8
and copy sample data for this recipe into the database.
Now let's start designing a simple summary report by following the steps of this recipe:
CustSummary.jrxml
file from the Task1
folder of the source code for this chapter. The Designer tab of iReport shows a report with a title, various fields in the Page Header section, and two static text components in the Column Header section. You will notice that all the other sections of the report, including the Detail 1 section, are empty. Packt Publishing
as the name of the customer and press the OK button. $V{InvoiceValue_1}
will appear in the Page Header section of your report. Using your mouse, align this text field component in front of the Total of Invoices Value: (for more clarification refer to the screenshot below this step) static text component, as shown in the following screenshot: Packt Publishing
as the name of the customer and click the OK button. You will see that the summary report this time shows the total of invoice values for Packt Publishing, as shown next. This Total of Invoice Values was missing in the previous preview of step 3. $F{InvoicePeriod}
will appear in the InvoicePeriod1 Group Header 1 section of your report. Using your mouse, align this text field component just below the Month static text component, in the Column Header section, as shown in the following screenshot: MMM-yyyy
as its value as shown in the following screenshot: $V{InvoiceValue_2}
will appear in the InvoicePeriod1 Group Header 1 section of your report. Using your mouse, align this Text Field component just below the Order Value static text component, in the Column Header section, as shown in the following screenshot: Packt Publishing
as the name of the customer and click the OK button. You will see a preview of your report, as shown next. This time you will notice that actual monthly order values are shown in the summary.Note that the Detail 1 section remained empty throughout this recipe. That's because you want to process the InvoiceValue records to generate a summary. You don't want to display individual records.
If you dropped anything into the Detail 1 section, it would display it for every record. So instead of dropping the InvoiceValue field into the Detail 1 section, in step 11, you dropped the InvoiceValue field into the InvoicePeriod1 Group Header 1 section. This section displays its data only when the InvoicePeriod (that is month) changes. As a result, only monthly records are shown instead of each individual record.
Now notice from the screenshot of step 11 that the sum of all invoices is shown in the Page Header section of your report. On the other hand, the sum of the monthly invoice values is shown in the InvoicePeriod1 Group Header 1 section in the same screenshot. How does JasperReports handle this differentiation?
iReport intelligently writes JRXML code for you. It watches which component you are dropping and into which section. Depending upon the component and section, it asks you some questions through dialogs and writes specific JRXML code for you, which meets your exact requirements.
Refer to step 4 of this recipe, where you dragged-and-dropped the InvoiceValue field into the Page Header section of your report. iReport asked you to use the actual InvoiceValue field or to perform some aggregation process on the value. You chose to sum the invoice values. As a result, iReport generated the JRXML code to sum all the invoice values for the particular customer.
Compare it with step 10 of this recipe, where you dragged-and-dropped the same InvoiceValue field into the InvoicePeriod1 Group Header 1 section and again chose to sum the invoice values. This time, iReport generated the JRXML code to sum the invoice values for the particular customer and for a particular month. That's because iReport knows that you are dropping the same InvoiceValue field into a group that is based on InvoicePeriod.
3.14.246.148