有很多关于如何在报告的行上设置斑马条纹的教程。像这样:
+-------+-------+
| Value | Color |
+-------+-------+
| A | white |
| A | black |
| B | white |
| B | black |
| B | white |
| C | black |
| D | white |
| D | black |
+-------+-------+
但我想做的是分组剥离。像这样:
+-------+-------+
| Value | Color |
+-------+-------+
| A | white |
| A | white |
| B | black |
| B | black |
| B | black |
| C | white |
| D | black |
| D | black |
+-------+-------+
我使用“值”列作为组的表达式,并且我的数据按“值”排序。 “black”是当组为黑色时要打印的黑色矩形。 “白色”是没有黑色矩形。我想要一个可以放入黑色矩形的“Print When Expression”中的变量。
到目前为止我已经尝试过:
- 创建变量 $V{print}
- 初始值表达式:false
- 变量表达式:!$V{print}
- 增量类型:组
- 增量组:值
我希望每次组更改时 $V{print} 的值都会更改为相反的值。我得到的是正常的条纹列表(黑,白,黑,白......)
最佳答案
您当前解决方案的问题是:
calculationType="Nothing"
Nothing: This is the default calculation type that a variable performs. It means that the variable's value is recalculated with every iteration in the data source and that the value returned is obtained by simply evaluating the variable's expression.
这种类型的计算将使您的 incrementType
无效,因此 incrementType
无效,因为我们不进行计算。这就是为什么目前你得到的是黑,白,黑,白。
这将达到您想要的结果
变量定义(让我们做一些计算示例,每次组更改、求和或计数时加 1)
<variable name="GroupCnt" class="java.lang.Integer" incrementType="Group" incrementGroup="myGroup" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
</variable>
conditionExpression(我们可以对变量GroupCnt
使用模运算符)
<conditionExpression><![CDATA[$V{GroupCnt}%2==0]]></conditionExpression>
完整的 jrxml 示例(我添加了一个矩形作为对此的 OP 注释)
<?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="group" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c1d9b4b7-6162-4b17-b871-3cf3b867d1ef">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="myStyle">
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{GroupCnt}.intValue()%2==0)]]></conditionExpression>
<style mode="Opaque" forecolor="#FFFFFF" backcolor="#000000"/>
</conditionalStyle>
</style>
<field name="Value" class="java.lang.String"/>
<variable name="GroupCnt" class="java.lang.Integer" incrementType="Group" incrementGroup="myGroup" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
</variable>
<group name="myGroup">
<groupExpression><![CDATA[$F{Value}]]></groupExpression>
</group>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement style="myStyle" x="0" y="0" width="150" height="20" uuid="7ca1ac35-6249-4ba6-ac87-031f8d410d2e"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{Value}]]></textFieldExpression>
</textField>
<rectangle>
<reportElement style="myStyle" x="150" y="0" width="150" height="20" uuid="d322e0df-0d39-4370-90e6-58305d449852"/>
</rectangle>
</band>
</detail>
</jasperReport>
new Boolean($V{GroupCnt}.intValue()%2==0)
、new Boolean
和 intValue()
code> 只是用来与旧的 jasper 报告版本兼容,在最新版本中不需要它
结果
关于jasper-reports - 当组发生变化时如何应用条件样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158278/