Monday 27 June 2016

Tip : Table Component Columns hiding based on Date Parameter in Jasper Reports (Dates Comparision in Jasper Reports)

Hi All,

In this post you will see a use case associated with Table Component in Japser Reports.

Use Case : 
Lets assume you have a table component that has 24 columns(months data).
Lets assume you have a parameter called "param_StartDate" =  "15-10-2015".
Lets assume there are 12 columns for "Unit Sales" and 12 columns for "Store Sales" alternatively.

The table component has to conditionally display "Store Sales" till last month and it has to display only "Unit Sales" starting from the current month.

i.e.,

Column1= Oct 2015 Unit Sales
Column2= Oct 2015 Store Sales

Column3 = Nov 2015 Unit Sales
Column4= Nov 2015 Store Sales

Column5= Dec 2015 Unit Sales
Column6= Dec 2015 Store Sales

Column7= Jan 2016 Unit Sales
Column8 = Jan 2016 Store Sales

Column9= Feb 2016 Unit Sales
Column10=Feb 2016 Store Sales

Column11= Feb 2016 Unit Sales
Column12=Feb 2016 Store Sales

-----------------------------------------
Column13= Mar 2015 Unit Sales
Column14= Mar 2015 Store Sales

Column15 = Apr 2015 Unit Sales
Column16= Apr 2015 Store Sales

Column17= May 2015 Unit Sales
Column18= May 2015 Store Sales

Column19= June 2016 Unit Sales ( Current Month)

Column21= Feb 2016 Unit Sales

Column23= Feb 2016 Unit Sales



Column22 and Column24 should be not displayed  as those falls in Current Month (June 2015) and next month.



Parameters Set : 
param_StartDate(java.util.Date)
Parameter1 = $P{param_StartDate} (java.util.Date)
Parameter2 = new Date() (java.util.Date)

Add below package in Report "Import" properties
<import value="org.apache.commons.lang.time.*"/>

NOTE: DateUtils and its methods are written in above package.

In the table component column headers write below expressions.

$P{Parameter1},0).getYear() = Current Month
$P{Parameter1},1).getYear() = Next Month
$P{Parameter1},2).getYear() = Next, next month.

Column1 Print When Expression on 1st Cell :

((DateUtils.addMonths($P{Parameter1},0).getYear()+1900)<($P{Parameter2}.getYear()+1900))
||
(
    (DateUtils.addMonths($P{Parameter1},0).getYear()+1900)==($P{Parameter2}.getYear()+1900)
    &&
    (DateUtils.addMonths($P{Parameter1},0).getMonth()+1)<($P{Parameter2}.getMonth()+1)
)


Column2 Print When Expression on 2nd Cell : 
((DateUtils.addMonths($P{Parameter1},1).getYear()+1900)<($P{Parameter2}.getYear()+1900))
||
(
    (DateUtils.addMonths($P{Parameter1},1).getYear()+1900)==($P{Parameter2}.getYear()+1900)
    &&
    (DateUtils.addMonths($P{Parameter1},1).getMonth()+1)<($P{Parameter2}.getMonth()+1)
)



 Column3 Print When Expression on 3rd Cell : 
((DateUtils.addMonths($P{Parameter1},2).getYear()+1900)<($P{Parameter2}.getYear()+1900))
||
(
    (DateUtils.addMonths($P{Parameter1},2).getYear()+1900)==($P{Parameter2}.getYear()+1900)
    &&
    (DateUtils.addMonths($P{Parameter1},2).getMonth()+1)<($P{Parameter2}.getMonth()+1)
)


Using  these expressions one can hide the columns on table component.


:-)

Tip : Data Set Variable Usage in Conditional Style for Table component in Jasper Reports

Hi,

Today, I wanted to have conditional colors based on aggregated value(s) of a data set on Table component.
Unless you add below property to the report the calculations can not be used for styling.

The property you have to add is :
 Property:
<property name="net.sf.jasperreports.style.evaluation.time.enabled" value="true"/>

Sample Screen shot :

JRXML
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0  -->
<!-- 2016-06-27T18:43:11 -->
<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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isIgnorePagination="true" uuid="4bdc9118-0a52-4a39-bed1-b1ee9813e230">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="foodmart"/>
    <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"/>
    <property name="net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.1" value="columnHeader"/>
    <property name="net.sf.jasperreports.export.xls.remove.empty.space.between.rows" value="true"/>
    <property name="net.sf.jasperreports.export.xls.freeze.row" value="2"/>
    <property name="net.sf.jasperreports.style.evaluation.time.enabled" value="true"/>
   
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Style1">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{totalchildren1}>=200 &&$V{totalchildren1} <=1000]]></conditionExpression>
            <style backcolor="#D43511"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{totalchildren1}>1000]]></conditionExpression>
            <style backcolor="#15C21B"/>
        </conditionalStyle>
    </style>

    <subDataset name="Dataset1" uuid="a03b4f51-6c4c-46cd-8545-7f8f7198dc1e">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="foodmart"/>
        <queryString>
            <![CDATA[SELECT state_province,member_card, SUM (total_children) totalChildren 
            FROM customer
GROUP BY state_province, member_card ORDER BY state_province ]]>
        </queryString>
        <field name="state_province" class="java.lang.String"/>
        <field name="member_card" class="java.lang.String"/>
        <field name="totalchildren" class="java.lang.Long"/>
        <variable name="totalchildren1" class="java.lang.Long" resetType="Group" resetGroup="Group1" calculation="Sum">
            <variableExpression><![CDATA[$F{totalchildren}]]></variableExpression>
        </variable>
        <group name="Group1">
            <groupExpression><![CDATA[$F{state_province}]]></groupExpression>
        </group>
    </subDataset>
    <queryString>
        <![CDATA[SELECT 1 as One ]]>
    </queryString>
    <field name="one" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <summary>
        <band height="60" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="60" uuid="71b1af30-df05-4763-a3d7-f30a00ca334a">
                    <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
                </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="Dataset1" uuid="254a3472-9e0d-4b10-9082-19e671ae41da">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="210" uuid="4e78f62c-6fa9-45c7-bf0f-148898adb85e">
                        <jr:groupHeader groupName="Group1">
                            <jr:cell height="30">
                                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                                <textField>
                                    <reportElement x="0" y="0" width="210" height="30" uuid="ff2110e4-2f89-4222-ae28-2ff85db11aec"/>
                                    <box leftPadding="5"/>
                                    <textElement textAlignment="Left" verticalAlignment="Middle">
                                        <font size="14" isBold="true"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[$F{state_province}]]></textFieldExpression>
                                </textField>
                            </jr:cell>
                        </jr:groupHeader>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement key="" x="0" y="0" width="210" height="30" uuid="b9713338-10ba-42b9-8794-786ea144c363"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font size="14" isBold="true"/>
                                </textElement>
                                <text><![CDATA[State]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30"/>
                    </jr:column>
                    <jr:column width="160" uuid="ea577695-22fb-4fa8-b7d7-55ae8f079797">
                        <jr:groupHeader groupName="Group1">
                            <jr:cell height="30"/>
                        </jr:groupHeader>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement key="" x="0" y="0" width="160" height="30" uuid="80e3a954-86db-4627-a841-7635786214a1"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font size="14" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Member Card]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="160" height="30" uuid="67a36de8-d542-4d5c-a352-d2270bb9facb"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{member_card}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="185" uuid="115b86b4-ec3d-4d3b-b855-d5ef38e9c0d4">
                        <jr:groupHeader groupName="Group1">
                            <jr:cell height="30">
                                <textField evaluationTime="Group" evaluationGroup="Group1">
                                    <reportElement style="Style1" mode="Opaque" x="0" y="0" width="185" height="30" uuid="eae7a88f-2f46-4266-90d2-1b5f2a0dc451"/>
                                    <box>
                                        <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle">
                                        <font size="14" isBold="true"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[$V{totalchildren1}]]></textFieldExpression>
                                </textField>
                            </jr:cell>
                        </jr:groupHeader>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement key="" x="0" y="0" width="185" height="30" uuid="ffce4b15-e7ae-4b07-94a9-5f713725dd25"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font size="14" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Total Children]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement mode="Opaque" x="0" y="0" width="185" height="30" uuid="9080cc65-6f3d-4a2d-943f-6e4942c18710"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{totalchildren}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>


Reference : 

 http://stackoverflow.com/questions/12269736/using-variables-in-conditional-style

Monday 6 June 2016

Tip : Move the entire row to next page when one or more text fields data overflows to next page

Hi,

Usually, Clients needs data to be displayed neatly at server UI level though it would be an excel extract or pdf extract.

This tip is useful when report text fields data overflows to next page with wired look and feel.

Solution : 
1) Remove "Detail Over flow" option for all the text elements that are kept on detail band if  the property is already applied.
2) Select the detail band and look into the properties of it.

    Chose Split Type = Prevent instead Stretch

Split type property specifies the band behavior to split the data.  

This will solve the data over flow problem and there would be no half text displayed in page and remaining text will be displayed in next page rather the entire row will be pushed to the new page.

References : 
http://stackoverflow.com/questions/10744920/avoid-split-of-text-field-to-multiple-pages