Programming Tutorials

Using Variables in JasperReports

By: MakeItSimple. in Java Tutorials on 2008-09-02  

A Report variable is a special objects build on top of an expression. Variables can be used to simplify the report design by declaring only once an expression that is heavily used throughout the report design or to perform various calculations on the corresponding expressions.

In its expression, a variable can reference other report variables, but only if those referenced variables were previously defined in the report design. So the order in which the variables are declared in a report design is important.  

As mentioned, variables can perform built-in types of calculations on their corresponding expression values like : count, sum, average, lowest, highest, variance, etc.  

A variable that performs the sum of the Quantity field should be declared like this:  

<variable name="QuantitySum" 
    class="java.lang.Double" calculation="Sum">
  <variableExpression>$F{Quantity}</variableExpression>
</variable>

For variables that perform calculation we can specify the level at which they are reinitialized. The default level is Report and it means that the variable is initialized only once at the beginning of the report and that it performs the specified calculation until the end of the report is reached. But we can choose a lower level of reset for our variables in order to perform calculation at page, column or group level.  

For example, if we want to calculate the total quantity on each page, we should declare our variable like this:  

<variable name="QuantitySum" class="java.lang.Double" 
          resetType="Page" calculation="Sum">
  <variableExpression>$F{Quantity}</variableExpression>
  <initialValueExpression>new Double(0) </initialValueExpression>
</variable>
Our variable will be initialized with zero at the beginning of each new page. 

There are also the following built-in system variables, ready to use in expressions:

PAGE_NUMBER

COLUMN_NUMBER

REPORT_COUNT

PAGE_COUNT

COLUMN_COUNT

GroupName_COUNT






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)