Mithlesh Singh February 2016

How can I show sum of columns in column header?

I have to design jasper report in iReport 5 tool

  1. I need to show sum of each column in header like in image

  2. How can I show columns vertical like in image

Desired output expected output

Is it possible to design report like this.

Answers


Bilal DEKAR February 2016

in this type of reports, you can use a crosstab, you put a field than name it monthly. Use in the columns and a field name it month in rows, and use the sum function in cells

<crosstab>
    <rowGroup name="month" width="128" totalPosition="End">
        ...
    </rowGroup>
    <columnGroup name="monthlyUse" height="66">
            ...
    </columnGroup>
    <measure name="nameMeasure" class="java.lang.Integer" calculation="Sum">
                <measureExpression><![CDATA[$F{number}]]></measureExpression>
    </measure>              
    ....                
</crosstab>

the crosstab will generate a table shwoing the monthly usage with a total row


Fabio Fantoni February 2016

if you're not familiar with crosstab, as previously suggested, you could create a subreport just to show the column totals and put it "before" the detail band (for example in the page header band).

obviously, following this way, you will access the datasource twice, and this could could be something you want to avoid, especially if time matters.

basically, a crosstab is the better solution overall, but if you need something simpler (maybe you're not familiar with iReport) or one-shot-like you could think about a subreport


Petter Friberg February 2016

Using the normal detail band and columnHeader band this is achieved by creating a variable with calculationType="sum" on the field you like to sum

See: How to sum all values in a column in Jaspersoft iReport Designer?

Then display the variable using a textField in the columnHeader band, setting evaluationTime="Report" so that variable is calculated before displaying it.

To rotate a textElement vertical use the rotation attribute (rotation="Left")

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="ReportTest" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="43c90ca5-f3c3-4dda-8423-9ff1442f90e3">
    <queryString>
        <![CDATA[select * from mytable]]>
    </queryString>
    <field name="descr" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="value" class="java.lang.Double">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <variable name="sumValue" class="java.lang.Double" calculation="Sum">
        <variableExpression><![CDATA[$F{value}]]></variableExpression>
    </variable>
    <columnHeader>
        <band height="70">
            <textField>
                <reportElement mode="Op 

Post Status

Asked in February 2016
Viewed 2,481 times
Voted 7
Answered 3 times

Search




Leave an answer