Jasper Reports Report Variables - JasperReports

What are Report variables in Jasper Reports?

The objects which are built on the report expression are report variables. The report variables are used for simplifying some of the tasks like:

  • Report expressions used in the process of report template. The report variables can declare the report expressions only once.
  • On the basis of some of the expressions like count, sum, average, lowest etc., various calculations can be performed using report variables.

Variables can be referenced by new variables in the expression if they are defined in a report design. Hence the declaration order of the variables is very essential.

How Variable is declared in Jasper Reports?

A variable is declared as follows:

The element <variable> has number of attributes which are as follows:

The Name Attribute

It is mandatory to provide the name attribute of the </variable> just like parameters and fields. The name attribute facilitates the report expressions to refer the variable by the declared name.

The Class Attribute

The class name for the variable values is specified by the class attribute, whose default value is java.lang.String. The default value can be changed to any available class in the classpath either at report filling time or at report compilation time. The type-casting is taken care by the report expressions and hence there is no requirement for manula type-casting.

Calculation

The calculations that are to be performed on the variable during report filling are determined by the calculation attribute. The values for the calculation attribute are as follows:

  • Average − The variable value is the average of every non-null value of the variable expression. Valid for numeric variables only.
  • Count − The variable value is the count of non-null instances of the variable expression.
  • First − The variable value is the value of the first instance of the variable expression. Subsequent values are ignored.
  • Highest − The variable value is the highest value for the variable expression.
  • Lowest − The variable value is the lowest value for the variable expression in the report.
  • Nothing − No calculations are performed on the variable.
  • StandardDeviation − The variable value is the standard deviation of all non-null values matching the report expression. Valid for numeric variables only.
  • Sum − The variable value is the sum of all non-null values returned by the report expression.
  • System − The variable value is a custom calculation (calculating the value for that variable yourself, using the scriptlets functionality of JasperReports).
  • Variance − The variable value is the variance of all non-null values returned by evaluation of the report variable's expression.

Incrementer FactoryClass

The value of the variable at the time of filling the record on the report is determined by this attribute. The default value is the class that determines the net.sf.jasperreports.engine.fill.JRIncrementerFactory.

IncrementType

The time for recalculating the value of the variable is determined by this attribute. The values used by this attribute are:

  • Column − The variable value is recalculated at the end of each column.
  • Group − The variable value is recalculated when the group specified by incrementGroup changes.
  • None − The variable value is recalculated with every record.
  • Page − The variable value is recalculated at the end of every page.
  • Report − The variable value is recalculated once, at the end of the report.

IncrementGroup

When the variable value is recalculated, the name of the group at that time is determined by this attribute. The name is taken from the JRXML report template.

ResetType

The variable when reset, the value of that variable is determined by this attribute and uses the following vlues:

  • Column − The variable value is reset at the beginning of each column.
  • Group − The variable value is reset when the group specified by incrementGroup changes.
  • None − The variable value is never reset.
  • Page − The variable value is reset at the beginning of every page.
  • Report − The variable value is reset only once, at the beginning of the report.

ResetGroup

When the variable value is reset, the name of the group is determined by this attribute.

What are the Built-In Report Variables in Jasper Reports?

The in-built system variables that are ready to use are as follows:

S.NO
Variable Name and Description
1
PAGE_NUMBER
This variable's value is its current page number. It can be used to display both the current page number and the total number of pages using a special feature of JasperReports text field elements, theevaluationTimeattribute.
2
COLUMN_NUMBER
This variable contains the current column number.
3
REPORT_COUNT
This report variable contains the total number of records processed.
4
PAGE_COUNT
This variable contains the number of records that were processed when generating the current page.
5
COLUMN_COUNT
This variable contains the number of records that were processed when generating the current column.
6
GroupName_COUNT
The name of this variable is derived from the name of the group it corresponds to, suffixed with the _COUNT sequence. This variable contains the number of records in the current group.

Example

To the existing report template, a variable (countNumber) is added. Rach record is prefixed with a count. The revised report template (jasper_report_template.jrxml) is as follows, which is saved to C:\tools\jasperreports-5.0.1\test directory −

There is no change in the java codes for report filling. The contents of the file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\JasperReportFill.java are as given below −

The contents of the POJO file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\DataBean.java are as given below −

The contents of the file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\DataBeanList.java are as given below −

How the report is generated using Jasper Reports variables?

The above file is executed and compiled by using the ANT build process. The contents of the build.xml file are saved under C:\tools\jasperreports-5.0.1\test. The import file is to be placed in the same directory as build.xml which is extracted from the chapter Environment Setup.

Open command line window and go to the directory where build.xml is placed. Execute the command ant -Dmain-class=com.wisdomjobs.JasperReportFill

The compilation of the above code results in a JasperViewer window that appears as follows:

Report variable example

It is observed that for each record, the count is prefixed.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JasperReports Topics