Jump to content

Crosstab conditional style with group variable


heiko.storz

Recommended Posts

Hello everyone,

is it possible to apply a conditional style to the whole row of a crosstab based on a row group variable?

My current solution has a conditional style on a measure variable of the crosstab. However when I apply it on the crosstab row textfields the condition is not checked on  empty cells, therefore the row style has some ugly holes and does not look correct.

I tried to add a group variable based on the row field howerver I can not access it in the conditional style declaration, I alwas get an error "Variable not found". I get this error when I decalre the variable in the main report dataset or in the crosstab subdataset. I don't know how to acces a variable of a crosstab subdataset in a conditional style.

I managed to alternate color the whole rows with the given $V{ROW_COUNT} variable, but I need to define my own variable.

Is there any example on how to do this?

I tried a lot but could not figure it out and the samples jut use the $V{ROW_COUNT} within a crosstab.

 

 

 

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Thank you very much for your quick answer.

I tried to set the Parent Style of the crosstab's Detail Cell to the conditional style, but still the empty cells do not have the style applied.

I also tried this with setting this property <property name="net.sf.jasperreports.style.evaluation.time.enabled" value="true"/> but still no change.

My result looks like this:

/sites/default/files/images/table.JPG

This is my conditional style: 

<style name="cellStyle">
   <conditionalStyle>
       <conditionExpression><![CDATA[$V{LINE_GROUP_COUNT} % 2 == 0 ? Boolean.TRUE : Boolean.FALSE]]></conditionExpression>
       <style mode="Opaque" backcolor="#E0E0E0"/>
    </conditionalStyle>
    <conditionalStyle>
       <conditionExpression><![CDATA[$V{LINE_GROUP_COUNT} % 2 == 0 ? Boolean.FALSE : Boolean.TRUE]]></conditionExpression>
       <style mode="Opaque" backcolor="#BFBFBF"/>
     </conditionalStyle>
</style>
 

This is my measure variable: 

<measure name="LINE_GROUP_COUNT" class="java.lang.Integer">
   <measureExpression><![CDATA[$F{lineGroupCount}]]></measureExpression>
</measure>
 
This is my detail crosstab cell:
 
<crosstabCell width="200" height="20">
  <cellContents mode="Opaque" style="cellStyle">
     <textField isStretchWithOverflow="true" isBlankWhenNull="true">
        <reportElement key="textField" stretchType="ContainerHeight" x="0" y="0" width="200" height="20" uuid="606a255d-b7ed-48ba-91fb-1d52c25ee38e">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
       </reportElement>
       <box padding="3">
          <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="Left" verticalAlignment="Middle" markup="styled"/>
       <textFieldExpression><![CDATA[$V{USER}]]></textFieldExpression>
     </textField>
   </cellContents>
</crosstabCell>

 

Would be great if you could figure out what is wrong with that.

Link to comment
Share on other sites

Hello,

Please check sample below its working fine. USE Sample DB to run the report.

 

 
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.0.2  -->
<!-- 2016-09-06T10:50:31 -->
<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="CrossTab" language="groovy" pageWidth="2000" pageHeight="800" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="1960" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenResourceMissingType="Key" uuid="e16b8ea9-0c78-4688-9968-aa0bf612aa76">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
<property name="com.jaspersoft.studio.data.sql.tables" value="Ik9SREVSUyIgQVMgLDE2Miw0NCwwOTlhNjRlYS00NWE5LTRmMTUtOWNmOS1mOTg1YWM2NjUwZGQ7"/>
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<style name="Crosstab_CH" 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="Crosstab_CG" 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="Crosstab 1_CH" 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="Crosstab 1_CG" 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="Crosstab 1_CT" mode="Opaque" backcolor="#005FB3">
<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="Crosstab 1_CD" 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{SHIPCOUNTRY}==USA]]></conditionExpression>
<style mode="Opaque" forecolor="#FF1814" backcolor="#FFFD80"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$V{cn1}>=1]]></conditionExpression>
<style mode="Opaque" forecolor="#E30400" backcolor="#F7EB39">
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
</style>
</conditionalStyle>
</style>
<queryString>
<![CDATA[sELECT top 20
"ORDERS"."ORDERID",
"ORDERS"."CUSTOMERID",
"ORDERS"."EMPLOYEEID",
"ORDERS"."ORDERDATE",
"ORDERS"."REQUIREDDATE",
"ORDERS"."SHIPPEDDATE",
"ORDERS"."FREIGHT",
"ORDERS"."SHIPCITY",
"ORDERS"."SHIPREGION",
"ORDERS"."SHIPCOUNTRY"
FROM "ORDERS"]]>
</queryString>
<field name="ORDERID" class="java.lang.Integer"/>
<field name="CUSTOMERID" class="java.lang.String"/>
<field name="EMPLOYEEID" class="java.lang.Integer"/>
<field name="ORDERDATE" class="java.sql.Timestamp"/>
<field name="REQUIREDDATE" class="java.sql.Timestamp"/>
<field name="SHIPPEDDATE" class="java.sql.Timestamp"/>
<field name="FREIGHT" class="java.math.BigDecimal"/>
<field name="SHIPCITY" class="java.lang.String"/>
<field name="SHIPREGION" class="java.lang.String"/>
<field name="SHIPCOUNTRY" class="java.lang.String"/>
<variable name="mon" class="java.util.Date">
<variableExpression><![CDATA[$F{ORDERDATE}.getMonth()]]></variableExpression>
</variable>
<variable name="Contry" class="java.lang.String">
<variableExpression><![CDATA[$F{SHIPCOUNTRY}]]></variableExpression>
<initialValueExpression><![CDATA[$F{SHIPCOUNTRY}]]></initialValueExpression>
</variable>
<variable name="cn" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[$F{SHIPCOUNTRY}]]></variableExpression>
<initialValueExpression><![CDATA[$F{SHIPCOUNTRY}]]></initialValueExpression>
</variable>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="239" splitType="Stretch">
<crosstab ignoreWidth="true">
<reportElement x="20" y="10" width="280" height="200" uuid="fda36794-60a2-4032-8a72-67a0e34adfdd">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
</reportElement>
<rowGroup name="SHIPCOUNTRY" width="60" totalPosition="End">
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{SHIPCOUNTRY}]]></bucketExpression>
</bucket>
<crosstabRowHeader>
<cellContents mode="Opaque" style="Crosstab 1_CH">
<box>
<rightPen lineWidth="1.0"/>
</box>
<textField>
<reportElement x="0" y="0" width="60" height="20" uuid="04c8b2ff-df6b-4b2c-bdd1-d78bf94c5874"/>
<box>
<rightPen lineWidth="2.0"/>
</box>
<textElement textAlignment="Center">
<font fontName="Calibri" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{SHIPCOUNTRY}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabRowHeader>
<crosstabTotalRowHeader>
<cellContents mode="Opaque" style="Crosstab 1_CT">
<staticText>
<reportElement x="0" y="0" width="60" height="20" uuid="382fc795-c4e4-4b7c-a635-ca2edafb288e"/>
<textElement textAlignment="Center">
<font fontName="Calibri" isBold="true"/>
</textElement>
<text><![CDATA[Total SHIPCOUNTRY]]></text>
</staticText>
</cellContents>
</crosstabTotalRowHeader>
</rowGroup>
<columnGroup name="SHIPCITY" height="20" totalPosition="End">
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{SHIPCITY}]]></bucketExpression>
</bucket>
<crosstabColumnHeader>
<cellContents mode="Opaque" style="Crosstab 1_CH">
<textField>
<reportElement x="0" y="0" width="120" height="20" uuid="ff64c904-b50e-445f-8cbc-54a18fea816a"/>
<textElement textAlignment="Center">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{SHIPCITY}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabColumnHeader>
<crosstabTotalColumnHeader>
<cellContents mode="Opaque" style="Crosstab 1_CT">
<staticText>
<reportElement x="0" y="0" width="60" height="40" forecolor="#FFFFFF" uuid="472873f3-f486-41fc-afbc-6343ea90f124"/>
<textElement textAlignment="Center">
<font fontName="Calibri" isBold="true"/>
</textElement>
<text><![CDATA[Total SHIPCITY]]></text>
</staticText>
</cellContents>
</crosstabTotalColumnHeader>
</columnGroup>
<columnGroup name="SHIPREGION" height="20" totalPosition="End">
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{SHIPREGION}]]></bucketExpression>
</bucket>
<crosstabColumnHeader>
<cellContents mode="Opaque" style="Crosstab 1_CH">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="120" height="20" uuid="c1fe7e4a-bce7-4fa5-ab5c-9ef4aad320de"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$V{SHIPREGION}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabColumnHeader>
<crosstabTotalColumnHeader>
<cellContents mode="Opaque" style="Crosstab 1_CG"/>
</crosstabTotalColumnHeader>
</columnGroup>
<measure name="FREIGHT_MEASURE" class="java.math.BigDecimal" calculation="Sum">
<measureExpression><![CDATA[$F{FREIGHT}]]></measureExpression>
</measure>
<measure name="cn1" class="java.lang.Integer" calculation="Count">
<measureExpression><![CDATA[$V{cn}]]></measureExpression>
</measure>
<crosstabCell width="120" height="20">
<cellContents mode="Opaque" style="Style1">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<textField pattern="#,##0.###" isBlankWhenNull="false">
<reportElement x="0" y="0" width="50" height="20" uuid="0ce944e5-4df8-49ad-8686-120da34bd8b1"/>
<textElement textAlignment="Center">
<font fontName="Calibri"/>
</textElement>
<textFieldExpression><![CDATA[$V{FREIGHT_MEASURE}==0?" ":$V{FREIGHT_MEASURE}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Style1" x="60" y="0" width="60" height="20" uuid="f8f27ca2-311d-4626-8dcd-feae5e022bd3">
<printWhenExpression><![CDATA[$V{cn1}!=0]]></printWhenExpression>
</reportElement>
<textElement textAlignment="Center">
<font fontName="Calibri"/>
</textElement>
<textFieldExpression><![CDATA[$V{cn1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" columnTotalGroup="SHIPCITY">
<cellContents mode="Opaque" style="Crosstab 1_CT">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="3ef9d9f8-618b-4597-a2aa-105fcf24e266"/>
<textElement textAlignment="Center">
<font fontName="Calibri" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{FREIGHT_MEASURE}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="0" height="20" columnTotalGroup="SHIPREGION">
<cellContents mode="Opaque" style="Crosstab 1_CG">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</cellContents>
</crosstabCell>
<crosstabCell width="120" height="20" rowTotalGroup="SHIPCOUNTRY">
<cellContents mode="Opaque" style="Crosstab 1_CT">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<textField pattern="#,##0.###">
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="75454bbb-8e1f-4469-8c2c-164296c50330"/>
<textElement textAlignment="Center">
<font fontName="Calibri" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{FREIGHT_MEASURE}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" rowTotalGroup="SHIPCOUNTRY" columnTotalGroup="SHIPCITY">
<cellContents mode="Opaque" style="Crosstab 1_CT">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="ed2981d7-1447-4946-a12c-255ddd7472df"/>
<textElement textAlignment="Center">
<font fontName="Calibri" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{FREIGHT_MEASURE}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="0" height="20" rowTotalGroup="SHIPCOUNTRY" columnTotalGroup="SHIPREGION">
<cellContents mode="Opaque" style="Crosstab 1_CT">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</cellContents>
</crosstabCell>
</crosstab>
</band>
</summary>
</jasperReport>
 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...