How to print two phone numbers with spaces in the same row using jasper reports? [duplicate] - jasper-reports

This question already has answers here:
formatting a string to a currency format in jasper report
(8 answers)
Closed 7 years ago.
I need to print two numbers in the same row with spaces or commas as
442233378,556664446 in this way I need to print using jasper reports.I am using the variable as $F(alias_name) to print these numbers.But it is printing out of the page as shown in the image it has to print the number above the line where the first number is printed.Please help me.

I think you get get a solution for this by just changing your query.
I have added a GROUP_CONCAT function onto your alias_name field, you can read more on what this does here.
SELECT
GROUP_CONCAT(DISTINCT alias_name SEPARATOR ', ') AS alias_name
FROM
service_alias
WHERE
service_id IN
(
SELECT
id
FROM
service
WHERE
order_id IN
(
SELECT
id
FROM
purchase_order
WHERE
id IN
(
SELECT
order_id
FROM
order_process
WHERE
invoice_id = $P{invoiceId}
)
AND
period_id != 1
)
)
Here is the query added into the entityDetails.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="Entity details" pageWidth="595" pageHeight="875" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" >
<property name="com.jasperassistant.designer.GridHeight" value="12"/>
<property name="com.jasperassistant.designer.GridWidth" value="12"/>
<property name="com.jasperassistant.designer.SnapToGrid" value="false"/>
<property name="com.jasperassistant.designer.Grid" value="false"/>
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="invoiceId" class="java.lang.Integer"/>
<queryString>
<![CDATA[
SELECT
GROUP_CONCAT(DISTINCT alias_name SEPARATOR ', ') AS alias_name
FROM
service_alias
WHERE
service_id IN
(
SELECT
id
FROM
service
WHERE
order_id IN
(
SELECT
id
FROM
purchase_order
WHERE
id IN
(
SELECT
order_id
FROM
order_process
WHERE
invoice_id = $P{invoiceId}
)
AND
period_id!=1
)
)
]]>
</queryString>
<field name="alias_name" class="java.lang.String"/>
<title>
<band splitType="Stretch"/>
</title>
<detail>
<band height="62">
<textField isBlankWhenNull="true">
<reportElement x="50" y="5" width="197" height="15" />
<textElement>
<font size="10" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{alias_name}]]></textFieldExpression>
</textField>
<image>
<reportElement x="18" y="4" width="18" height="13" />
<imageExpression><![CDATA["/opt/apps/openbrm-2.0/openbrm/resources/logos/phone-symbol.png"]]></imageExpression>
</image>
</band>
</detail>
</jasperReport>

Related

How to send parameters from master report to chart (Line graph) in Jasper?

I am generating a report having a table and a Line graph.
The master report dataset returns 8 records. So, I need 8 pages with each page displaying data for each record. I have mapped the field from master dataset to both the table and line graph's dataset.
The table is displayed 8 times perfectly with the correct param value.
But, the line graph always shows data for the last parameter value.
Jrxml Content:
<?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="chart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="66962ca4-5579-453f-a9fe-5d7284566dc1">
<subDataset name="chartDS" uuid="74b2bdf1-5e99-43ec-94c6-f6c999ea449c">
<parameter name="deviceId" class="java.lang.Long"/>
<queryString language="SQL">
<![CDATA[SELECT
ticket.descr,
IFNULL(SUM(diff)/60, 0) as duration,
DATE(ticket.created_date) as date
FROM
(SELECT
t.id, t.created_date, t.restored_date, E.descr,TIMESTAMPDIFF(SECOND,t.created_date,t.restored_date) as diff
FROM
ticket t
Inner JOIN (SELECT DISTINCT
description AS descr
FROM
alarm_config
WHERE
enabled = 1
AND site_location_device_id = $P{deviceId} ) E ON t.description LIKE CONCAT('%', E.descr, '%')
AND t.site_location_device_id = $P{deviceId}
ORDER BY E.descr) ticket
GROUP BY ticket.descr, DATE(ticket.created_date)
ORDER BY DATE(ticket.created_date)]]>
</queryString>
<field name="descr" class="java.lang.String"/>
<field name="duration" class="java.math.BigDecimal"/>
<field name="date" class="java.sql.Date"/>
</subDataset>
<queryString language="SQL">
<![CDATA[SELECT
sl.location_name,sld.id as deviceId ,sld.name
FROM
site
INNER JOIN
site_location sl ON site.id = sl.site_id
INNER JOIN
site_location_device sld ON sl.id = sld.site_location_id
where site_id = 32 and sld.device_type_id = (SELECT id FROM device_type WHERE name LIKE 'HSS')
]]>
</queryString>
<field name="location_name" class="java.lang.String"/>
<field name="deviceId" class="java.lang.Long"/>
<field name="name" class="java.lang.String"/>
<detail>
<band height="273" splitType="Stretch">
<barChart>
<chart evaluationTime="Report">
<reportElement x="20" y="23" width="514" height="200" uuid="53f820c6-7e46-4052-bcdf-65d3a4b9cf15"/>
<chartTitle>
<titleExpression><![CDATA[$F{deviceId}]]></titleExpression>
</chartTitle>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<dataset>
<datasetRun subDataset="chartDS" uuid="94e17f1e-b5a7-4b22-9864-481fc0adf56b">
<datasetParameter name="deviceId">
<datasetParameterExpression><![CDATA[$F{deviceId}]]></datasetParameterExpression>
</datasetParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>
</dataset>
<categorySeries>
<seriesExpression><![CDATA[$F{descr}]]></seriesExpression>
<categoryExpression><![CDATA[$F{date}]]></categoryExpression>
<valueExpression><![CDATA[$F{duration}]]></valueExpression>
</categorySeries>
</categoryDataset>
<barPlot>
<plot labelRotation="54.0"/>
<itemLabel/>
<categoryAxisFormat labelRotation="54.0">
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</valueAxisFormat>
</barPlot>
</barChart>
</band>
</detail>
</jasperReport>
PS: Both table and graph are in the detail section.
Also, I am trying this from eclipse - report designer plugin
Finally, I managed to solve the problem.
The issue was with the evaluation time of the chart's expression.
It was by mistake set to "Report".I changed it to "Now".
Replaced this
<chart evaluationTime="Report">
with below
<chart evaluationTime="Now">
evaluationTime − Determines when the chart's expression will be evaluated. Values could be Now, Report, Page, Column, Group, Band. The default value is Now

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 to add indentation on bullet list?

I want to add some indentation to my text field. I've already added a bullet style (adding the <li> tag to the text) but it seems that JasperSoft Studio doesn't automatically indentate the text.
Even if setting the "styled" markup and adding a <ul> tag at the top of the paragraph like this:
"<ul><li> Example text 1... </li><li> Example text 2... </li></ul>"
JasperSoft is still not able to manage the <ul> tag (looking at the documentation, unfortunately, also other HTML tags..) so the result is not what I wanted.
At this point I tried to search on different forums and I found that I could create a manual report style and apply that to my text field (following this guide: Indentation in generated PDF using JasperReports) but niether this worked for me.
I also tried to put a manual configuration, thinking that creating a style could not be comfortable for Jaspersoft, and set to the text field a configuration like this:
So, setting as left padding property the 10 px value, all the text should get a small displacement to the right. Setting the first line indentation property at -10 px the first line of the paragraph should automatically get to the 0 px position, based on the margin of the page.
This should be all my indentation, and it seems that JasperSoft is understanding this because of this:
Printing the page the result that I get is completely different.
Does anyone know what I do forget?
This would be a quick way to make a bullet list without using attribute tag html, that you have correctly defined as not well supported by jasper-reports.
Use the unicode symbol \u2022 to represent the bullet, with the following example text (\n is line break)
"\u2022 Some text on line 1 that gets longer to wrap\n\u2022 This is text on line 2"
Then you can set leftPadding="10" and firstLineIndent="-10" as described in Indentation in generated PDF using JasperReports
Example jrxml
<?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="Indentation" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f0ac92f3-35e5-417e-aecd-5c47be379bf8">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="44" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement x="0" y="0" width="110" height="40" uuid="3563a40d-d80e-4e09-9d84-d4f1779c1895"/>
<box topPadding="0" leftPadding="10" bottomPadding="0" rightPadding="0"/>
<textElement markup="none">
<paragraph lineSpacing="Single" firstLineIndent="-10" leftIndent="0" spacingBefore="0"/>
</textElement>
<textFieldExpression><![CDATA["\u2022 Some text on line 1 that gets longer to wrap\n\u2022 This is text on line 2"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Output
Probably however if it was me I would have converted the text to a
JRDataSource
and used the jr:list component. For an example on how to create a datasource from a String see this How to convert a , separated String to a table layout?
and if you instead are wondering how you can manipulate your text to insert the unicode see How can I replace a character with an unicode image?
Edit adding solution preferred by me which avoids firstLineIndent="-10" hack
The solution is to convert the string into a JRDatasource in this example
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList($P{testText}.split("\n")))
and then used the jr:list, with <field name="_THIS" class="java.lang.String"/>
Full jrxml
<?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="Indentation" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f0ac92f3-35e5-417e-aecd-5c47be379bf8">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="ListDataset" uuid="a6053d26-ad58-4808-ac51-76e62529d7de">
<queryString>
<![CDATA[]]>
</queryString>
<field name="_THIS" class="java.lang.String"/>
</subDataset>
<parameter name="testText" class="java.lang.String">
<defaultValueExpression><![CDATA["Some text on line 1 that gets longer to wrap\nThis is text on line 2"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="34" splitType="Stretch">
<componentElement>
<reportElement x="0" y="0" width="170" height="20" uuid="a9d62b3a-cdad-4c44-a3e6-8e7688986380"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="ListDataset" uuid="a21cd89b-dd5a-4135-90e0-a84ef83bd9dc">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList($P{testText}.split("\n")))]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="170">
<ellipse>
<reportElement x="13" y="4" width="4" height="4" backcolor="#000000" uuid="c2a202b7-ca0f-4ce4-a2d8-35b314f8e1ee">
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
</ellipse>
<textField isStretchWithOverflow="true">
<reportElement x="30" y="0" width="80" height="20" uuid="31b05be1-9d89-4928-aec8-095e15e66711"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</title>
</jasperReport>
The output is same as above, but we are not hacking with negative values which could create problems on certain exports.
Very simple approach if don't need text reflow:
Set "Markup" to none
Use \u2022 to get a bullet.
Break the text into lines manually
Put \u00a0\u00a0\u00a0 (non-breaking space) in front of each line which doesn't have a bullet. This fakes the indent pretty well.
Drawbacks:
You have to break the lines manually
Doesn't support nice spacing between paragraphs. Just can only have whole empty lines.

Pagination in bar chart with series

I have to create a bar chart which can have a large amount (around 50) of categories (X Axis). I wish to split this report into multiple pages so that I can apply pagination. I have found an answer here:
How can I generate paginated bar chart in jasper report
But the above answer discusses a bar chart with single series.
Following is a sample report which I intend to create:
Assuming we have around 50 dates in X axis, the report will become large. So I have to break this report such that I am showing a configurable amount (say 3) of categories per page. Following is my approach:
Create a group of date field
Create a variable which counts this group.
Create another group with groupExpression $V{variable1} - 1 - ( ($V{variable1} - 1) % 3 )
Following is 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="paginationBar3d" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a9331cff-3045-47b5-8399-17a89e2ac39c">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[select a.year_built, aty.description, count(*) as count
from aircraft a, aircraft_types aty
where a.aircraft_type_id = aty.aircraft_type_id
group by year_built, description order by a.year_built;]]>
</queryString>
<field name="year_built" class="java.sql.Date">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="description" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="count" class="java.lang.Long"/>
<variable name="variable1" class="java.lang.Integer" incrementType="Group" incrementGroup="yearGroup" calculation="Count">
<variableExpression><![CDATA[Boolean.TRUE]]></variableExpression>
</variable>
<group name="yearGroup">
<groupExpression><![CDATA[$F{year_built}]]></groupExpression>
</group>
<group name="splitter">
<groupExpression><![CDATA[$V{variable1} - 1 - ( ($V{variable1} - 1) % 3)]]></groupExpression>
<groupFooter>
<band height="802">
<bar3DChart>
<chart>
<reportElement uuid="7114e305-2cfb-4757-b034-b0a12dd412d9" x="0" y="0" width="555" height="802"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<dataset resetType="Group" resetGroup="splitter"/>
<categorySeries>
<seriesExpression><![CDATA[$F{description}]]></seriesExpression>
<categoryExpression><![CDATA[$F{year_built}]]></categoryExpression>
<valueExpression><![CDATA[$F{count}]]></valueExpression>
</categorySeries>
</categoryDataset>
<bar3DPlot>
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</bar3DPlot>
</bar3DChart>
</band>
</groupFooter>
</group>
</jasperReport>
This generates a single category per page. What should be the correct approach in this scenario?
This can be done using Reports Group, I have done it earlier ,
You can try this Link :-
Split a chart in to multiple pages
and just replace
$V{REPORT_COUNT} - 1 - ( ($V{REPORT_COUNT} - 1) % 3 )
with
$V{REPORT_COUNT} - 1 - ( ($V{REPORT_COUNT} - 1) % 18 )
The above report works as intended if I define yearGroup after splitter. I am not sure how ordering of groups matter.

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>