kostepanych
kostepanych

Reputation: 2619

Divide report result into two columns

I want to divide report results into two columns to get an output (using Jasper Studio):

result row 1 result row 4

result row 2 result row 5

result row 3

According to this topic I've set Column Count value as 2 and Print Order as Horizontal. But in this case I get an output:

result row 1 result row 2

result row 3 result row 4

result row 5

When I set a Print Order to Vertical, I get:

result row 1

result row 2

result row 3

result row 4

result row 5

And only if I set small Page height I get required output. But number of rows is always different. So I can't hardcode that small page height. Is it possible to calculate it somehow?

So how to get required output?

Here is an simple jrxml example:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test2" columnCount="2" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="481b7288-0681-43c8-b039-9e95ef42c274">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter (12)"/>
<queryString language="jsonql">
    <![CDATA[array]]>
</queryString>
<field name="result" class="java.lang.String">
    <property name="net.sf.jasperreports.jsonql.field.expression" value="result"/>
    <fieldDescription><![CDATA[result]]></fieldDescription>
</field>
<columnHeader>
    <band height="61" splitType="Stretch">
        <staticText>
            <reportElement x="29" y="0" width="100" height="30" uuid="2d1cbfb0-be49-4f6e-8cd2-1e641ae6e21b">
                <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c95f114a-b4bc-439b-aff0-23268822dc5d"/>
            </reportElement>
            <text><![CDATA[result]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="125" splitType="Stretch">
        <textField>
            <reportElement x="29" y="10" width="100" height="30" uuid="e9e3c344-22bd-4614-9adf-8bbd7a69bf96">
                <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c95f114a-b4bc-439b-aff0-23268822dc5d"/>
            </reportElement>
            <textFieldExpression><![CDATA[$F{result}]]></textFieldExpression>
        </textField>
    </band>
</detail>
</jasperReport>

JSON data adapter was used in this example. Here is content of the data adapter json file: {"array": [ { "ind": 1, "result": "row 1" }, { "ind": 2, "result": "row 2" }, { "ind": 3, "result": "row 3" }, { "ind": 4, "result": "row 4" }, { "ind": 5, "result": "row 5" } ] }

UPD: I've found the solution. I've divided report into 2 subreports: one subreport is the left column, other is right column. In those subreports I check $V{REPORT_COUNT} and compare it with numberOfRecords/2 in printWhenExpression to devide results into two parts.

Upvotes: 1

Views: 2194

Answers (1)

Nikhil Patil
Nikhil Patil

Reputation: 2540

Here is the example of how you can do it if you have count in dataset. Also note that it will only work if report ends in a single page -

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.15.0.final using JasperReports Library version 6.15.0-dd49bfb94918336b8321d5507193f0169ead4e95  -->
<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="test2" columnCount="2" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="481b7288-0681-43c8-b039-9e95ef42c274">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="SampleDB"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <queryString>
        <![CDATA[select result, count
from sampletable]]>
    </queryString>
    <field name="result" class="java.lang.Long">
        <property name="com.jaspersoft.studio.field.name" value="result"/>
        <property name="com.jaspersoft.studio.field.label" value="result"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="sampletable"/>
    </field>
    <field name="count" class="java.lang.Integer">
        <property name="com.jaspersoft.studio.field.name" value="count"/>
        <property name="com.jaspersoft.studio.field.label" value="count"/>
    </field>
    <group name="Group1" isStartNewColumn="true">
        <groupExpression><![CDATA[$F{count}%2 == 0 ? ($V{REPORT_COUNT}  <= $F{count}/2) : (($V{REPORT_COUNT} - 1) <= $F{count}/2)]]></groupExpression>
    </group>
    <columnHeader>
        <band height="49" splitType="Stretch">
            <staticText>
                <reportElement x="29" y="0" width="100" height="30" uuid="2d1cbfb0-be49-4f6e-8cd2-1e641ae6e21b">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c95f114a-b4bc-439b-aff0-23268822dc5d"/>
                </reportElement>
                <text><![CDATA[result]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="48" splitType="Stretch">
            <textField>
                <reportElement x="29" y="10" width="100" height="30" uuid="e9e3c344-22bd-4614-9adf-8bbd7a69bf96">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c95f114a-b4bc-439b-aff0-23268822dc5d"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{result}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Below are the changes -

  1. Add count to result (Here I am using database dataset with count in query, but you do any other as long as you can get total count to use in report) -

enter image description here

  1. Now, edit report format to add 2 columns and keep the print order as vertical -

enter image description here

  1. Then, create a group in the report with group expression as - $F{count}%2 == 0 ? ($V{REPORT_COUNT} <= $F{count}/2) : (($V{REPORT_COUNT} - 1) <= $F{count}/2), here $F{count} is the total record count from the dataset and $V{REPORT_COUNT} is the inbuilt variable to get the row number.

enter image description here

  1. Finally, check Start New Column in the Group Band Properties -

enter image description here

This would divide rows equally into 2 columns as long it is a single page.


Alternatively, you can sort your records (it would be a very complex sort) and then set the report page format as 2 column and set print order to horizontal.

Upvotes: 0

Related Questions