Adding Groups
Now, suppose you want to group employees by service, and calculate a subtotal of salaries for each service. The first thing to do is to define a report group via the "View/Report Groups" menu (see
Figure 12).
The most important field here is 'Group Expression'. Each time this expression changes, a new group generates. So, to group records by service, use the service name field ("$F{serv_name}"). As you would expect, for this to work property, you must correctly order the SQL query ordering ("order by serv_name,...").
When you add a new group, you get two new sections: "serviceHeader" and "serviceFooter". These sections are generated at the start and end of each service group, respectively. Reposition the static column headers into the "serviceHeader" section, and place the $F{serv_name} field just above these columns to act as a group heading.
Now create a new variable called service_salary_subtotal, as illustrated in Figure 13. It is similar to the previous variable, but with two important differences: Reset Type is 'Group', and Reset Group is 'service', meaning that the variable will be reset to zero at the start of each new service group.
Drag this variable into the "serviceFooter" section to display the subtotal of all salaries for each group. The layout should look something like the one in Figure 14. The generated report should look something like Figure 15.
Reports with Charts
In JasperReports 1.0.1 and iReport 0.5.1, you can design reports with charts. Suppose you want to add a pie chart that shows the relative salary costs of each service to the end of your report. You would have to put it in the Summary section. You may need to enlarge it a bit so that a decent-sized graph can fit. Then add a new chart in this section using the "Edit/Insert Element/Chart" menu item or the "Chart tool" icon. Choose a pie chart.
Click on the new chart, and go to the 'Chart' tab. Once there, click on the 'Edit Chart Properties' button and go to the 'Chart data' tab (see Figure 16).
Chart parameters are different for each type of chart. The 'Chart data' tab for a pie chart has three zones:
- Key Expression: Identifies each slice. Enter "$F{serv_name}" for each slice to represent a service.
- Value Expression: Enter "$V{service_salary_subtotal}" to associate the total salary cost of each service.
- Label Expression: The label to display for each slice. Enter "$F{serv_name}" to display the service name.
Now run the report. You should get a pie chart at the end of your report (see Figure 17).