By the end of this article, you should be able to:
- Add multiple columns to a report
- Group report data
- Add images and charts to a report
Creating more elaborate reports
In this section, we will modify the simple report we created in the previous article to illustrate how to add images, charts, and multiple columns to a report. We will also see how to group report data. We will perform all of these tasks graphically with iReport.
Adding images to a report
Adding static images to a report is very simple with iReport. Just drag the Image component from the Palette to the band where it will be rendered in the report.
When we drop the image component into the appropriate band, a window pops up asking us to specify the location of the image file to display.
After we select the image, we can drag it to its exact location where it will be rendered.
As we can see, adding images to a report using iReport couldn’t be any simpler.
Adding multiple columns to a report
The report we’ve been creating so far in this article (continued from the first part of this article, Graphical Report Design with iReport: Part 1, for database details, refer to Database for our reports section of Creating Dynamic Reports from Databases) contains over 11,000 records. It spans over 300 pages. As we can see, there is a lot of space between the text fields. Perhaps it would be a good idea to place the text fields closer together and add an additional column. This would cut the number of pages in the report by half.
To change the number of columns in the report, we simply need to select the root report node in the Report Inspector window at the top left and then modify its Columns property in the Properties window at the bottom right.
When we modify the Columns property, iReport automatically modifies the Column Width property to an appropriate value. We are free, of course, to modify this value if it doesn’t meet our needs.
As our report now contains more than one column, it makes sense to re-add the Column Header band we deleted earlier. This can be done by right-clicking on the band in the Report Inspector window and selecting Add Band.
Next, we need to move the static text in the page header to the Column Header band. To move any element from one band to another, all we need to do is drag it to the appropriate band in the Report Inspector window.
Next, we need to resize and reposition the text fields in the Detail band and the static text elements in the Column Header band so that they fit in the new, narrower width of the columns. Also, resize the Column Header band to avoid having too much whitespace between the elements of the Column Header and Detail bands. Our report now looks like this:
We can see the resulting report by clicking on Preview.
Grouping report data
Suppose we are asked to modify our report so that data is divided by the state where the aircraft is registered. This is a perfect situation to apply report groups. Report groups allow us to divide report data when a report expression changes.
To define a report group, we need to right-click on the root report node in the Report Inspector window, and then select Add Report Group.
Then, enter the Group name and indicate whether we want to group by a field or by a report expression. In our case, we want to group the data by state field.
After clicking on Next>, we need to indicate whether we want to add a group header and/or footer to our report.
For aesthetic purposes, we move the static text fields in the Column Header band to the Group Header band, remove the column and page header bands, and add additional information to the Group Header band. After making all of these changes, our report preview will look like this:
We can preview the report by clicking Preview.