Dynamically change width of element - jasper-reports

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>

Related

How can I use 2 columns in details in Jasper Studio? [duplicate]

This is my current 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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="18199607-277f-4e05-b2ba-be2f5d89e7d5">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[SELECT
language.`id` AS language_id,
language.`name` AS language_name
FROM
`language` language]]>
</queryString>
<field name="language_id" class="java.lang.Long"/>
<field name="language_name" class="java.lang.String"/>
<title>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="662306ce-d3df-4306-b320-e89a92485da3" x="0" y="0" width="555" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true" isItalic="true" isUnderline="true" isStrikeThrough="false"/>
</textElement>
<text><![CDATA[Languages]]></text>
</staticText>
</band>
</title>
<detail>
<band height="41" splitType="Stretch">
<textField>
<reportElement uuid="1f1d2c1a-bafd-4095-9c7a-e0a48c20a82f" x="23" y="14" width="231" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="1f1d2c1a-bafd-4095-9c7a-e0a48c20a82f" x="301" y="14" width="235" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="52cb1ba3-4bdd-4b18-877e-0c40f70d073d" x="0" y="14" width="23" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="52cb1ba3-4bdd-4b18-877e-0c40f70d073d" x="278" y="14" width="23" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_id}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
and this is my output:
and I need this:
So how can I show data in two columns from a list.
You should set report's property Print order (printOrder) value as Horizontal and the report's property Columns (columnCount) value as 2.
The 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="report39" language="groovy" columnCount="2" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8ea55a1a-7e67-4906-b7be-7314b7bfa03d">
<queryString>
<![CDATA[SELECT id, name FROM PRODUCT]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<detail>
<band height="61" splitType="Stretch">
<textField>
<reportElement uuid="ea1c8668-6d75-42da-9293-6cfd81297c03" x="0" y="0" width="24" height="61"/>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="652cf497-5bc2-4b62-b47a-23ec135cbfdf" x="24" y="0" width="176" height="61"/>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The report's design (in iReport):
The result will be (via preview in iReport):

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.

JasperReports: How to show data in two columns

This is my current 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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="18199607-277f-4e05-b2ba-be2f5d89e7d5">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[SELECT
language.`id` AS language_id,
language.`name` AS language_name
FROM
`language` language]]>
</queryString>
<field name="language_id" class="java.lang.Long"/>
<field name="language_name" class="java.lang.String"/>
<title>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="662306ce-d3df-4306-b320-e89a92485da3" x="0" y="0" width="555" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true" isItalic="true" isUnderline="true" isStrikeThrough="false"/>
</textElement>
<text><![CDATA[Languages]]></text>
</staticText>
</band>
</title>
<detail>
<band height="41" splitType="Stretch">
<textField>
<reportElement uuid="1f1d2c1a-bafd-4095-9c7a-e0a48c20a82f" x="23" y="14" width="231" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="1f1d2c1a-bafd-4095-9c7a-e0a48c20a82f" x="301" y="14" width="235" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="52cb1ba3-4bdd-4b18-877e-0c40f70d073d" x="0" y="14" width="23" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="52cb1ba3-4bdd-4b18-877e-0c40f70d073d" x="278" y="14" width="23" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{language_id}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
and this is my output:
and I need this:
So how can I show data in two columns from a list.
You should set report's property Print order (printOrder) value as Horizontal and the report's property Columns (columnCount) value as 2.
The 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="report39" language="groovy" columnCount="2" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8ea55a1a-7e67-4906-b7be-7314b7bfa03d">
<queryString>
<![CDATA[SELECT id, name FROM PRODUCT]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<detail>
<band height="61" splitType="Stretch">
<textField>
<reportElement uuid="ea1c8668-6d75-42da-9293-6cfd81297c03" x="0" y="0" width="24" height="61"/>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="652cf497-5bc2-4b62-b47a-23ec135cbfdf" x="24" y="0" width="176" height="61"/>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The report's design (in iReport):
The result will be (via preview in iReport):

Jasper Report graph in iReport repeating

I have a line graph in iReport which is incrementally repeating in every page, that is, if the graph has 30 x/y pairs (this is dynamic), the report will have 30 pages, of which the first will have just one value, the 2nd two values, and so on. Only the last one will contain the complete graph.
Questions "chart repeat many time" and "Problem with charting using JasperReport" and "How to print the grand total only in the last page of a very long report?" did not solve my problem. I understand that putting the chart in the Detail section makes the chart repeat, but putting it in the Summary, Footer, lastPageFooter, Title, etc. sections makes it display only one value. Same when I put it in the Detail section and use <printWhenExpression>.
My code:
<?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="nodes-allarmipersistenti" language="groovy" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="day" class="java.lang.Number"/>
<field name="month" class="java.lang.String"/>
<field name="year" class="java.lang.String"/>
<field name="dsunita" class="java.lang.String"/>
<field name="valueNumber" class="java.lang.Number"/>
<field name="logo" class="java.lang.String"/>
<field name="today" class="java.lang.String"/>
<field name="option" class="java.lang.String"/>
<title>
<band height="89">
<staticText>
<reportElement mode="Opaque" x="0" y="67" width="424" height="22" backcolor="#CCFFFF"/>
<textElement textAlignment="Right" verticalAlignment="Middle" markup="none">
<font size="10" isBold="true" pdfFontName="Helvetica-Bold" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[GRAFICO PARAMETRO]]></text>
</staticText>
<staticText>
<reportElement x="600" y="0" width="100" height="38"/>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Stampato in data]]></text>
</staticText>
<textField>
<reportElement x="700" y="0" width="102" height="38"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{today}]]></textFieldExpression>
</textField>
<image>
<reportElement x="0" y="0" width="100" height="67"/>
<imageExpression><![CDATA[$F{logo}]]></imageExpression>
</image>
<staticText>
<reportElement x="700" y="38" width="102" height="29"/>
<textElement textAlignment="Right"/>
<text><![CDATA[MaRe - Telecontrollo]]></text>
</staticText>
<textField>
<reportElement mode="Opaque" x="424" y="67" width="378" height="22" backcolor="#CCFFFF"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[' ' + $F{option}]]></textFieldExpression>
</textField>
</band>
</title>
<lastPageFooter>
<band height="387" splitType="Stretch">
<xyLineChart>
<chart evaluationTime="Page">
<reportElement x="0" y="0" width="802" height="387"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<dataset incrementType="Report"/>
<xySeries>
<seriesExpression><![CDATA["Unità " + $F{dsunita}]]></seriesExpression>
<xValueExpression><![CDATA[$F{day}]]></xValueExpression>
<yValueExpression><![CDATA[$F{valueNumber}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
</linePlot>
</xyLineChart>
</band>
</lastPageFooter>
</jasperReport>
Datasource here is a JRBeanCollectionDataSource.
Putting it in the Detail band is wrong (as you noticed). Putting it in the Title or Summary will work. Your choice of evaluationTime="Page" doesn't look right. Try changing this to evaluationTime="Report"