pageWidth
|
Determines the width of the page in pixels.
|
Any non-negative integer.
|
595
|
pageHeight
|
Determines the height of the page in pixels.
|
Any non-negative integer.
|
842
|
leftMargin
|
Determines the left margin of the page in pixels.
|
Any non-negative integer.
|
20
|
rightMargin
|
Determines the right margin of the page in pixels.
|
Any non-negative integer.
|
20
|
topMargin
|
Determines the top margin of the page in pixels.
|
Any non-negative integer.
|
30
|
bottomMargin
|
Determines the bottom margin of the page in pixels.
|
Any non-negative integer.
|
30
|
orientation
|
Determines the orientation of the page.
|
Portrait, Landscape
|
Portrait
|
whenNoDataType
|
Determines how to create reports... |
JasperReports provides several ways to control text properties in the report. We can control the font, whether the text is bold, italic, underlined, its background and foreground colors, and so on.
One way JasperReports allows us to control text properties is by using the <style>
element. This element allows us to control the foreground and background colors, the style of font (bold, italic, or normal), the font size, a border for the font, and many other attributes. Styles can extend other styles and add to or override properties of the parent style.
The following JRXML template illustrates the use of styles:
Setting a report's background
Reports
can have elements that appear in the report background, behind all other report elements. We can add any report element to the background by using the JRXML <background>
element. The following JRXML template demonstrates how to do this:
Using report expressions, another feature of JasperReports, we can display calculated data on a report. Calculated data is the data that is not static and not specifically passed as a report parameter or a datasource field.
Report expressions are built from combining report parameters, fields, and static data. By default, report expressions can be built using the Java language, but JasperReports can support any other language supported by the JVM. The JasperReports project file includes examples of using BeanShell and Groovy to build report expressions.
Note
By far the most commonly used report expressions are Java expressions, so we will cover only those. Refer to the examples distributed with the JasperReports project ZIP file if you need to create expressions in BeanShell or Groovy.
We have already seen simple report expressions in the form of report parameters and fields. We can use any valid Java language expression that returns a string or a numeric value as report expressions...
Adding multiple columns to a report
JasperReports allows us to generate reports with multiple columns. Reports we have seen so far seem to have multiple columns. For example, the report we created in the previous section has a column for model, another column for tail number, and one more for serial number. However, all three of these fields are laid out in a single <band>
element.
When we add multiple columns to a report, we should think of the data inside a band as a cell, regardless of how the data is laid out inside that band.
The f
lightstats
database we used for the examples in Chapter 4, Creating Dynamic Reports from Databases, contains the country, state, and city where an aircraft is registered. Let's create a report displaying the tail number of all aircraft registered in the state of New York in the United States. Our report will display the data in three columns. The following JRXML template will generate a report with the desired layout:
JasperReports allows us to group report data in a logical manner. For example, if we were creating a report about cars, we could group the data by car make and/or model. If we were creating a report about sales figures, we could group the report data by geographical area.
The flightstats
database we used for the examples in Chapter 4, Creating Dynamic Reports from Databases, contains the country, state, and city where an aircraft is registered. Let's create a report displaying aircraft data registered in any state starting with the letter "A" in the United States. We will group the report data by state abbreviation. The JRXML template for the report is as follows:
When we wrote the report in the Report Expressions section, we had to type the following expression twice:
$F{fixed_wing_single_engine_cnt}.intValue() +$F{fixed_wing_multiple_engine_cnt}.intValue())
This expression was typed once to calculate the number of fixed-wing aircraft reported, and again to calculate the total number of aircraft reported. This duplication is not a good thing because, if we need to change the expression for any reason, we would have to do it twice. JasperReports allows us to assign report expressions to a variable, eliminating the need to type the expression multiple times. The following JRXML template is a modified version of the one we wrote in that section, this version takes advantage of report variables to eliminate the duplicate expression.
Stretching text fields to accommodate data
By default, <textField>
elements have a fixed size. If the data they need to display does not fit in their defined size, it is simply not displayed in the report. This is rarely the behavior we would want. Luckily, JasperReports allows us to alter this default behavior. This is accomplished by setting the isStretchWithOverflow
attribute of the <textField>
element to true
.
The following JRXML template demonstrates how to allow text fields to stretch so that they can accommodate large amounts of data:
Laying out report elements
As
we saw in Chapter 3, Creating your First Report, a report can contain the following sections: a report title, a page header, a page footer, a column header, a column footer, a detail section, a report summary, and a last page footer. These sections are defined by the <title>
, <pageHeader>
, <pageFooter>
, <columnHeader>
, <columnFooter>
, <detail>
, <summary>
, and <lastPageFooter>
JRXML elements, respectively.
Each of these elements contains a single <band>
element as its only subelement. The <band>
element can contain zero or more <line>
, <rectangle>
, <ellipse>
, <image>
, <staticText>
, <textField>
, <subReport>
, or <elementGroup>
subelements. Except for <elementGroup>
, each of these elements must contain a single <reportElement>
as its first element. The <reportElement>
subelement determines how data is laid out for that particular element...
Setting common element properties
JasperReports 1.1 introduced a new report element, the <frame>
element, allowing us to group elements together and give them a common look. For example, we can set the background color of the frame, and it will be inherited across all the elements contained within the frame. Frames also provide a straightforward way of placing a border around multiple report elements. The following JRXML template is a new version of the example in the previous section. It has been modified to illustrate the use of frames.
Sometimes report elements can have the same value over and over. For example, the report we created in the Grouping reportdata section is sorted by model number. For airplanes that are the same model, we see the model number repeated over and over again. Perhaps the report would be easier to read if we printed the model number only when it is different from the previous one. This will add to report readability, as the person reading the report would not have to look at the model number unless it is different from the previous one. In the following example, we will modify the JRXML template discussed in that section to accomplish this:
One
nice feature of JasperReports is that it allows incorporating a report within another report. That is, one report can be a subreport of another. Subreports allow us to keep report designs simple, as we can create many simple reports and encapsulate them into a master report.
Let's create a more detailed version of the report discussed in the previous section. This new version divides the reported aircraft into the city to which they are registered. We will create one report that displays the aircraft registered in each city for a particular state. Then, we'll use that report as a subreport for a master report that divides the aircraft by state. The JRXML template for the subreport is as follows:
In this chapter, we discussed several JasperReports features that allow us to control the layout of a report.
By setting the appropriate attributes of the <jasperReport>
JRXML element, we can control report-wide layout properties, such as margins, page width, page orientation, and others. Text properties such as size and font can be set by using report styles or by setting the appropriate attributes of the <text>
JRXML element. We can use styled text to modify the style of individual words, phrases, or sentences by setting the isStyledText
attribute of the <textElement>
JRXML element.
We learned to add multiple columns to a report by setting the columnCount
attribute of the <jasperReport>
JRXML element. We also learned to divide the report data into logical groups by using the <group>
JRXML element. The chapter also dealt with displaying dynamic data in a report by using report expressions, encapsulating report expressions using report variables, and performing...