如何使用JSON与Jasper Report对数据集进行分组?

时间:2016-11-17 12:23:38

标签: json jasper-reports subreport

我是Jasper Report的新手,我正在使用JSON作为数据源。我有以下数据集作为样本。

{"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"
    }

我将ShipCountry作为参数传递给我的子报表,它正常工作。

enter image description here

但是,我的模板显示两次相同的数据,因为它每次为我的子集上的每个对象循环我的数据集。 如何按国家分组我的订单?如何才能显示重复?像这样:

enter image description here

我在GitHub上的country_orders_report.jrxmlcountry_order_list.jrxml代码。 my whole data非常小:)

希望有所帮助!

1 个答案:

答案 0 :(得分:1)

这是因为您在主报表和子报表中的两个查询都有相同的结果。

在您的主报告中 - JsonCountryReport.jrxml - 您有这样的疑问:

Northwind.Orders(ShipCountry == Germany)

将产生2个结果。这意味着渲染子报表两次(对于每个查询结果)。

然后,在您的子报告 - JsonCountryOrdersReport.jrxml 中 - 您的查询几乎相同:

Northwind.Orders(ShipCountry == $P{ShipCountry})

将转换为上面的那个,因为您为ShipCountry传递了相同的值。结果现在应该变得明显。

您可能希望:

Northwind.Customers(Country == Germany)

作为主要报告查询,并将国家/地区字段传递给子报告而不是 ShipCountry

编辑:由于只能使用订单的ShipCountry,因此您可以使用 ShipCountry 组作为@AlexK提及。但是,将子报表放在groupHeader波段中是完全矫枉过正的。您已经拥有主查询中的所有必需数据,因此不需要子报表。您可以按如下方式重新构建主报告:

<?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>