Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dynamic variable in "print when expression"?

I am using iReport for making PDF Report.
My objectives are I want to use dynamic variable (name of variable is pgnum) in my 3 objects elements, and use pgnum in "print when expression" object properties.
I have three objects elements, they are a text field, frame name’s frameA and frame name’s frameB.

I want to make something like this in the print when expression :

  • text field = print when expression : $V{pgnum} % 6 == 1
  • frameA = print when expression : $V{pgnum} % 6 == 1
  • frameB = print when expression : $V{pgnum} % 6 != 1

If I have total 14 pages in my PDF so the result I want are :

  • When the page 7 and page 13, text field and frameA will be print.
  • And the other page(except page 7 and page 13) only frameB will be print.

I was tried all the way in iReport but the pgnum always be static result, even in text field the evaluating time properties is for every page. And frame A never be print too, because I think the pgnum is always set to be zero (0) value, and never be increment. So the result is frame B always be printed in all page.

Can you help me to solve this problem using iReport? Or can you suggest or even help me using the other jasper report something like dynamic report or dynamic jasper to solve this problem ?

NB:

  • Name of the dynamic variable is pgnum.
  • My pgnum has initial value 0 (zero)
  • And has properties like this => class="java.lang.Integer" incrementType="Page" calculation="Sum"

Thanks for reading. I’m waiting for your reply soon.

Here is my jrxml file:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="deploy_details" language="groovy" pageWidth="502" pageHeight="842" columnWidth="502" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <style name="table_CH" mode="Opaque" backcolor="#999999">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <variable name="pgnum" class="java.lang.Integer" incrementType="Page" calculation="Count">
        <variableExpression><![CDATA[pgnum+1]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <detail>
        <band height="762" splitType="Stretch">
            <frame>
                <reportElement isPrintRepeatedValues="false" mode="Opaque" x="56" y="66" width="114" height="43" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement mode="Transparent" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement/>
                    <text><![CDATA[FrameA]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement isPrintRepeatedValues="false" mode="Opaque" x="185" y="66" width="114" height="43" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 != 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement mode="Transparent" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement/>
                    <text><![CDATA[FrameB]]></text>
                </staticText>
            </frame>
            <textField isStretchWithOverflow="true" evaluationTime="Page" isBlankWhenNull="true">
                <reportElement isPrintRepeatedValues="false" x="122" y="35" width="100" height="20" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <box>
                    <topPen lineWidth="3.25"/>
                    <leftPen lineWidth="3.25"/>
                    <bottomPen lineWidth="3.25"/>
                    <rightPen lineWidth="3.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA["textField"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Thanks Alex K for reply, but your answer still not solve my problem, I have tried and follow all of your order to solve this problem, but I still get the pgnum in static value (not increment).

You suggested me before about dataSource, and now I have included the problem with new jrxml and dataSource example files in testing package. But I have a new problem, the result (dummyData) which I created is always print "empty" in test.pdf, can you help me again, sir? And can you solve my primary objective before, please?

Thank you I am waiting for your reply

Here's the link: dataSource, jrxml

like image 511
Skyeres Avatar asked Dec 09 '22 12:12

Skyeres


1 Answers

Your variable's expression is wrong.

The right expression is if you wants to start calculation with 0 value and increment pgnum value by 1 for each new page:

<variable name="pgnum" class="java.lang.Integer" incrementType="Page">
    <variableExpression><![CDATA[$V{pgnum} + 1]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

It is analogue of this pseudo code:

int pgnum = 0;

// iterating thru records
// ....
    if (isNewPageStart()) {
        pgnum = pgnum + 1; 
    }

If you want to set initial value (for example with help of parameter initialValue) for pgnum and increment pgnum value by 1 for each new page, the expression will be:

<parameter name="initialValue" class="java.lang.Integer" isForPrompting="false">
    <defaultValueExpression><![CDATA[5]]></defaultValueExpression>
</parameter>

<variable name="pgnum" class="java.lang.Integer" incrementType="Page">
    <variableExpression><![CDATA[$V{pgnum} + 1]]></variableExpression>
    <initialValueExpression><![CDATA[$P{initialValue}]]></initialValueExpression>
</variable>

It is analogue of this pseudo code:

int initialValue = 5;
int pgnum = initialValue;

// iterating thru records
// ....
    if (isNewPageStart()) {
        pgnum = pgnum + 1; 
    }

You can also increase the value of pgnum by 1 for each new page with help of this expression:

<variable name="pgnum" class="java.lang.Integer" incrementType="Page" calculation="Count">
    <variableExpression><![CDATA[100]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

The variableExpression should contains any not null value. I've set 100 value, but I can set for example 5 or 9.


Information about variables

Quotes from JasperReports Ultimate Guide - topic Variables:

Variable Name
Just as for parameters and fields, the name attribute of the ‹variable› element is 
mandatory and allows referencing the variable by its declared name in report expressions.

Reset Type
The value of a report variable can change with every iteration, but it can be brought 
back to the value returned by its initial value expression at specified times during the
report-filling process. This behavior is controlled using the resetType attribute, 
which indicates when the variable should be reinitialized during the report-filling process.

There are five reset types for a variable:
* No reset: The variable will never be initialized using its initial value expression
and will only contain values obtained by evaluating the variable’s expressio (resetType="None").
* Report-level reset: The variable is initialized only once, at the beginning of the
 report-filling process, with the value returned by the variable’s initial value expression (resetType="Report").
* Page-level reset: The variable is reinitialized at the beginning of each new page (resetType="Page").
* Column-level reset: The variable is reinitialized at the beginning of each new column (resetType="Column").
* Group-level reset: The variable is reinitialized every time the group specified by 
the resetGroup attributes breaks (resetType="Group"). The default value for this attribute is resetType="Report".


Increment Type
This property lets you choose the exact moment to increment the variable. By default, 
variables are incremented with each record in the data source, but in reports with multiple 
levels of data grouping, some variables might calculate higher-level totals and would need to 
be incremented only occasionally, not with every iteration through the data source. 

This attribute uses the same values as the resetType attribute, as follows:
* Row-level increment: The variable is incremented with every record during the iteration through the data source (incrementType="None").
* Report-level increment: The variable never gets incremented during the report-filling process (incrementType="Report").
* Page-level increment: The variable is incremented with each new page (incrementType= "Page").
* Column-level increment: The variable is incremented with each new column (incrementType="Column").
* Group-level increment: The variable is incremented every time the group specified by the incrementGroup attributes breaks (incrementType="Group").

CALCULATIONS
As mentioned, variables can perform built-in types of calculations on their corresponding
expression values. The following subsections describe all the possible values for the 
calculation attribute of the  element.

Calculation Nothing
This is the default calculation type that a variable performs. It means that the variable’s
value is recalculated with every iteration in the data source and that the value returned is
obtained by simply evaluating the variable’s expression.

Calculation Count
A count variable includes in the count the non-null values returned after evaluating the
 variable’s main expression, with every iteration in the data source. Count variables must
 always be of a numeric type. However, they can have non-numeric expressions as their main
 expression since the engine does not care about the expression type, but only counts for the
 non-null values returned, regardless of their type.

Only the variable’s initial value expression should be numeric and compatible with the
 variable’s type, since this value will be directly assigned to the count variable when initialized.

Calculation DistinctCount
This type of calculation works just like the Count calculation, the only difference being
 that it ignores repeating values and counts only for distinct non-null values.

Calculation Sum
The reporting engine can sum up the values returned by the variable’s main expression if you
 choose this type of calculation; but make sure the variable has a numeric type. You cannot
 calculate the sum of a java.lang.String or java.util.Date type of report variable unless a
 customized variable incrementer is used, as explained in the “Incrementers” section later in this chapter.

Calculation Average
The reporting engine can also calculate the average for the series of values obtained by
 evaluating the variable’s expression for each record in the data source. This type of
 calculation can be performed only for numeric variables (see the following “Incrementers”
 section, later in this chapter for details). 

Calculation Lowest and Highest
Choose this type of calculation when you want to obtain the lowest or highest value in the
 series of values obtained by evaluating the variable’s expression for each data source record.

Calculation StandardDeviation and Variance
In some special reports, you might want to perform more advanced types of calculations on
 numeric expressions. JasperReports has built-in algorithms to obtain the standard deviation
 and the variance for the series of values returned by evaluation of a report variable’s expression.

Calculation System
This type of calculation can be chosen only when you don’t want the engine to calculate any
 value for your variable. That means you are calculating the value for that variable
 yourself, almost certainly using the scriptlets functionality of JasperReports.

For this type of calculation, the only thing the engine does is to conserve the value you
 have calculated yourself, from one iteration in the data source to the next.

Calculation First
When using the calculation type First, the variable will keep the value obtained after the 
first incrementation and will not change it until the reset event occurs.

Here is a simple report variable declaration that calculates the sum for a numeric report
field called Quantity:
    ‹variable name="QuantitySum" class="java.lang.Double" calculation="Sum"›
        ‹variableExpression>$F{Quantity}‹/variableExpression›
    ‹/variable›
If you want the sum of this field for each page, here’s the complete variable declaration:
    ‹variable name="QuantitySum" class="java.lang.Double" resetType="Page" calculation="Sum"›
        ‹variableExpression›$F{Quantity}‹/variableExpression›
        ‹initialValueExpression>new Double(0)‹/initialValueExpression›
    ‹/variable›
In this example, our page sum variable will be initialized with zero at the beginning of each new page.

In your case the working template will be:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="deploy_details" language="groovy" pageWidth="502" pageHeight="842" columnWidth="502" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="7e1f4be9-dd35-4266-9eb3-ca71387dda65">
    <style name="table_CH" mode="Opaque" backcolor="#999999">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <variable name="pgnum" class="java.lang.Integer" incrementType="Page">
        <variableExpression><![CDATA[$V{pgnum} + 1]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <detail>
        <band height="762" splitType="Stretch">
            <frame>
                <reportElement uuid="e15c7831-1b8e-4f62-beb2-61e268dfbd73" mode="Opaque" x="56" y="66" width="114" height="43" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement uuid="ac19b45c-2ff9-41a9-9dd4-8189e51c7bdc" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement markup="none"/>
                    <text><![CDATA[FrameA]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement uuid="983ad5a2-5d79-4b6d-b1de-41428e4c7ccb" mode="Opaque" x="185" y="66" width="114" height="43" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 != 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement uuid="bbd99be7-18c1-4e11-a77e-6ab9e5b79500" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement/>
                    <text><![CDATA[FrameB]]></text>
                </staticText>
            </frame>
            <textField isStretchWithOverflow="true" evaluationTime="Page" isBlankWhenNull="true">
                <reportElement uuid="ba06215f-ef20-463c-baf3-891c33472f72" isPrintRepeatedValues="false" x="122" y="35" width="100" height="20" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <box>
                    <topPen lineWidth="3.25"/>
                    <leftPen lineWidth="3.25"/>
                    <bottomPen lineWidth="3.25"/>
                    <rightPen lineWidth="3.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA["textField"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The result will be (via preview in iReport), for the page #1:

enter image description here

And for the page #2:

enter image description here

like image 89
Alex K Avatar answered Dec 15 '22 01:12

Alex K