How to repeat textfield by parameter in jaspersoft? - jasper-reports

I have a text for a document, that can repeat itself for more than 100 times, what I was trying to do is putting a lot of textfield's and then putting a print when expression so that only the quantity of the parameters that I gave would repeat.
The problem is that it can repeat more times than I thought. I like to pass as parameter the number of times it should repeat, not using datasource.

An easy way to repeat text would be to actually use a datasource, the JREmptyDataSource in constructor you can define how many records you like.
I will show an example using the jr:list component to repeat text, but you could use the jr:table component or a subreport instead.
Pass to this component an empty datasource with the number of records you like (hence the parameter defined):
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{NR_REPEAT})]]></dataSourceExpression>
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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="716e18ba-d975-40fa-9be2-89f43a4ab69c">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="RepeatDataset" uuid="185d5db2-6f6b-4c9a-9905-e7a554b6a8fa">
<queryString>
<![CDATA[]]>
</queryString>
</subDataset>
<parameter name="NR_REPEAT" class="java.lang.Integer">
<defaultValueExpression><![CDATA[5]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="22">
<textField>
<reportElement x="0" y="0" width="260" height="20" uuid="b0a2bf35-f015-4b85-aa87-b5587e48de10"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA["NR. REPEAT IS: " + $P{NR_REPEAT}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="31" splitType="Stretch">
<componentElement>
<reportElement x="0" y="0" width="550" height="22" uuid="0d5f8ed2-c697-4337-b0b0-2411da8cc5fa">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<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="RepeatDataset" uuid="e98a25f7-2d4e-403e-8199-0d3573bb3a3e">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{NR_REPEAT})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="22" width="550">
<textField>
<reportElement x="0" y="0" width="550" height="22" uuid="21984a90-5517-4c8b-82f0-1fe682728830">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT} + " - Hello world"]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
Output setting NR_REPEAT to 5

Related

How to use the same REPORT_COUNT in multiple tables incrementing it?

I'm using iReport 5.6 to generate reports for my JavaApp, I have three datasets where as bellow :
dataset1 query
SELECT * FROM players s WHERE s.role = 1
dataset2 query
SELECT * FROM players s WHERE s.role = 2
dataset3 query
SELECT * FROM players s WHERE s.role = 3
then I have created 3 Detail bands where I have put :
Detail 1 contains table1 that uses dataset1 : in this table I defined the headers that are the same for the other tables (2 and 3)
Detail 2 contains table2 that uses dataset2
Detail 3 contains table3 that uses dataset3
What I can't do is to add a count column using the $V{REPORT_COUNT} jasper variable for the three table that doesn't refresh the counting as I want this counting to continue the incrementation.
To achieve this you can use the possibility to specify returnValue from datasetRun (table) and then pass this to next datasetRun (table) as a datasetParameter
In example we will pass as returnValue the $V{REPORT_COUNT} (of the table) to a variabile in main report $V{currentRecordCnt}, the currentRecordCnt we will then pass to next datasetRun (table 2) and use this to increment a variabile in 2 dataset
<variable name="accRecordCnt" class="java.lang.Integer">
<variableExpression><![CDATA[$P{currentRecordCnt}+$V{REPORT_COUNT}]]></variableExpression>
</variable>
We then pass the accRecordCnt back to main report's currentRecordCnt and repeat same for table 3.
Full jrxml 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="AccuSum" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="85a44d77-bc70-4059-a88a-60aca0ef6bf0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="Dataset1" uuid="5a00c263-2028-4446-948e-d614136ec5d7">
<queryString>
<![CDATA[]]>
</queryString>
</subDataset>
<subDataset name="Dataset2" uuid="3ff8e779-2e60-403c-81f8-1d20fd04fc78">
<parameter name="currentRecordCnt" class="java.lang.Integer"/>
<queryString>
<![CDATA[]]>
</queryString>
<variable name="accRecordCnt" class="java.lang.Integer">
<variableExpression><![CDATA[$P{currentRecordCnt}+$V{REPORT_COUNT}]]></variableExpression>
</variable>
</subDataset>
<subDataset name="Dataset3" uuid="3ff8e779-2e60-403c-81f8-1d20fd04fc78">
<parameter name="currentRecordCnt" class="java.lang.Integer"/>
<queryString>
<![CDATA[]]>
</queryString>
<variable name="accRecordCnt" class="java.lang.Integer">
<variableExpression><![CDATA[$P{currentRecordCnt}+$V{REPORT_COUNT}]]></variableExpression>
</variable>
</subDataset>
<queryString>
<![CDATA[]]>
</queryString>
<variable name="currentRecordCnt" class="java.lang.Integer"/>
<detail>
<band height="54" splitType="Stretch">
<componentElement>
<reportElement x="0" y="10" width="540" height="30" uuid="c46d16ff-7b32-4481-8d89-0435f34f7b32">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Dataset1" uuid="6b893feb-3e07-4393-ae4d-30b64e3dbaf5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(5)]]></dataSourceExpression>
<returnValue fromVariable="REPORT_COUNT" toVariable="currentRecordCnt"/>
</datasetRun>
<jr:column width="160" uuid="3b6f8589-0c6d-4709-a8cd-6cf642fd4ec9">
<jr:detailCell height="30">
<textField>
<reportElement x="0" y="0" width="160" height="30" uuid="a04fcd2b-e179-4fdb-8eac-31da14721c9a"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
<band height="50">
<componentElement>
<reportElement x="0" y="10" width="540" height="30" uuid="6866e777-15b8-49a7-b00f-3ce3593c16c0">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Dataset2" uuid="f71b2649-27c0-4a30-bb68-8bc7c8a26100">
<datasetParameter name="currentRecordCnt">
<datasetParameterExpression><![CDATA[$V{currentRecordCnt}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(5)]]></dataSourceExpression>
<returnValue fromVariable="accRecordCnt" toVariable="currentRecordCnt"/>
</datasetRun>
<jr:column width="160" uuid="6e116b9f-cf9f-4a27-9e9a-892263482caf">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:detailCell height="30">
<textField>
<reportElement x="0" y="0" width="160" height="30" uuid="8cb085f1-5155-4b17-9304-c3bb616ac965"/>
<textFieldExpression><![CDATA[$V{accRecordCnt}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
<band height="50">
<componentElement>
<reportElement x="0" y="10" width="540" height="30" uuid="338527a7-5c7e-419d-ac85-658ac2d30655">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Dataset3" uuid="8c00ef2e-d328-42a8-8ba5-12bfde8225c0">
<datasetParameter name="currentRecordCnt">
<datasetParameterExpression><![CDATA[$V{currentRecordCnt}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(5)]]></dataSourceExpression>
</datasetRun>
<jr:column width="160" uuid="7e28ae8d-b64b-42fa-a5eb-4213198d1341">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:detailCell height="30">
<textField>
<reportElement x="0" y="0" width="160" height="30" uuid="05900214-f740-4742-bae3-f4a97aed073e"/>
<textFieldExpression><![CDATA[$V{accRecordCnt}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
Output (with 1 empty record)

Jasper reports: positioning element on bottom of the page

I wonder if it's possible to position dynamically growing (table) element to the bottom of the page? My table element is on detail band, just above the page footer band.
Basically in my case the position of the topmost row of the table would be dynamically changing all the time based on the amount of rows in the table. But I'm not sure if creating this kind of presentation is even possible with Jasper where the table would be basically "growing" from bottom to top where the last row of the table would be basically fixed to the bottom of the page, just above page footer. There would be no problem if the position of the table's topmost row would be always fixed and table would "grow normally" from the fixed top position towards the bottom of the page...
I tried setting the table's position type property to Fix relative to bottom, but after that the whole table disappeared completely. This was the only thing I was able to think of so far to solve my issue.
Maybe the easiest way to achieve this is to put your table in a <groupFooter> with footerPosition="StackAtBottom"
The correct way to group depends on your datasource, but let assume you have just one table creating a dummy group.
Example of dummy group with table StackAtBottom of page
<?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="Example2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ca579c38-1e4f-4993-a020-efcea9d1096e">
<style name="table"><box><pen lineWidth="1.0" lineColor="#000000"/></box></style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF"><box><pen lineWidth="0.5" lineColor="#000000"/></box></style>
<subDataset name="Table" uuid="982be61b-ae46-4404-a9a0-30ba13e8c414">
<queryString language="xPath">
<![CDATA[/report/table/entry]]>
</queryString>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="class" class="java.lang.String">
<fieldDescription><![CDATA[class]]></fieldDescription>
</field>
</subDataset>
<queryString language="xPath">
<![CDATA[/report]]>
</queryString>
<group name="dummy" footerPosition="StackAtBottom">
<groupFooter>
<band height="29">
<componentElement>
<reportElement key="table" style="table" x="0" y="0" width="360" height="20" uuid="53ea5a0e-1218-4150-ab5a-5f947e73b284"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Table" uuid="64092841-9993-4ccd-89b4-84a546c719cf">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("report/table/entry")]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="4f5b1813-a9cc-4f83-9bdb-b0d8c4299133">
<jr:detailCell style="table_TD" height="20">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="f1a97e19-e23d-40b6-ad95-10614f516db7"/>
<textFieldExpression><![CDATA[$F{class}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="98cfbe63-f865-419c-ad8f-d8af2ed706ba">
<jr:detailCell style="table_TD" height="20">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="0480f047-02ba-4ec4-b12a-ef56a3cbfee9"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</groupFooter>
</group>
<detail>
<band height="17" splitType="Stretch"/>
</detail>
</jasperReport>
Result

Calculate sum of column in JasperReports and calculate on based of that value

I have data something like below which are fetched by query for JasperReports.
The column1 has data:
12,
21,
23,
321,
23
Now I want to show the percentage in column2 for each row's value e.g for row 1
(100 * 12) / sum of (column1)
It should be done for all the rows.
How can i do that in JasperReports?
Add a variable(e.g. v_sum) to get the sum of the column(eg. EMPLOYEEID).
<variable name="v_sum" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{EMPLOYEEID}]]></variableExpression>
</variable>
And select the filed of column 2 where you want to get the % value and select property:-
Evaluation Time- Auto
Example report, you can run this with Sample Database(HSQLDB):-
<?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="76072389-4335-4fd9-b45a-111b679776c9">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[select EMPLOYEEID from orders]]>
</queryString>
<field name="EMPLOYEEID" class="java.lang.Integer"/>
<variable name="v_sum" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{EMPLOYEEID}]]></variableExpression>
</variable>
<columnHeader>
<band height="28" splitType="Stretch">
<staticText>
<reportElement uuid="00bd99b4-690f-4eea-aebe-08b0cdcfcec2" x="15" y="6" width="100" height="20"/>
<textElement/>
<text><![CDATA[EmployeeID]]></text>
</staticText>
<staticText>
<reportElement uuid="6396c710-73d9-407f-ba02-4aceb524cb75" x="172" y="6" width="100" height="20"/>
<textElement/>
<text><![CDATA[%]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="dbbdb209-dbd4-4dbb-a454-32968c31ec79" x="14" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{EMPLOYEEID}]]></textFieldExpression>
</textField>
<textField evaluationTime="Auto">
<reportElement uuid="f14e9e42-5486-4204-974c-c2c148ab73c7" x="172" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[(100*$F{EMPLOYEEID}) / $V{v_sum}]]></textFieldExpression>
</textField>
</band>
</detail>

display column header in jasper reports

Trying to create a jasper report, I can see the data when I export to xls or csv. But column header is missing, not sure whats wrong with it, following is my report. Thanks in Advance.
<?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="scireport" language="groovy" printOrder="Horizontal" pageWidth="5000" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="4960" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
<![CDATA[select PICKUP_DATE from SCI_PLDDATA_VIEW order by EVENT_TIMESTAMP DESC]]>
</queryString>
<field name="PICKUP_DATE" class="java.sql.Timestamp">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<group name="stopid">
<groupHeader>
<band height="50"/>
</groupHeader>
</group>
<columnHeader>
<band height="44" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="900" height="20"/>
<textElement>
<font size="14" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$R{jasper.PICKUP_DATE}]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="34" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="MM-dd-yyyy HH:mm" isBlankWhenNull="true">
<reportElement x="0" y="0" width="900" height="20"/>
<textElement/>
<textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{PICKUP_DATE}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
You are fetching data from database , your value will come in field. You should use $F{} instead of $R{} in column header.

Dynamically change width of element

I need to create table of contents in JasperReports.
I have already achieved this, but cant do anything with lines in TOC.
Currently they have a static width:
Chapter 1 ........ 2
Long Chapter Name ........ 3
End ........ 4
I want to stretch dotted lines to the width of the chapter name. Like here:
Chapter 1 ................. 2
Long Chapter Name ......... 3
End ....................... 4
How can I do this? Is there some 'padding' functionality in JR or should I create scriptlet for this task ? Thanks!
P.S. I'm using jasper reports 5.0.1
This wouldn't be a very neat way but I would draw many dots in your text box and hide the end using a frame to allow writing the number of the page.
<?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="table of content" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="df5894b6-0e62-4082-bdb8-3f0a1b26a2f4">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[SELECT
1 AS page,
'test' AS title
FROM
"account" account
Union
SELECT
4 AS page,
'chapter 2' AS title
FROM
"account" account
Union
SELECT
20 AS page,
'chapter 3: hello' AS title
FROM
"account" account]]>
</queryString>
<field name="page" class="java.lang.Integer"/>
<field name="title" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="2e81fd32-3b8d-4b98-b96d-6409afb8d16f" x="0" y="0" width="555" height="20"/>
<textElement>
<font isBold="true" isStrikeThrough="false"/>
</textElement>
<text><![CDATA[Table of content]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band splitType="Stretch"/>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="c8707508-946a-48ae-ae75-61810003e1db" x="0" y="0" width="555" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{title}+" .............................................................................................................................."]]></textFieldExpression>
</textField>
<frame>
<reportElement uuid="4040d203-6b49-4da2-8bcf-f3d6d48c7fd7" mode="Opaque" x="341" y="0" width="214" height="20"/>
</frame>
<textField>
<reportElement uuid="472f8f95-fdea-4cd9-b3a5-a1b9b66a8c44" mode="Opaque" x="321" y="0" width="20" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{page}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>