Designing a simple summary report

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.

Getting ready

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.

How to do it...

Now let's start designing a simple summary report by following the steps of this recipe:

  1. Open the 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.
    How to do it...
  2. Switch to the Preview tab. A Parameter prompt dialog will appear, which will ask you to enter the name of the customer. Type Packt Publishing as the name of the customer and press the OK button.
    How to do it...
  3. A summary report for the particular customer will appear. You will see that the summary report shows the Customer Name and the Number of Invoices, but the report is missing Total of Invoice Values and actual monthly order values.
    How to do it...
  4. Switch back to the Designer tab. Expand the Fields node from the Report Inspector window on the left of the Designer tab. The Fields node will expand to show all the fields. You will find the InvoiceValue field at the end of the list of fields. Drag-and-drop the InvoiceValue field from the Report Inspector into the Page Header section. A dialog will appear. Select The result of an aggregation function radio button and the Sum option from the combo box in the dialog and click the Ok button, as shown in the following screenshot:
    How to do it...
  5. A text field component with the expression $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:
    How to do it...
  6. Switch to the Preview tab. A Parameter prompt dialog will appear, which will ask you to enter the name of the customer. Type 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.
    How to do it...
  7. Switch back to the Designer tab. Expand the Fields node from the Report Inspector window. The Fields node will expand to show all the fields. You will find the InvoicePeriod field at the second last position, just above the InvoiceValue. Drag-and-drop the InvoicePeriod field from the Report Inspector into the InvoicePeriod1 Group Header 1 section. A dialog will appear. Select The field value radio button in the dialog and click the Ok button, as shown in the following screenshot:
    How to do it...
  8. A text field component with the expression $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:
    How to do it...
  9. While the text field component of the InvoicePeriod1 Group Header 1 section is selected, the Properties window below the Palette of components will show the properties of the Text Field component. Find the Pattern property in the Properties window and type MMM-yyyy as its value as shown in the following screenshot:
    How to do it...
  10. Similarly, drag-and-drop the InvoiceValue from the Report Inspector into the InvoicePeriod1 Group Header section of your report. A dialog will appear. Select the The result of an aggregation function radio button in the dialog and the Sum option from the combo box in the dialog. Then click the Ok button, as shown in the following screenshot:
    How to do it...
  11. A Text Field component with the expression $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:
    How to do it...
  12. Switch to the Preview tab. A Parameter prompt dialog will appear, which will ask you to enter the name of the customer. Type 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.
    How to do it...

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.246.148