Jasper Reports: chart based on two columns - jasper-reports

I am designing a report with a chart based on fields of the data set. This is the content of the dataset:
I want to add a bars chart with one bar for each entry in the dataset, so I added the following chart code:
<groupFooter>
<band height="249">
<bar3DChart>
<chart>
<reportElement x="10" y="20" width="540" height="200" uuid="532522d1-b004-47b7-9e2a-ff12ab88c7ed"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<dataset resetType="Group" resetGroup="indicador"/>
<categorySeries>
<seriesExpression><![CDATA[$F{fecha}]]></seriesExpression>
<categoryExpression><![CDATA[0]]></categoryExpression>
<valueExpression><![CDATA[$F{valor}]]></valueExpression>
</categorySeries>
</categoryDataset>
<bar3DPlot>
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</valueAxisFormat>
</bar3DPlot>
</bar3DChart>
</band>
</groupFooter>
The problem is that field date is no unique, so the chart doesn't depict the table. I want to set the tag seriesExpression to the combination of fields date and division, because such combination is unique.
How can I do it? I tried putting both fields in the same seriesExpression, but I got an error.
EDIT
I tried putting together both fields: <seriesExpression><![CDATA[$F{fecha}$F{division}]]></seriesExpression>, but I got this errors:

seriesexpression must be an Expression, so if Fecha is a Date something like
<seriesExpression><![CDATA[new SimpleDateFormat("dd/MM/yyyy").format($F{fecha})+" "+$F{division}]]></seriesExpression>

Related

Jasper Report Text Field gets cut

<band height="20">
<textField textAdjust="StretchHeight">
<reportElement positionType="Float" x="10" y="0" width="545" height="20" uuid="835e5c50-820a-48e7-aa71-b0e5fea3737f">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box bottomPadding="10">
<pen lineWidth="0.0"/>
</box>
<textElement textAlignment="Justified" markup="none">
<font fontName="Helvetica Neue" size="8.7"/>
<paragraph lineSpacing="Single"/>
</textElement>
<textFieldExpression><![CDATA["RAY ENGINEERING PRIVATE LIMITED mainly deals in products such as GENERAL CONSTRUCTION SERVICES OF HIGHWAYS, STREETS, ROADS, RAILWAYS AND AIRFIELD RUNWAYS, BRIDGES AND TUNNELS (37.32%),CONSTRUCTION SERVICES OF OTHER NON-RESIDENTIAL BUILDINGS SUCH AS EDUCATIONAL INSTITUTIONS,HOSPITALS, CLINICS INCLUDING VERTINARY CLINICS, RELIGIOUS ESTABLISHMENTS, COURTS, PRISONS, MUSEUMS AND OTHER SIMILAR BUILDINGS (16.22%),GENERAL CONSTRUCTION SERVICES OF HARBOURS, WATERWAYS, DAMS, WATER MAINS AND LINES & IRRIGATION AND OTHER WATERWORKS (12.48%)"]]></textFieldExpression>
</textField>
</band>
The whole text is not getting printed . It gets cut after OTHER .
If i add more words in the end after (12.48%) it starts displaying the whole text.
This unusual issue seems to due to a bug in jasper library as i have tried every aspect to correct this.

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

iReport pageHeader Image issue

I am using iReport 3.0.0 , presently trying to make a simple change replace image inside a pageHeader with another , but the issue is the new image doesnt fit in.
The band height is 100
old image is png format, 217 x 132 ,and it is inside image element width="190" height="70" ( and this worked fine )
new image is png format, 192 x 130 and is inside image element width="103" height="100"
The issue is for new image there is a v small portion of the image in bottom is not showing up , i changed the height from 70 to 100 , still the same issue.
Any pointers appreciated
OLD CODE IS
<pageHeader>
<band height="100" isSplitAllowed="true" >
<image evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement x="0" y="12" width="190" height="70" key="image-2"/>
<box></box>
<graphicElement stretchType="NoStretch"/>
<imageExpression class="java.lang.String"><![CDATA[$P{REPORT_BASE_DIR}+"Old_logo.png"]]></imageExpression>
</image>
</band>
</pageHeader>
NEW Code IS
<pageHeader>
<band height="100" isSplitAllowed="true" >
<image evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement x="379" y="0" width="103" height="100" key="image-2"/>
<box></box>
<graphicElement stretchType="NoStretch"/>
<imageExpression class="java.lang.String"><![CDATA[$P{REPORT_BASE_DIR}+"New_Logo.png"]]></imageExpression>
</image>
What am i doing wrong ??
Whats is your ireport version?
I tried it with the examples that IDE provides :
Example 1: https://gist.github.com/64fbf51838e648c28a76.git
<image>
<reportElement x="2" y="0" width="118" height="132"/>
<imageExpression><![CDATA["tree1.png"]]></imageExpression>
</image>
Example 2: https://gist.github.com/1a97fa35e63cabb2f8e7.git
<image>
<reportElement x="275" y="0" width="300" height="64"/>
<imageExpression><![CDATA["leaf_banner_red.png"]]></imageExpression>
</image>
I think is your <box> tag or something diferent to this examples.

How to create a single pdf containing multiple invoices

I feed an xml file to JasperReports in order to generate a single invoice. This works fine.
However, I would also like to generate multiple invoices (i.e. all invoices for a given user) from a single xml file. The output should be just a single pdf file.
The XML file looks like this
<orders>
<order>
...
</order>
<order>
...
</order>
<order>
...
</order>
</orders>
Is this possible with JasperReports by just editing the jrxml file? I know that I could generate a pdf for each invoice by calling JasperReports in a loop. But I'd like to avoid this since it would require modifing a lot of code.
Update
I use the Jasper-Rails plugin for RoR to generate the reports. https://github.com/fortesinformatica/jasper-rails/blob/master/lib/jasper-rails/jasper_reports_renderer.rb
An xml for a single invoice is simply <order> ... </order>
The jrxml (note: xPath has already been changed to reflect mkl's approach) http://pastebin.com/R0vnrQgU
It is possible to generate a single PDF with multiple invoices if it before was possible to generate a PDF for a single invoice.
It is not clear how many changes to the JRXML are required. Unfortunately the OP did not provide a JRXML. To illustrate a simple case:
If the order merely contains some static amount of data and the JRXML, therefore, only essentially works on one data set, there is hardly anything to change. e.g.:
Let's assume you have original XMLs similar to this sample:
<order>
<name>Mr. Smith</name>
<quantity>2</quantity>
<item>bike</item>
<price>200</price>
</order>
and an original JRXML similar to this sample:
<queryString language="xPath">
<![CDATA[/order]]>
</queryString>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.String">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="item" class="java.lang.String">
<fieldDescription><![CDATA[item]]></fieldDescription>
</field>
<field name="price" class="java.lang.String">
<fieldDescription><![CDATA[price]]></fieldDescription>
</field>
<variable name="aggregated" class="java.lang.Number">
<variableExpression><![CDATA[java.lang.Integer.parseInt($F{quantity}) * java.lang.Float.parseFloat($F{price})]]></variableExpression>
</variable>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="535" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Dear " + $F{name} + ","]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="40" width="100" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="40" width="235" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{item}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="335" y="40" width="100" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="75" width="535" height="35"/>
<textElement/>
<textFieldExpression><![CDATA["Please pay " + $V{aggregated} + " soon."]]></textFieldExpression>
</textField>
<textField>
<reportElement x="435" y="40" width="100" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{aggregated}]]></textFieldExpression>
</textField>
</band>
</detail>
Now for multi-invoice XMLs like this sample:
<orders>
<order>
<name>Mr. Smith</name>
<quantity>2</quantity>
<item>bike</item>
<price>200</price>
</order>
<order>
<name>Mr. Fisher</name>
<quantity>3</quantity>
<item>box</item>
<price>10</price>
</order>
</orders>
all you have to do in the JRXML is to change the xPath expression to match all order elements and add a page break between data sets:
...
<queryString language="xPath">
<![CDATA[//order]]>
</queryString>
...
<detail>
<band height="125" splitType="Stretch">
...
<break>
<reportElement x="0" y="124" width="535" height="1"/>
</break>
</band>
</detail>
Even if the order contains more dynamic data, e.g. a variable number of items, it might be just as easy as long as the order element defines the main data set (the variable number of items might be handled by some table element working on a subdataset).
Update
After the OP provided his JRXML file, it turns out that it essentially is of the simple type indicated above. A small correction is required, though:
The OP's JRXML uses only two sections, Title and Page Footer. The latter indeed is used for page footer material but the former is not used for a title but instead for the actual invoice body, i.e. for material which shall be printed not once (as a title) but as many times as there are data sets! To make this ready for multi-invoice outputs, therefore, the title band first has to be moved to become a detail band:
...
</background>
<title>
<band height="616" splitType="Stretch">
...
</band>
</title>
<pageFooter>
...
becomes
...
</background>
<detail>
<band height="616" splitType="Stretch">
...
</band>
</detail>
<pageFooter>
...
And due to the sheer size of this band, this already suffices to make the JRXML ready for multi-order inputs: A page break occurs automatically before the next detail section with information for the next data set because not more than one of these bands fits on a page and the band splitType is "Stretch".
The output into a single pdf isn't the point.
The key is how to use page breaks for each invoice and the use of the headers/bands, I think.

Just one pie in report chart if parameter values are equal

Was created pie 3d chart. It has several params. For example, one of them call "errors" and another - "success". Both is Integer. If values are different, for example 10 and 9, all work fine, but if values are equal, it draw like one pie, and in label expression writed only one value.
In this image I set 10 errors and 10 success, but as can be seen, JasperReports draw just 10 success.
I check it in iReport and by generation report in Java, but results was same.
One more example.
<parameter name="access" class="java.lang.Number"/> set 10
<parameter name="configChange" class="java.lang.Number"/> set 10
<parameter name="creating" class="java.lang.Number"/> set 5
<parameter name="deleting" class="java.lang.Number"/> set 10
<parameter name="updating" class="java.lang.Number"/> set 5
<parameter name="objRequest" class="java.lang.Number"/> set 7
<parameter name="unknown" class="java.lang.Number"/> set 8
Result:
jrxml-file:
<pie3DChart>
<chart isShowLegend="true" renderType="draw" theme="default">
<reportElement uuid="1ea2fe12-8478-48a3-8a16-828cb17bc242" positionType="FixRelativeToBottom" mode="Opaque" x="0" y="50" width="430" height="280" backcolor="#CCFFFF"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<pieSeries>
<keyExpression><![CDATA[$P{errors}]]></keyExpression>
<valueExpression><![CDATA[$P{errors}]]></valueExpression>
<labelExpression><![CDATA["Error"]]></labelExpression>
</pieSeries>
<pieSeries>
<keyExpression><![CDATA[$P{success}]]></keyExpression>
<valueExpression><![CDATA[$P{success}]]></valueExpression>
<labelExpression><![CDATA["Success"]]></labelExpression>
</pieSeries>
</pieDataset>
<pie3DPlot depthFactor="0.2">
<plot backgroundAlpha="0.1" foregroundAlpha="0.6">
<seriesColor seriesOrder="0" color="#FF0000"/>
<seriesColor seriesOrder="1" color="#0066FF"/>
</plot>
<itemLabel/>
</pie3DPlot>
</pie3DChart>