Jump to content
Changes to the Jaspersoft community edition download ×
  • Using report variables


    Giulio Toffoli
    • Product: iReport Designer

    [日本語]

    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.png.e56bd8cd7d7e61317bc2f9a6b1ee4916.png

    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.

    figure_2.png.036c6fd6d9538dcad00142e7fabb8f36.png

    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.

    figure_3.png.9dba6b4d0185146111c78f1a95768c02.png

    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 ©.

    figure_4.png.42d92487c3e9330561a51e47575fe005.png

    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.

    figure_5.png.d8e5a028c16b45b501b184956abeaf75.png

    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.

    figure_6.png.a0d9b71bfe9f5163fdbc5f740c3846d4.png

    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.

    figure_7.png.e2bd464d3d49122634ff98745517a6d5.png

    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).

    figure_8.png.f9109f61ed560fe795634a0d18d0caee.png

    figure_9.png.38903875add7192de6bd6f9ced6de4d3.png

    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).

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...