Crosstabs (cross-tabulation) reports are the reports containing tables that tabulate the data across rows and columns. This feature was introduced in JasperReports 1.1. The following example illustrates the use of crosstabs in a report. The JRXML template will generate a report displaying a table containing the number of aircraft in each city of the state of New York. The last column of the table will display the total number of aircraft for all models in each city. The last row will display the total number of aircraft of each model in the table. To avoid having an unmanageable number of columns in the table, we will limit the report to aircraft models that start with the letter "C".
Sometimes we would like to display related charts or crosstabs for similar data grouped differently. For example, in the previous section, we generated a crosstab displaying the total number of aircraft of a particular set of models in the state of New York. We can display the same set of data for different states by using subdatasets. The following example illustrates how to do this:
Adding hyperlinks and anchors to reports
JasperReports allows us to add hyperlinks and anchors to our reports. The only report elements that can be hyperlinks or anchors are text fields, charts, and images. Hyperlinks allow us to quickly navigate between different report sections, a feature that is very useful when producing long reports. The following example illustrates how to add hyperlinks to our reports:
Handling very large reports
Sometimes, when filling a report, the report datasource may have a lot of data. In some cases, the generated report can become very large, and in some cases larger than the memory allocated for the JVM, causing an OutOfMemoryException
.
It is possible to set up JasperReports so that it stores segments of a report on the disk in order to free some memory. This can be accomplished by using a built-in report parameter REPORT_VIRTUALIZER
. The value for this parameter must be an instance of a class implementing net.sf.jasperreports.engine.JRVirtualizer
. JasperReports comes with an implementation of this interface, namely net.sf.jasperreports.engine.fill.JRFileVirtualizer
. This implementation is sufficient to handle the vast majority of the large reports. If, for some reason, this implementation is not sufficient for our needs, we can always create our own implementation of net.sf.jasperreports.engine.JRVirtualizer
. The following example illustrates typical usage of JRVirtualizer...
In this chapter, we discussed several features that allow us to create elaborate reports. We learned to render localized reports by using the resourceBundle
attribute of the <jasperReport>
JRXML element. We then used scriptlets to add complex functionality to our reports, including variable value modification and performance measurement. We saw how to add cross-tabulation tables (crosstabs) to our reports by taking advantage of the <crosstab>
JRXML element and display related charts or crosstabs for each record in a report by using subdatasets. To ease the task of report navigation, we learned how to add hyperlinks, anchors, and bookmarks to our reports. We have also seen how we can safely generate reports larger than the available memory by taking advantage of report virtualization.