Using report variables

[日本語]

by Giulio Toffoli

Introduction

Variables are used to perform calculations. They are managed using the report inspector and the property sheet just like the parameters.

To manage the parameters, use the report inspector.

Figure 1

From here is possible to add and remove variables. To modify a variable, select it in the report inspector and use the property sheet.

Select it in the report inspector and use the property sheet.

Built-in variables

All reports contain a set of built-in variables. Their value changes during the report execution. Built-in variables include the PAGE_NUMBER, which holds the current page of the report and the REPORT_COUNT which holds the number of records currently processed. The built-in variables can not be modified or deleted.

To hide the built-in variables from the report inspector, click the filter icon at the bottom of the report inspector.

Create a total

The fastest way to create a calculation is to use drag a field inside a band (like the summary) and use the calculation wizard. The following figure shows a simple report that lists a set of countries (SHIPCOUNTRY) and the number of orders placed in that country (C).

By dragging the field C inside the summary band, iReport asks what value to show. It can be just the value of C (which in this band will be just the last value assumed by the field C) or the result of an aggregation function like the sum.

Select the sum and press ok. Running the report, we can see that the value printed is, in effect, the sum of all orders in all countries.

How does it happen? When we asked to print the sum of C, iReport created for use a variable which has been added to the variables (the name is C_1). This variable is of type Integer (since the sum is a sum of Integer values), the calculation type is Sum and the expression used to update the variable value (with the sum calculation) is $F{C} which is an expression to get the value of the field C. For each record of the report, JasperReports gets the value of the field C (actually it evaluates the variable expression), and add that value to the set of collected values to perform the calculation. The variable C_1 is displayed in a textfield in the summary band.

We used a simple wizard here, but the variable C_1 can be created from scratch adding the variable from the Variables node and configuring it as we have just seen.

Page X of Y

To add the Page X of Y in a report, just drag the Tool Page X of Y from the palette into any band (for example, in the page footer).

This tool creates two textfielda showing the same variable: PAGE_NUMBER. The first textfield shows the current page, the second the total pages of the report. This is possible because the evaluation time of the two textfield is different, in particular the first textfield has evaluation time set to Now so PAGE_NUMBER contains the value of the current page, the second to Report (at this evaluation time, JasperReports has reached the end of the report, so PAGE_NUMBER contains the last page number).

The evaluation time of a textfield is very important because allows us to print the value assumed by a variable at different times. With this idea we can put the total sum of orders seen in the previous sample in the title band and get the correct value by setting the evaluation time of that textfield to Report (this is automatically done by iReport when a field is dragged in the title and the user chooses to show the result of an aggregation function).

 

Feedback
randomness