Jasper report with dynamic number of subreports [duplicate] - jasper-reports

I have this set of records:
colors table.
color_group | color_name | color_no
Primary | Red | 1
Primary | Blue | 3
Primary | Yellow | 2
Secondary | Green | 8
Secondary | Violet | 1
Secondary | Orange | 7
Others | Pink | 6
Others | White | 4
Others | Black | 5
I want to generate report like this.
Primary
Red 1
Blue 3
Yellow 2
Secondary
Green 8
Violet 1
Orange 7
Others
Pink 6
White 5
Black 4
how can i display it like this in jasper report?

This is solved by using group on the color_group field and adding a groupHeader band to display its value.
Example
<?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="group" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c1d9b4b7-6162-4b17-b871-3cf3b867d1ef">
<field name="color_group" class="java.lang.String"/>
<field name="color_name" class="java.lang.String"/>
<field name="color_no" class="java.lang.Integer"/>
<group name="colorGroup">
<groupExpression><![CDATA[$F{color_group}]]></groupExpression>
<groupHeader>
<band height="20">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="e98d6fc1-1ecd-4af4-8250-d8aaa497011e"/>
<textFieldExpression><![CDATA[$F{color_group}]]></textFieldExpression>
</textField>
</band>
</groupHeader>
</group>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="20" y="0" width="100" height="20" uuid="7ca1ac35-6249-4ba6-ac87-031f8d410d2e"/>
<textFieldExpression><![CDATA[$F{color_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="120" y="0" width="100" height="20" uuid="395b0cdd-4d2c-4d0b-93cd-7cad6daf3a4b"/>
<textFieldExpression><![CDATA[$F{color_no}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Result

As per my knowledge this is the process to create and use group in iReport.
*Write desired query to fetch fields.
*Go to report name -> right click -> add report group ->
specify group name and group by object(in group-criteria) -> next ->
add group-header and footer if needed(in details) -> finish.
*Use this group where ever necessary(by using 'print when group' expression
as created group).

Related

How to adjust text field with stretch with overflow

I use iReport 4.5.1 (I know it's so so old but it's at work and I don't have authority to upgrade it).
Anyway, I have a text field with following value:
$F{field1} + " — " + $F{field2}
Both field1 and field 2 are of java.lang.String type.
Since neither field1 nor field2 has consistent length, I have checked the stretch with the overflow property as TRUE. But the problem is that when I have a long text for field2 the rest of it comes below field1 while I need it begin right below field2.
Actually, it appears like:
something for field1 — something very long
for field2
While I need it like:
something for field1 — something very long
for field2
and if I put $F{field1} in a separate text field, since it doesn't have a fixed length, sometimes an extra space appears after $F{field1} or if I set its stretch with overflow property as TRUE, $F{field1} appears in two lines, which I don't like it either.
Generally, I need $F{field1} appear in one line with only one space after that and $F{field2} to split in two lines, if its length is too long, and appear as I described above.
Is there any way to do that?
Here's 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="adjust_textField" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="parameter1" class="java.lang.Integer"/>
<queryString>
<![CDATA[SELECT code, name FROM test_co1 WHERE id = $P{parameter1}]]>
</queryString>
<field name="code" class="java.lang.String"/>
<field name="name" class="java.lang.String"/>
<detail>
<band height="125" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="41" y="66" width="270" height="20"/>
<textElement verticalAlignment="Bottom">
<font fontName="Gill Sans MT" size="12" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{code} + " — " + $F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Which reads data from the following table:
CREATE TABLE public.test_co1 (
id INTEGER,
code TEXT,
name TEXT
) ;
/* Data for the 'public.test_co1' table (Records 1 - 54) */
INSERT INTO public.test_co1 ("id", "code", "name")
VALUES
(1, E'J', E'WELDING & CUTTING'),
(2, E'L', E'CONFINED SPACES'),
(6, E'N', E'COMPRESSED GAS/FUEL CYLINDERS'),
(15, E'W18', E'java code to iReportWEAPON SAFETY FOR PROP. MASTERS'),
(45, E'GD$', E'java code to iReportGENERAL SAFETY TRAINING/INJURY & ILLNESS PREVENTION PROGRAM'),
(49, E'GD$', E'java code to iReportENVIRONMENTAL SAFETY');
When you enter 45 as the parameter value, it displays it as
GD$ — java code to iReportGENERAL SAFETY
TRAINING/INJURY & ILLNESS PREVENTION
PROGRAM
while I need it as :
GD$ — java code to iReportGENERAL SAFETY
TRAINING/INJURY & ILLNESS PREVENTION
PROGRAM
Try something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0 -->
<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="adjust_textField" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f5fbc25f-d746-4a3c-bb20-80389233ba42">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="parameter1" class="java.lang.Integer"/>
<queryString>
<![CDATA[SELECT id, code, name FROM test_co1
WHERE id = $P{parameter1}]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<field name="code" class="java.lang.String"/>
<field name="name" class="java.lang.String"/>
<detail>
<band height="125" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="30" uuid="b40f6b54-cdd8-4bed-8a3f-96808e8eeecb"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{code} + " — "]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="100" y="0" width="180" height="30" uuid="db6850d8-c213-4544-8bae-cf515ae7484e"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
I haven't tested it, but the idea is to create two fields that will stretch in height with each other. The first field is right-aligned and the second field is left-aligned.
This was written using JasperReports 6.3, so the code might not work in an older version. The approach, though, is likely correct.

How do you calculate the median for a variable in iReport?

I am preparing a report in iReport, and I need to calculate the median of a field for a variable that will be used in a crosstab. I noticed there is no built in calculation type for median (just highest and lowest).
Is there any way to get the median or 50th percentile of a field in iReport?
There is no builtin median calculation in JasperReports.
A median can still be obtained by manually collecting the values in a list and then using Apache Commons Math to do the computation.
See the following example. The report also uses Apache Commons Lang to convert a wrapper array to a primitive array. To run the report, you will need to add commons-math3-x.y.z.jar and commons-lang3-x.y.jar to the classpath.
<?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="FirstJasper" columnCount="1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
<style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="8"/>
<queryString>SELECT * FROM Orders</queryString>
<field name="Freight" class="java.lang.Double"/>
<variable name="FreightList" class="java.util.List">
<variableExpression>$V{FreightList}</variableExpression>
<initialValueExpression>new java.util.ArrayList()</initialValueExpression>
</variable>
<variable name="AddFreight" class="java.lang.Boolean">
<variableExpression>$V{FreightList}.add($F{Freight})</variableExpression>
</variable>
<title>
<band height="50">
<textField evaluationTime="Report">
<reportElement x="5" y="5" width="350" height="40"/>
<textFieldExpression><![CDATA["median is " + org.apache.commons.math3.stat.StatUtils.percentile(org.apache.commons.lang3.ArrayUtils.toPrimitive((Double[]) $V{FreightList}.toArray(new Double[$V{FreightList}.size()])), 50)]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="13">
<textField pattern="0.00">
<reportElement x="5" y="0" width="350" height="11"/>
<textFieldExpression><![CDATA[$F{Freight}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

How to show number of rows on page and the total numbers of rows in report

In JasperReport's report I have been showing data using table
In column footer I want to show total number of records per page
Page 1 Showing 25 records out of 65
Page 2 Showing 50 records out of 65
Page 3 Showing 15 records out of 65
I am using column count to show number of records per page but for total records in report what should I use
Page 1 Showing 25 records out of 25
Page 2 Showing 50 records out of 50
Page 3 Showing 15 records out of 65
Which variable should I use or any condition to be added?
You can easy solve this task with help of built-in variable $V{REPORT_COUNT} and custom variable.
The working sample
The 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="no_of_rows_on_page" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="da1c0873-1b22-4592-93a1-c9ffbdcec490">
<queryString>
<![CDATA[SELECT name FROM PRODUCT]]>
</queryString>
<field name="name" class="java.lang.String"/>
<variable name="rowsOnPage" class="java.lang.Integer" resetType="Page">
<variableExpression><![CDATA[$V{rowsOnPage} + 1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="dc686db4-cb13-46c2-8420-e9ba198116e3" x="0" y="0" width="185" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="20">
<textField>
<reportElement uuid="0940bce6-118f-47bc-81f6-ae03ca55ddcb" x="205" y="0" width="112" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Showing " + $V{rowsOnPage} + " records out of "]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement uuid="b5f1da29-7ac8-40a9-889e-e306a0cd26fe" x="317" y="0" width="60" height="20"/>
<box leftPadding="2"/>
<textElement/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="fb9ca24d-844e-4921-8a79-85c2ec2d2888" x="159" y="0" width="46" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Page " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
The result
The output result generated in iReport via preview mode
How it works
I've add 3 textFields on Page Footer band:
1st one is for showing current page number. The expression is: "Page " + $V{PAGE_NUMBER} and the Evaluation Time is Now
2nd one is for showing count of rows on current page. The expression is: `"Showing " + $V{rowsOnPage} + " records out of "' and the Evaluation Time is Now
3rd one is for showing total rows in whole report. The expression is: $V{REPORT_COUNT} and the Evaluation Time is Report.
As you can see I've add custom variable rowsOnPage for counting number of rows on page.
The variable definition:
Expression: $V{rowsOnPage} + 1
Type: Integer
Initial value: 0
Reset type: Page
See details about:
Variables
Built-in variables in JasperReports Ultimate Guide
The variable "$V{REPORT_COUNT}" can solve your problem, use it at the evaluation time "report".
If you are using Ireport designing tool, then u can show total very easily by placing Page X of Y from palette.
Ireport 5.5 has palette under Window menu.

Print When Expression to print only the last element in the group

I have following data set in oracle table and want to generate CSV file from that data.
My data
CaseID Amount
1000 10
1000 20
1000 50
2000 30
2000 10
3000 30
3000 20
3000 20
It is necessary to show the following output from above data.
Required Output
CaseID Amount Aggregate
1000 10
1000 20
1000 50 80
2000 30
2000 10 40
3000 30
3000 20
3000 20 70
Aggregate column is populated by accumulating the sum of Amount according to caseID group.
Group summary should display on last element of the group. But not in the next row, should display in the same row and next column, that matching with the last element.
I tried with 'Print When Expression' for other scenarios, but unable to use with this scenario. Also I have find similar questions, but not cater with this requirement. So would like to know the possible solutions for this.
You can do something similar with evaluationTime="Auto". You can have a text element that shows the value on the last record in the group and is empty for other records. It's not the same as not printing the element at all, but you can't use the print when expression because it doesn't have delayed evaluation.
evaluationTime="Auto" uses the reset types of variables to decide the moment at which it reads the variable values. Each group has an automatically created count variable that resets with the group, and if you create a new variable that resets on each record you can use it to determine if the current record is the last record in the group.
The whole report would look something like this
<?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="report" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="jasperreports" whenResourceMissingType="Key" isIgnorePagination="true" uuid="816687ff-bb19-4f6b-a2b6-53634ce095fc">
<style name="dd" isDefault="true" fontSize="10"/>
<field name="CaseID" class="java.lang.Integer"/>
<field name="Amount" class="java.lang.Integer"/>
<variable name="AmountSum" class="java.lang.Integer" resetType="Group" resetGroup="CaseIDGroup" calculation="Sum">
<variableExpression><![CDATA[$F{Amount}]]></variableExpression>
</variable>
<variable name="GroupCurrentCount" class="java.lang.Integer" resetType="None">
<variableExpression><![CDATA[$V{CaseIDGroup_COUNT}]]></variableExpression>
</variable>
<group name="CaseIDGroup">
<groupExpression><![CDATA[$F{CaseID}]]></groupExpression>
</group>
<detail>
<band height="20">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="af7a5ea9-ffcb-4a7f-aaa8-7d5cab06a579"/>
<textFieldExpression><![CDATA[$F{CaseID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="a7e2795e-456a-4c6e-946f-8315df453b1f"/>
<textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
</textField>
<textField evaluationTime="Auto" isBlankWhenNull="true">
<reportElement x="200" y="0" width="100" height="20" uuid="afda4fcc-78fc-46f9-8c5c-2e0c4f04dfa5"/>
<textFieldExpression><![CDATA[$V{GroupCurrentCount}.equals($V{CaseIDGroup_COUNT}) ? $V{AmountSum} : null]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

Grouping records in JasperReports

I have a problem with JasperReports. I want to group the records depending on one specific column's value.
For example input data:
Name--email--PledgeType--amount
aaa--aa#yahoo.com--1--20.00
bbb--bb#yahoo.com--2--30.00
ccc--cc#gmai.com--1--35.00
ddd--dd#gmai.com--2-- 40.00
The output report will be grouped by the "PledgeType" value (1, 2, ... number):
Total for group one: 55.00
Name email amount
aaa aa#yahoo.com 20.00
ccc cc#gmai.com 35.00
------------------------------------
Total for group two: 70.00
Name email amount
bbb bb#yahoo.com 30.00
ddd dd#gmai.com 40.00
Can JasperReports solve this problem? how?
You can define grouping in JasperReports. JasperReports calculates the total for you, there is comfortable way to add groups and totals. Here an overview what you need to do in iReport.
To add the group
modify your query to order by pledgeType - JasperReports requires the data sorted according to your grouping.
right click on the report in the report inspector and choose Add Report Group.
Follow the wizard, set as group name PledgeType and choose Group by the following report object where you select the field PledgeType. Click next. Check Add the group header and click Finish.
To add the total
right click on variables in the report inspector and choose Add Variable.
In the properties panel choose this configuration: Variable class: BigDecimal, Calculation: Sum, ResetType: Group, ResetGroup PledgeType, Variable Expression: $F{amount}.
Drag & drop the variable into the group header in the report designer. Click on the field and change: Text field expression: "Total for group " + $F{PledgeType} + ": " + $V{totalPledge}, Expression Class: java.lang.String. Evaluation time: Group. Evaluation Group: PledgeType.
Info: The evaluation time decides when a variable gets evaluated, i.e. when the sum of the calculation will be shown. If you set it to group it means 'once the group processing is completed'.
Attached the generated report and the JRXML.
The JRXML is created with iReport 5.0 - however, if you follow the steps above it should work with JR v 2+
<?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="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ce08fe1c-1543-4460-8613-7f03b200082b">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[select * from
(select 'aaa' as Name, 'aa#yahoo.com' as email, 1 as PledgeType, 20.00 as amount
union select 'bbb', 'bb#yahoo.com' ,2, 30.00
union select 'ccc', 'cc#gmai.com' ,1, 35.00
union select 'ddd', 'dd#gmai.com' ,2, 40.00) tbl
order by PledgeType]]>
</queryString>
<field name="Name" class="java.lang.String"/>
<field name="email" class="java.lang.String"/>
<field name="PledgeType" class="java.lang.Long"/>
<field name="amount" class="java.math.BigDecimal"/>
<variable name="totalPledge" class="java.math.BigDecimal" resetType="Group" resetGroup="PledgeType" calculation="Sum">
<variableExpression><![CDATA[$F{amount}]]></variableExpression>
</variable>
<group name="PledgeType">
<groupExpression><![CDATA[$F{PledgeType}]]></groupExpression>
<groupHeader>
<band height="61">
<textField evaluationTime="Group" evaluationGroup="PledgeType">
<reportElement uuid="401c7b3b-af73-4d40-8982-9c1692eb7085" x="0" y="21" width="555" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Total for group " + $F{PledgeType} + ": " + $V{totalPledge}]]></textFieldExpression>
</textField>
<staticText>
<reportElement uuid="87cd0d21-014d-4e6c-a54a-006165a38414" x="0" y="41" width="185" height="20"/>
<textElement/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement uuid="bd0fc2f5-4963-4c9d-a9be-3659be06e436" x="185" y="41" width="185" height="20"/>
<textElement/>
<text><![CDATA[email]]></text>
</staticText>
<staticText>
<reportElement uuid="5d5d7ce1-5353-4f83-91b4-57725b0c922b" x="370" y="41" width="185" height="20"/>
<textElement/>
<text><![CDATA[amount]]></text>
</staticText>
</band>
</groupHeader>
</group>
<detail>
<band height="20">
<textField>
<reportElement uuid="5b325da6-7c56-4357-8808-911dad16ec53" x="0" y="0" width="185" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="0bc06b28-7b8c-4af9-997a-714d1599def1" x="185" y="0" width="185" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="e5504bb9-c3c0-4135-94c6-7ea935f97cb6" x="370" y="0" width="185" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>