I'm new to Jasper Report, and I'm using JSON as datasource. I have the following dataset, as a sample.
{"Northwind": {
"Customers": [
...
],
"Orders": [
{
"ShipPostalCode": 51100,
"ShippedDate": "1996-07-16",
"OrderDate": "1996-07-04",
"OrderID": 10248,
"Freight": 32.38,
"RequiredDate": "1996-08-01",
"ShipCity": "Berlin",
"ShipCountry": "Germany",
"EmployeeID": 5,
"ShipVia": 3,
"CustomerID": "ALFKI",
"ShipAddress": "59 rue de l'Abbaye",
"ShipName": "Vins et alcools Chevalier"
},
...
{
"ShipPostalCode": 44087,
"ShippedDate": "1996-07-10",
"OrderDate": "1996-07-05",
"OrderID": 10249,
"Freight": 11.61,
"RequiredDate": "1996-08-16",
"ShipCity": "Munich",
"ShipCountry": "Germany",
"EmployeeID": 6,
"ShipVia": 1,
"CustomerID": "RATTC",
"ShipAddress": "Luisenstr. 48",
"ShipName": "Martinez Gonzalez"
}
I'm passing the ShipCountry as parameter to my subreport, which is working as it should.
However, my template is showing the same data, twice, since it loops my dataset everytime for every object on my subset.
How group my orders by country ? How can I make to not show it duplicated ? Like this:
My country_orders_report.jrxml and country_order_list.jrxml code on GitHub. And my whole data, is very small :)
Hope that helps!
This happens because you have the same result for both queries in your main report and subreport.
In your main report - JsonCountryReport.jrxml - you have this query:
Northwind.Orders(ShipCountry == Germany)
that will produce 2 results. This implies rendering the subreport twice(for each query result).
Then, in your subreport - JsonCountryOrdersReport.jrxml - you have almost the same query:
Northwind.Orders(ShipCountry == $P{ShipCountry})
that will translate into the one above because you pass the same value for ShipCountry. The outcome should become obvious now.
You probably want to have:
Northwind.Customers(Country == Germany)
as your main report query with a Country field that you pass on to the subreport instead of ShipCountry.
EDIT: Since Orders' ShipCountry is only to be used, then you could use a ShipCountry group as #AlexK mentioned. However, placing the subreport in the groupHeader band is total overkill. You already have all the required data from your main query, so a subreport will not be necessary. You could restructure your main report as follows:
<?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="country_orders_report_new" pageWidth="595" pageHeight="842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50" uuid="bbe115b5-a5a0-4b39-9b73-7092dc59ab6d">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JSON Data Adapter"/>
<style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
<style name="Sans_Normal_8" style="Sans_Normal" fontSize="8"/>
<style name="Sans_Bold" fontName="DejaVu Sans" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
<style name="Sans_Bold_8" style="Sans_Bold" fontSize="8"/>
<queryString language="json">
<![CDATA[Northwind.Orders]]>
</queryString>
<field name="ShipCountry" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="ShipCountry"/>
</field>
<field name="Id" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="OrderID"/>
</field>
<field name="OrderDate" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="OrderDate"/>
</field>
<field name="ShipCity" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="ShipCity"/>
</field>
<field name="Freight" class="java.lang.Float">
<property name="net.sf.jasperreports.json.field.expression" value="Freight"/>
</field>
<sortField name="ShipCountry"/>
<variable name="OrderNumber" class="java.lang.Integer" resetType="Group" resetGroup="ShipCountryGroup" calculation="Count">
<variableExpression><![CDATA[0]]></variableExpression>
</variable>
<variable name="TotalFreight" class="java.lang.Float" resetType="Group" resetGroup="ShipCountryGroup" calculation="Sum">
<variableExpression><![CDATA[$F{Freight}]]></variableExpression>
</variable>
<group name="ShipCountryGroup" isReprintHeaderOnEachPage="true">
<groupExpression><![CDATA[$F{ShipCountry}]]></groupExpression>
<groupHeader>
<band height="21">
<textField>
<reportElement style="Sans_Bold" x="5" y="5" width="100" height="15" isPrintWhenDetailOverflows="true" uuid="0aaeed6e-7ba1-4ab9-be59-d6ca702995fc"/>
<textFieldExpression><![CDATA[$F{ShipCountry}]]></textFieldExpression>
</textField>
<line>
<reportElement x="0" y="20" width="515" height="1" isPrintWhenDetailOverflows="true" uuid="6d76c22c-329f-4e77-a886-8580d3cb6bc1"/>
</line>
</band>
<band height="14">
<frame>
<reportElement mode="Opaque" x="0" y="2" width="356" height="10" forecolor="#CCFFFF" backcolor="#CCFFFF" uuid="e9af134f-31eb-48be-bd9b-292188f2554f"/>
<staticText>
<reportElement style="Sans_Bold_8" mode="Opaque" x="0" y="0" width="48" height="10" backcolor="#CCFFFF" uuid="62e5e770-7b05-4ecd-a254-ab0c7f643a37"/>
<textElement textAlignment="Right"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement style="Sans_Bold_8" mode="Opaque" x="54" y="0" width="87" height="10" backcolor="#CCFFFF" uuid="c472f825-47f4-4e16-a782-cc4b02572cb0"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Order Date]]></text>
</staticText>
<staticText>
<reportElement style="Sans_Bold_8" mode="Opaque" x="146" y="0" width="108" height="10" backcolor="#CCFFFF" uuid="89b5edba-1606-4f5d-89cb-144042c1fcdd"/>
<text><![CDATA[Ship City]]></text>
</staticText>
<staticText>
<reportElement style="Sans_Bold_8" mode="Opaque" x="259" y="0" width="92" height="10" backcolor="#CCFFFF" uuid="e7c6fbe3-ecb2-4c65-83d6-7c813448cec6"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Freight]]></text>
</staticText>
</frame>
</band>
</groupHeader>
<groupFooter>
<band height="14">
<frame>
<reportElement mode="Opaque" x="0" y="2" width="356" height="10" forecolor="#33CCCC" backcolor="#33CCCC" uuid="084cfbb4-f390-4302-8bf5-2e65b34829b8"/>
<staticText>
<reportElement style="Sans_Bold_8" mode="Opaque" x="160" y="0" width="67" height="10" backcolor="#33CCCC" uuid="06753d49-aed5-46c8-be06-b107f81d7c2f"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Total :]]></text>
</staticText>
<textField>
<reportElement style="Sans_Bold_8" mode="Opaque" x="227" y="0" width="27" height="10" backcolor="#33CCCC" uuid="d8edf4da-1e47-45ec-bbf3-f63b3bf0b93b"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{OrderNumber}]]></textFieldExpression>
</textField>
<textField pattern="¤ #,##0.00">
<reportElement style="Sans_Bold_8" mode="Opaque" x="259" y="0" width="92" height="10" backcolor="#33CCCC" uuid="a713b487-68a2-4391-a231-9bf9aa740090"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{TotalFreight}]]></textFieldExpression>
</textField>
</frame>
</band>
</groupFooter>
</group>
<title>
<band height="50">
<line>
<reportElement x="0" y="0" width="515" height="1" uuid="fc148b4e-50df-4a12-aa14-8505a4cfa6e1"/>
</line>
<staticText>
<reportElement style="Sans_Normal" x="0" y="10" width="515" height="30" uuid="5bf7651c-cd6b-4eaf-b65a-1413d60faab0"/>
<textElement textAlignment="Center">
<font size="22"/>
</textElement>
<text><![CDATA[Country Orders Report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="21">
<staticText>
<reportElement style="Sans_Bold" mode="Opaque" x="0" y="5" width="515" height="15" forecolor="#FFFFFF" backcolor="#333333" uuid="da0f1cad-f552-424b-bf19-b41cabbfa4ac"/>
<text><![CDATA[Country Order List]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="14">
<textField>
<reportElement style="Sans_Normal_8" x="0" y="2" width="51" height="10" uuid="ec54687d-3c95-4647-9db5-fa71a6e81009"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{Id}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" pattern="yyyy, MMM dd">
<reportElement style="Sans_Normal_8" positionType="Float" x="54" y="2" width="87" height="10" uuid="a112ba7b-c321-467c-91ec-ffb513c23338"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{OrderDate}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement style="Sans_Normal_8" positionType="Float" x="146" y="2" width="108" height="10" uuid="6a61edb3-239e-4791-a046-a6459343ac07"/>
<textFieldExpression><![CDATA[$F{ShipCity}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" pattern="¤ #,##0.00">
<reportElement style="Sans_Normal_8" positionType="Float" x="259" y="2" width="92" height="10" uuid="61a8a117-6a43-46a7-9b96-10c5beb578ab"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{Freight}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="40">
<line>
<reportElement x="0" y="10" width="515" height="1" uuid="1371178a-a590-4616-affe-a4e1a24bcc84"/>
</line>
<textField>
<reportElement x="200" y="20" width="80" height="15" uuid="3eb302d8-0855-4f82-a666-3c9628dce372"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="280" y="20" width="75" height="15" uuid="86f46fca-dbcb-4a60-b2f7-f8da6a4224f7"/>
<textFieldExpression><![CDATA[" " + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
Related
I am using jaspersoft studio 6.6.0 for my report, and i have json as a datasource for my report, in my report there is a field to display next port name based on condition of current port, but i am not able to display that, my json is as like this
"routingLocs": [
{
"callOrder": 1,
"locCode": "ZWTHJ",
"isCurrentLoc": "N"
},
{
"callOrder": 2,
"locCode": "TRYAR",
"isCurrentLoc": "Y"
},
{
"callOrder": 3,
"locCode": "AUABP",
"isCurrentLoc": "N"
},
{
"callOrder": 4,
"locCode": "RAJPI",
"isCurrentLoc": "N"
}
]
now i have to display locCode of object which is next to object that has isCurrentLoc = "Y", for this i have created one field with expression as
<![CDATA[routingLocs..locCode(^.isCurrentLoc == "Y")]]>
This expression is printing "TRYAR" instead of "AUABP"
My field definition in JRXML to display the value is
<field name="nextPort" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression">
<![CDATA[routingLocs..locCode(^.isCurrentLoc == "Y")]]>
</property>
<fieldDescription><![CDATA[nextPort]]></fieldDescription>
</field>
please help me to fix this expression
Thanks in advance for any kind of support
You cannot land on a sibling element with a jsonql query, but you can filter the JSON in a separate subDataset to achieve what you want.
Provided there is some order in your JSON data, like the ascending order of the callOrder, you can start creating your main dataset with your filtering query, something like:
<queryString language="jsonql">
<![CDATA[routingLocs.*(isCurrentLoc == "Y")]]>
</queryString>
Then create a subDataset that accepts a MAIN_CALL_ORDER parameter and that will use the same JSON data that your main does:
<subDataset name="nextPortDataset" uuid="a8b8a64f-4c6b-4c28-91db-e10f28cef52c">
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
<parameter name="MAIN_CALL_ORDER" class="java.lang.Integer"/>
<queryString language="jsonql">
<![CDATA[routingLocs.*(callOrder > $P{MAIN_CALL_ORDER})[0]]]>
</queryString>
<field name="locCode" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="locCode"/>
<fieldDescription><![CDATA[locCode]]></fieldDescription>
</field>
</subDataset>
Here, the query will produce the first item that has the callOrder greater than the one sent from the main dataset.
Then this dataset should be linked to a list that will be passed the parameter like so:
<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="nextPortDataset" uuid="55c7173b-0b04-4d4f-bd80-48cfe1ef620c">
<datasetParameter name="MAIN_CALL_ORDER">
<datasetParameterExpression><![CDATA[$F{mainCallOrder}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:listContents height="30" width="460">
<textField>
<reportElement x="230" y="0" width="115" height="30" uuid="213ab052-9cdc-419a-8d3d-45db3bf6ef90"/>
<textFieldExpression><![CDATA[$F{locCode}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
A basic working JRXML would look 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" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e9573346-c7d0-4b7a-a838-d2ff5b7b9512">
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
<subDataset name="nextPortDataset" uuid="a8b8a64f-4c6b-4c28-91db-e10f28cef52c">
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
<parameter name="MAIN_CALL_ORDER" class="java.lang.Integer"/>
<queryString language="jsonql">
<![CDATA[routingLocs.*(callOrder > $P{MAIN_CALL_ORDER})[0]]]>
</queryString>
<field name="callOrder" class="java.lang.Integer">
<property name="net.sf.jasperreports.json.field.expression" value="callOrder"/>
<fieldDescription><![CDATA[callOrder]]></fieldDescription>
</field>
<field name="locCode" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="locCode"/>
<fieldDescription><![CDATA[locCode]]></fieldDescription>
</field>
<field name="isCurrentLoc" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="isCurrentLoc"/>
<fieldDescription><![CDATA[isCurrentLoc]]></fieldDescription>
</field>
</subDataset>
<queryString language="jsonql">
<![CDATA[routingLocs.*(isCurrentLoc == "Y")]]>
</queryString>
<field name="mainCallOrder" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="callOrder"/>
<fieldDescription><![CDATA[mainCallOrder]]></fieldDescription>
</field>
<field name="mainLocCode" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="locCode"/>
<fieldDescription><![CDATA[mainLocCode]]></fieldDescription>
</field>
<field name="mainIsCurrentLoc" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="isCurrentLoc"/>
<fieldDescription><![CDATA[mainIsCurrentLoc]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="32" splitType="Stretch">
<staticText>
<reportElement x="115" y="0" width="115" height="30" uuid="4ec73015-1c44-441c-a80e-bff448923a29">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
</reportElement>
<text><![CDATA[callOrder]]></text>
</staticText>
<staticText>
<reportElement x="230" y="0" width="115" height="30" uuid="018825e6-aa24-4a11-85a4-760fa6cf913c">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cc7977d7-a541-4d3b-81d8-97be3451e130"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<text><![CDATA[locCode]]></text>
</staticText>
<staticText>
<reportElement x="345" y="0" width="115" height="30" uuid="16e13486-0a34-4c65-b512-00a4678c65f7">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="82e0773c-3e7d-4372-a44c-93a4bfcfd6b0"/>
</reportElement>
<text><![CDATA[isCurrentLoc]]></text>
</staticText>
<staticText>
<reportElement x="0" y="2" width="60" height="30" uuid="ad4e4685-2696-472c-a3b6-4a9961aeb6dc">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
</reportElement>
<text><![CDATA[Main Dataset]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="110" splitType="Stretch">
<textField>
<reportElement x="115" y="0" width="115" height="30" uuid="3bffc0f6-fb80-443a-b06d-ebbe2b669388">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
</reportElement>
<textFieldExpression><![CDATA[$F{mainCallOrder}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="230" y="0" width="115" height="30" uuid="1267f406-8492-4dd1-859a-a37f24ac545a">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cc7977d7-a541-4d3b-81d8-97be3451e130"/>
</reportElement>
<textFieldExpression><![CDATA[$F{mainLocCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="345" y="0" width="115" height="30" uuid="522f3a23-a2b4-443d-9ca5-9abbd54a07df">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="82e0773c-3e7d-4372-a44c-93a4bfcfd6b0"/>
</reportElement>
<textFieldExpression><![CDATA[$F{mainIsCurrentLoc}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="0" y="80" width="560" height="30" uuid="859af22b-90ec-41fb-acf0-36d4acf2b90a"/>
<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="nextPortDataset" uuid="55c7173b-0b04-4d4f-bd80-48cfe1ef620c">
<datasetParameter name="MAIN_CALL_ORDER">
<datasetParameterExpression><![CDATA[$F{mainCallOrder}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:listContents height="30" width="560">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="6db9b791-aaac-45e9-af0f-34546254b9e9"/>
<text><![CDATA[Filtering List]]></text>
</staticText>
<textField>
<reportElement x="115" y="0" width="115" height="30" uuid="ee03e48b-38dd-4814-ad7d-2549810c0991">
<property name="com.jaspersoft.studio.unit.x" value="px"/>
</reportElement>
<textFieldExpression><![CDATA[$F{callOrder}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="230" y="0" width="115" height="30" uuid="213ab052-9cdc-419a-8d3d-45db3bf6ef90"/>
<textFieldExpression><![CDATA[$F{locCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="345" y="0" width="115" height="30" uuid="a8ea32ea-150f-4d1f-8945-18c2b287c157"/>
<textFieldExpression><![CDATA[$F{isCurrentLoc}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
and produce the following output:
This sample needs a file-based data adapter that is linked to both main dataset and subDataset with this property:
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
If you don't have a file-based data adapter, you could create one and export it to file from the Repository Explorer inside Jaspersoft Studio.
JasperReports generate null to table cell if this value is not defined in table scope, but defined globally.
At the top of the report I have defined variables:
<subDataset name="Dataset1" uuid="c145c0b0-641e-4a32-8e07-265189715ef9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="data\JSONDdapterNew.xml"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="groupName" class="java.lang.String">
<fieldDescription><![CDATA[groupName]]></fieldDescription>
</field>
<field name="elementName" class="java.lang.String">
<fieldDescription><![CDATA[elementName]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<fieldDescription><![CDATA[elementValue]]></fieldDescription>
</field>
</subDataset>
Then I pass they to the Jasper table (markup simplified without dropping sense):
<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="db1f69ee-c0db-4ece-aacb-a181465bdc79">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("elements")]]></dataSourceExpression>
</datasetRun>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{elementName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{elementValue}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
Example for input json:
[
{
"groupName" : "Group1",
"elements" : [
{
"elementName" : "el11",
"elementValue" : "evl1"
},
{
"elementName" : "el12",
"elementValue" : "evl2"
}
]
},
{
"groupName" : "Group2",
"elements" : [
{
"elementName" : "el21",
"elementValue" : "ev21"
},
{
"elementName" : "el22",
"elementValue" : "ev22"
}
]
}
]
As result I see values for elementName and elementValue, but for groupName I see null. I want to see something like that:
Group1 | el11 | ev11 |
Group1 | el12 | ev12 |
Group2 | el21 | ev21 |
Group2 | el22 | ev22 |
If you really must use a table element and you have it in the Detail band, you could pass the groupName as a parameter to the table dataset:
First, you would add the groupName field to the main dataset:
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="groupName" class="java.lang.String">
<fieldDescription><![CDATA[groupName]]></fieldDescription>
</field>
The subDataset would become:
<subDataset name="Dataset1" uuid="c145c0b0-641e-4a32-8e07-265189715ef9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="data\JSONDdapterNew.xml"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<parameter name="groupNameParam" class="java.lang.String"/>
<field name="elementName" class="java.lang.String">
<fieldDescription><![CDATA[elementName]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<fieldDescription><![CDATA[elementValue]]></fieldDescription>
</field>
</subDataset>
The table datasetRun would become:
<datasetRun subDataset="Dataset1" uuid="db1f69ee-c0db-4ece-aacb-a181465bdc79">
<datasetParameter name="groupNameParam">
<datasetParameterExpression><![CDATA[$F{groupName}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("elements")]]></dataSourceExpression>
</datasetRun>
And then your textFieldExpression would go from $F{groupName} to $P{groupNameParam}.
The easier solution, that does not rely on a table element, would involve switching to the newer JSONQL(stable in Jaspersoft Studio since v6.4.0) language that allows traversals up the JSON tree:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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_v3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5c8f0a97-e66c-4103-8305-10e7cefe9ca2">
<queryString language="jsonql">
<![CDATA[elements.*]]>
</queryString>
<field name="groupName" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="^^.groupName"/>
<fieldDescription><![CDATA[Group Name]]></fieldDescription>
</field>
<field name="elementName" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="elementName"/>
<fieldDescription><![CDATA[Element Name]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="elementValue"/>
<fieldDescription><![CDATA[Element Value]]></fieldDescription>
</field>
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="185" height="30" uuid="ef173e64-d7d6-4afd-abba-9ad32bf204b6">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b7c69e0c-11ed-4881-93c1-dd83cefb5d4c"/>
</reportElement>
<text><![CDATA[Group Name]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="185" height="30" uuid="cd0d90cb-2ced-4f52-becb-7e171c0e7824">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cef61be1-0d90-4575-8b39-00b300c52204"/>
</reportElement>
<text><![CDATA[Element Name]]></text>
</staticText>
<staticText>
<reportElement x="370" y="0" width="185" height="30" uuid="7bee32cf-b618-489d-bb81-014d410ba074">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9a5a388b-f4ea-48dd-9b4f-82a7c58b2c22"/>
</reportElement>
<text><![CDATA[Element Value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="185" height="30" uuid="6866d5da-c4a4-4ae3-af9f-19d29e3d28dc">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b7c69e0c-11ed-4881-93c1-dd83cefb5d4c"/>
</reportElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="0" width="185" height="30" uuid="87262f8a-1548-44a3-887e-e916aa551eab">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cef61be1-0d90-4575-8b39-00b300c52204"/>
</reportElement>
<textFieldExpression><![CDATA[$F{elementName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="370" y="0" width="185" height="30" uuid="7bf9a789-1f10-4986-abd7-aa7c57bd1b9c">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9a5a388b-f4ea-48dd-9b4f-82a7c58b2c22"/>
</reportElement>
<textFieldExpression><![CDATA[$F{elementValue}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
This is similar to previous questions, but it has me stumped. I'm using Studio 6.5.1 CE version. So I have json data in a file that starts
{
"users": {
"itemCount": 30,
"items": [{
"id": 1,
"username": "user1",
...
My main json query is blank, with a single field defined: itemCount with expression "users.itemCount".
I have a Details band table and "UserDataset" with jsonql query "users.items". There are fields "id" with expression "id", "username" with expression "username", etc. Data preview shows the fields accurately.
When running the report I do see the single itemCount field populated. The problem is the table is missing. Apparently I cannot successfully tie the Datasource to the table. I have tried all sorts of expressions, and I thought something like this should work:
<datasetRun...>
...
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_CONNECTION}).subDataSource("users.items")]]></dataSourceExpression>
</datasetRun>
However, I can never get the table populated. What am I doing wrong? (I have similar report formats with jdbc sources and I have no problem with those tables.)
I can't find any combination of query/expression that works, so I am appending the jrxml file with style info stripped out for viewing.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1 -->
<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="Users" pageWidth="792" pageHeight="612" orientation="Landscape" columnWidth="752" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="61928541-d006-4374-9164-985c6c4116c9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Users.json"/>
<subDataset name="UserDataset" uuid="effd13cd-71de-4771-b706-d68b1042b564">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Users.json"/>
<queryString language="jsonql">
<![CDATA[users.items]]>
</queryString>
<field name="id" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="id"/>
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="username" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="username"/>
<fieldDescription><![CDATA[username]]></fieldDescription>
</field>
</subDataset>
<queryString language="jsonql">
<![CDATA[]]>
</queryString>
<field name="itemCount" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="users.itemCount"/>
<fieldDescription><![CDATA[itemCount]]></fieldDescription>
</field>
<detail>
<band height="116" splitType="Stretch">
<componentElement>
<reportElement x="-9" y="50" width="770" height="62" uuid="7d7f19b0-9800-43ab-86a4-45846dae7775">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</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="UserDataset" uuid="fdfe69e2-eeac-4287-9e41-4c39610a79be">
<datasetParameter name="JSON_INPUT_STREAM">
<datasetParameterExpression><![CDATA[$P{JSON_INPUT_STREAM}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.source">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.source}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.sources">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.sources}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.date.pattern">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.date.pattern}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.number.pattern">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.number.pattern}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="JSON_LOCALE">
<datasetParameterExpression><![CDATA[$P{JSON_LOCALE}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.locale.code">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.locale.code}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="JSON_TIME_ZONE">
<datasetParameterExpression><![CDATA[$P{JSON_TIME_ZONE}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.timezone.id">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.timezone.id}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_CONNECTION}).subDataSource("users.items")]]></dataSourceExpression>
</datasetRun>
<jr:column width="30" uuid="65fed6e9-ae42-4e04-b0b4-e2c8ef4a1b27">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="30" height="30" uuid="becae13f-4af9-4884-9b38-ab19347e9455"/>
<text><![CDATA[USER_ID]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="30" height="30" uuid="8c6b26dc-c436-4e68-acd5-89c33e09dac6"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="224b44e6-a9c2-4407-891d-623c7d6b33dc">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="3c961dd1-f991-4cde-9d22-224d4f3ae86c"/>
<text><![CDATA[USERNAME]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="3acbeee1-e7a4-4f45-b54c-b10c69bf7a95"/>
<textFieldExpression><![CDATA[$F{username}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
<textField>
<reportElement x="0" y="10" width="100" height="30" uuid="683b48a8-3f4d-4a3a-9ab5-df8255699b8b"/>
<textFieldExpression><![CDATA[$F{itemCount}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="39" splitType="Stretch">
<textField>
<reportElement x="326" y="6" width="100" height="30" uuid="94b2cbaa-c1f0-4934-a304-a0c4c84486a0"/>
<textFieldExpression><![CDATA["Page" +$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
Your subDataset should have no query, like this:
<subDataset name="UserDataset" uuid="effd13cd-71de-4771-b706-d68b1042b564">
<field name="id" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="id"/>
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="username" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="username"/>
<fieldDescription><![CDATA[username]]></fieldDescription>
</field>
</subDataset>
And your table's datasetRun should only use the correct expression, like this, with nothing else in it (you were casting the REPORT_CONNECTION to JsonDataSource):
<datasetRun subDataset="UserDataset" uuid="fdfe69e2-eeac-4287-9e41-4c39610a79be">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("users.items")]]></dataSourceExpression>
</datasetRun>
I am trying to figure out how to take the average of the Time To Buildof the items.
This is the equation I have used to gather the time:
($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (24* 60 * 60 * 1000) + " days " +
(($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 60 * 1000)) % 24 + " hour(s), " +
(($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 1000)) % 60 + " minute(s)"
This are the photos of my disign and variables, along with my XML, to help explain my situation.
QUESTION: How do I fix my avgToBuild variable so that it averages all of the calculated Time To Build?
<?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="BOM Build Time" pageWidth="595" pageHeight="840" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="775a7e35-9af8-4206-a155-b05a478c35b0">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="bomNumber" class="java.lang.String">
<parameterDescription><![CDATA[BOM Number:]]></parameterDescription>
</parameter>
<parameter name="To" class="java.util.Date"/>
<parameter name="From" class="java.util.Date"/>
<queryString>
<![CDATA[SELECT
datediff(day,MO."DATECREATED",MO."DATECOMPLETED") AS dayToBuild,
COMPANY."NAME" AS COMPANY_NAME,
BOM."NUM" AS BOM_NUM,
MO."DATECREATED" AS MO_DATECREATED,
MO."NUM" AS MO_NUM,
MO."DATECOMPLETED" AS MO_DATECOMPLETED,
MOITEM."BOMID" AS MOITEM_BOMID,
MOITEM."MOID" AS MOITEM_MOID,
MOITEM."QTYTOFULFILL" AS MOITEM_QTYTOFULFILL,
BOMITEMTYPE."ID" AS BOMITEMTYPE_ID,
WO."NUM" AS WO_NUM
FROM
"BOM" BOM INNER JOIN "MOITEM" MOITEM ON BOM."ID" = MOITEM."BOMID"
INNER JOIN "MO" MO ON MOITEM."MOID" = MO."ID"
INNER JOIN "BOMITEMTYPE" BOMITEMTYPE ON MOITEM."TYPEID" = BOMITEMTYPE."ID"
INNER JOIN "WO" WO ON MOITEM."ID" = WO."MOITEMID",
"COMPANY" COMPANY
WHERE
BOM."NUM"=$P{bomNumber}
AND MO."DATECOMPLETED" BETWEEN $P{From} AND $P{To}
ORDER BY
3 ASC]]>
</queryString>
<field name="DAYTOBUILD" class="java.lang.Long"/>
<field name="COMPANY_NAME" class="java.lang.String"/>
<field name="BOM_NUM" class="java.lang.String"/>
<field name="MO_DATECREATED" class="java.sql.Timestamp"/>
<field name="MO_NUM" class="java.lang.String"/>
<field name="MO_DATECOMPLETED" class="java.sql.Timestamp"/>
<field name="MOITEM_BOMID" class="java.lang.Integer"/>
<field name="MOITEM_MOID" class="java.lang.Integer"/>
<field name="MOITEM_QTYTOFULFILL" class="java.lang.Double"/>
<field name="BOMITEMTYPE_ID" class="java.lang.Integer"/>
<field name="WO_NUM" class="java.lang.String"/>
<variable name="timeToBuild" class="java.lang.String">
<variableExpression><![CDATA[($F{MO_DATECOMPLETED}.getTime()- $F{MO_DATECREATED}.getTime()) / (24* 60 * 60 * 1000) + " days " +
(($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 60 * 1000)) % 24 + " hour(s), " +
(($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 1000)) % 60 + " minute(s)"]]></variableExpression>
</variable>
<variable name="avgTimeToBuild" class="java.lang.String" calculation="Average">
<variableExpression><![CDATA[$V{timeToBuild}]]></variableExpression>
</variable>
<title>
<band height="27" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="555" height="25" uuid="40060ef5-07fd-4875-9f81-10b65d60d11d"/>
<textElement textAlignment="Center" verticalAlignment="Top">
<font size="16" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{COMPANY_NAME}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="50">
<staticText>
<reportElement x="0" y="0" width="555" height="25" uuid="b17515a1-2396-49f1-ad96-452ce08d720e"/>
<textElement textAlignment="Center">
<font size="14"/>
</textElement>
<text><![CDATA[BOM Build Time Report]]></text>
</staticText>
<staticText>
<reportElement x="0" y="25" width="190" height="20" uuid="24836422-ce77-43c9-8268-a8f13d2d11b5"/>
<textElement textAlignment="Right">
<font size="14"/>
</textElement>
<text><![CDATA[Date Range:]]></text>
</staticText>
<textField>
<reportElement x="190" y="25" width="100" height="20" uuid="e25d36f8-e42b-4015-bc41-7bfbc9040119"/>
<textElement textAlignment="Right">
<font size="14"/>
</textElement>
<textFieldExpression><![CDATA[$P{From}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="290" y="25" width="20" height="20" uuid="45821608-63dc-459f-b2c3-41a6156b72dd"/>
<textElement textAlignment="Center">
<font size="14"/>
</textElement>
<text><![CDATA[-]]></text>
</staticText>
<textField>
<reportElement x="310" y="25" width="100" height="20" uuid="05fd3fd8-735d-402c-9d98-bdd3f9f52378"/>
<textElement>
<font size="14"/>
</textElement>
<textFieldExpression><![CDATA[$P{To}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<columnHeader>
<band height="24" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="75" height="20" uuid="b59f6065-7fc9-482d-9e08-e381ac697304"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[BOM #]]></text>
</staticText>
<staticText>
<reportElement x="80" y="0" width="100" height="20" uuid="4ec40990-bdfe-415d-a7ae-b50e315d00ef"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Start Date]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="100" height="20" uuid="032d2ae9-99fe-4c6f-8cf4-24b9c5367a44"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Finish Date]]></text>
</staticText>
<staticText>
<reportElement x="415" y="0" width="140" height="20" uuid="55b731d5-b826-4ee1-b7d2-4a83cabd4ef8"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Time To Build]]></text>
</staticText>
<staticText>
<reportElement x="290" y="0" width="50" height="20" uuid="807ee552-ea26-4891-bc72-a433aeab1866"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[QTY]]></text>
</staticText>
<staticText>
<reportElement x="345" y="0" width="50" height="20" uuid="cb39685d-f7c8-432f-9b69-454ff5fe2134"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[WO #]]></text>
</staticText>
<line>
<reportElement x="0" y="19" width="555" height="1" uuid="266ffac8-70a0-4756-9191-5de40f532d9f"/>
</line>
</band>
</columnHeader>
<detail>
<band height="24" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="75" height="20" isRemoveLineWhenBlank="true" uuid="3b256693-f18d-4ef9-89ec-7890481d1855"/>
<textFieldExpression><![CDATA[$F{BOM_NUM}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="80" y="0" width="100" height="20" isRemoveLineWhenBlank="true" uuid="7c01356e-4c95-4e15-9691-632e0b84946e"/>
<textFieldExpression><![CDATA[$F{MO_DATECREATED}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="185" y="0" width="100" height="20" isRemoveLineWhenBlank="true" uuid="516bc38e-99f3-486f-ae74-c8cfe6a5b5b1"/>
<textFieldExpression><![CDATA[$F{MO_DATECOMPLETED}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="415" y="0" width="140" height="20" isRemoveLineWhenBlank="true" uuid="aa538d4a-48ec-4b72-84ed-e9e889dcaee5"/>
<textElement>
<paragraph tabStopWidth="40"/>
</textElement>
<textFieldExpression><![CDATA[$V{timeToBuild}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="290" y="0" width="50" height="20" isRemoveLineWhenBlank="true" uuid="cf252514-6297-405d-a12e-6424332e6d10"/>
<textFieldExpression><![CDATA[$F{MOITEM_QTYTOFULFILL}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="345" y="0" width="50" height="20" isRemoveLineWhenBlank="true" uuid="6637a124-0145-4e56-b3eb-7af3bae5446e"/>
<textFieldExpression><![CDATA[$F{WO_NUM}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="22">
<staticText>
<reportElement x="0" y="0" width="150" height="20" uuid="5ac0e1eb-dc7b-4866-88aa-3c1a9c3ef4a9"/>
<textElement>
<font size="12"/>
</textElement>
<text><![CDATA[Average Time To Build:]]></text>
</staticText>
<textField>
<reportElement x="150" y="0" width="150" height="20" uuid="860dd902-9791-443e-9a9d-3dedab365b23"/>
<textFieldExpression><![CDATA[$V{avgTimeToBuild}]]></textFieldExpression>
</textField>
</band>
</summary>
You need to calculate the average of the numeric value not the String value.
Lets calculate the average in milliseconds class="java.lang.Long"
<variable name="avgTimeToBuild" class="java.lang.Long" calculation="Average">
<variableExpression><![CDATA[$F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()]]></variableExpression>
</variable>
To display apply same expression as in "Time to Build" column using the variable $V{avgTimeToBuild}, even if in this case (since it in summary band) its not nessary I would put evaluationTime="Report" to make sure it's evaluated after displaying all records (if you move it in pageFooter this would be required)
<textField evaluationTime="Report">
<reportElement x="150" y="0" width="150" height="20" uuid="860dd902-9791-443e-9a9d-3dedab365b23"/>
<textFieldExpression><![CDATA[$V{avgTimeToBuild} / (24* 60 * 60 * 1000) + " days " + ($V{avgTimeToBuild} / (60 * 60 * 1000)) % 24 + " hour(s), " +($V{avgTimeToBuild} / (60 * 1000)) % 60 + " minute(s)"]]></textFieldExpression>
</textField>
I want to create Jasper Report with Dynamic no of tables.FOR EXAMPLE:
User 1 - 5 tables
User 2 - 3 tables
User 3 - 6 tables
I tried with list option but my tables are not visible and number of pages are getting increased.
<subDataset name="tableDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37">
<queryString language="json">
<![CDATA[tableData]]>
</queryString>
<field name="description" class="java.lang.String">
<fieldDescription><![CDATA[description]]></fieldDescription>
</field>
</subDataset>
<subDataset name="listDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37">
<queryString language="json">
<![CDATA[listData]]>
</queryString>
<field name="tableData" class="java.lang.String">
<fieldDescription><![CDATA[tableData]]></fieldDescription>
</field>
</subDataset>
<queryString language="json">
<![CDATA[mainDataSet]]>
</queryString>
<field name="listData" class="java.lang.String">
<fieldDescription><![CDATA[listData]]></fieldDescription>
</field>
<componentElement>
<reportElement x="-1" y="170" width="830" height="150" uuid="ab0272db-d740-42c9-94e5-238a2cc4a63e"/>
<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="f80306fb-80fc-405b-b90d-f9fe6b6ee2e1">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("listData")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="150" width="830">
<componentElement>
<reportElement positionType="Float" x="0" y="67" width="825" height="76" uuid="6a8e89c4-d4ea-4d8f-b932-8fbb24d00725"/>
<jr:table>
<datasetRun subDataset="tableDataSet" uuid="6ab7d349-34c8-4db7-bf3c-4e638ca36f5c">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
<jr:columnHeader style="colBotLeftLine" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[DESCRIPTION]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="footerBotLeftLine" height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[Grand Total]]></text>
</staticText>
</jr:columnFooter>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="734834e8-5896-4355-9454-6b188e86bfff"/>
<textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</jr:listContents>
</jr:list>
</componentElement>
Json Data.
{mainDataSet :
[
{CompanyName : "XYZ",
listData:
[
{tableId:1,
tableName:"Table One",
tableData:
[
{description:"Table One Row Data One "},
{description:"Table One Row Data Two"}
]
},
{tableId:2,
tableName:"Table Two",
tableData:
[
{description:"Table Two Row Data One "},
{description:"Table Two Row Data Two"}
]
}
]
}
]
}
mainDataSet is my Report Data Set, listData is the data for list and tableData is the one i want to display in my tables.
NOTE
If I remove the
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
and put
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
Then Two tables are created with null values.
The query to main report let's leave it blank (and keep the mainDataSet node)
<queryString language="json"><![CDATA[]]></queryString>
Define the CompanyName field as
<field name="CompanyName" class="java.lang.String">
<fieldDescription><![CDATA[mainDataSet[0].CompanyName]]></fieldDescription>
</field>
The dataSourceExpression to pass to the jr:list
<datasetRun subDataset="listDataSet" uuid="71276e30-7777-44ae-b6d9-2087a4c51ca3">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("mainDataSet.listData")]]></dataSourceExpression>
</datasetRun>
Hence point to the mainDataSet.listData
The dataSourceExpression to pass to the table (we are now in the listData)
<datasetRun subDataset="tableDataSet" uuid="5bca90cb-1473-4ff6-82fc-1da5ae4fb44c">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
</datasetRun>