RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Open Source Java Reporting with JasperReports and iReport : Page 4

JasperReports is a powerful and flexible open source reporting solution. The iReport visual designer enables you to take advantage of the full power of JasperReports without in-depth knowledge of the JasperReports native XML format.


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).

Figure 12. Adding a New Group

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,...").

Figure 13. Adding a New Group

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.

Figure 14. The Grouped Report Layout

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.

Figure 15. The Grouped Report

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.

Figure 16. Chart Design

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).

Figure 17. Chart Report

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).

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date