sorting - 如何在 jasper 中对交叉表的列标题值进行排序?

标签 sorting jasper-reports crosstab

我在 jasper 交叉表中的列值有月份名称。

问题是它们从后端以各种对象发送,所以我无法对来自后端的月份名称进行排序。 <

因此出现了这个问题。 enter image description here

我想按一月、二月、三月、四月、五月、八月的顺序对这些列进行排序。

我可以在 jasper 的列值中设置 if else if else 条件吗?

最佳答案

要对交叉表标题进行排序,您有两种选择:

  1. 创建 Comparator java 中的类并将其传递给 comparatorExpression 中的 columnGroup 的存储桶表达式

  2. 创建一个可排序的度量并将其传递给 orderByExpression

在此示例中,我将使用方法 2 并假设后端以日期格式 MMM 返回月份,因此我创建一个日期对象并从中获取时间。如果月份名称不是 MMM 格式,您需要将三元表达式实现为 suggested通过 Alex K .

new java.text.SimpleDateFormat("yyyy-MMM-dd").parse("2000-" + $F{Month} + "-01").getTime()

创建度量:

<measure name="monthSort" class="java.lang.Long">
    <measureExpression><![CDATA[new java.text.SimpleDateFormat("yyyy-MMM-dd").parse("2000-" + $F{Month} + "-01").getTime()]]></measureExpression>
</measure>

将度量添加到 columnGroup 中的 orderByExpression(如果尚未实例化变量,则避免空指针)

<columnGroup name="Month" height="20" totalPosition="End">
    <bucket class="java.lang.String">
        <bucketExpression><![CDATA[$F{Month}]]></bucketExpression>
        <orderByExpression><![CDATA[($V{monthSort}==null?0:$V{monthSort})]]></orderByExpression>
    </bucket>
    ....
 </columnGroup>

完整示例

数据

+------+-------+-------+
| Name | Month | Value |
+------+-------+-------+
| cat1 | Jan   |   121 |
| cat1 | Feb   |    52 |
| cat1 | Aug   |    73 |
| cat1 | Mar   |    14 |
| cat2 | Feb   |    54 |
| cat2 | Apr   |    80 |
| cat2 | Dec   |    10 |
| cat2 | Jan   |    20 |
+------+-------+-------+

jrxml

<?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="SortValue" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0ba3ded0-a5d4-435b-a2b1-c61ecd71ac00">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Months"/>
    <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_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_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>
    <queryString language="csv">
        <![CDATA[]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="Month" class="java.lang.String"/>
    <field name="Value" class="java.lang.String"/>
    <summary>
        <band height="300" splitType="Stretch">
            <crosstab>
                <reportElement x="0" y="0" width="520" height="70" uuid="13bf3aea-b677-4389-bbd5-60cb98bad0c6">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <rowGroup name="Name" width="60" totalPosition="End">
                    <bucket class="java.lang.String">
                        <bucketExpression><![CDATA[$F{Name}]]></bucketExpression>
                    </bucket>
                    <crosstabRowHeader>
                        <cellContents mode="Opaque" style="Crosstab_CH">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="f9becd93-ee0a-443b-bf24-d3c5f903d2b7"/>
                                <textFieldExpression><![CDATA[$V{Name}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabRowHeader>
                    <crosstabTotalRowHeader>
                        <cellContents mode="Opaque" style="Crosstab_CT">
                            <staticText>
                                <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="87613864-7410-44f6-a39f-7a727545db9d"/>
                                <text><![CDATA[Total Name]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabTotalRowHeader>
                </rowGroup>
                <columnGroup name="Month" height="20" totalPosition="End">
                    <bucket class="java.lang.String">
                        <bucketExpression><![CDATA[$F{Month}]]></bucketExpression>
                        <orderByExpression><![CDATA[($V{monthSort}==null?0:$V{monthSort})]]></orderByExpression>
                    </bucket>
                    <crosstabColumnHeader>
                        <cellContents mode="Opaque" style="Crosstab_CH">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="f8957674-bebb-4c29-9799-ff0b1756d910"/>
                                <textFieldExpression><![CDATA[$V{Month}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabColumnHeader>
                    <crosstabTotalColumnHeader>
                        <cellContents mode="Opaque" style="Crosstab_CT">
                            <staticText>
                                <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="50a78979-ea87-457d-aced-36a474659b62"/>
                                <text><![CDATA[Total Month]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabTotalColumnHeader>
                </columnGroup>
                <measure name="Value_MEASURE" class="java.lang.String" calculation="First">
                    <measureExpression><![CDATA[$F{Value}]]></measureExpression>
                </measure>
                <measure name="monthSort" class="java.lang.Long">
                    <measureExpression><![CDATA[new java.text.SimpleDateFormat("yyyy-MMM-dd").parse("2000-" + $F{Month} + "-01").getTime()]]></measureExpression>
                </measure>
                <crosstabCell width="60" height="20">
                    <cellContents mode="Opaque" style="Crosstab_CD">
                        <textField isBlankWhenNull="true">
                            <reportElement x="0" y="0" width="60" height="20" uuid="f53a65f4-1f08-4856-92a4-09267c6c073c"/>
                            <textFieldExpression><![CDATA[$V{Value_MEASURE}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" columnTotalGroup="Month">
                    <cellContents mode="Opaque" style="Crosstab_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="0a17d733-1388-4df9-b754-182eaa09eca8"/>
                            <textFieldExpression><![CDATA[$V{Value_MEASURE}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="Name">
                    <cellContents mode="Opaque" style="Crosstab_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="2bcf8a9c-8315-4508-9397-4850b08957f9"/>
                            <textFieldExpression><![CDATA[$V{Value_MEASURE}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="Name" columnTotalGroup="Month">
                    <cellContents mode="Opaque" style="Crosstab_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="5431126e-8ed1-4c20-be7b-f88df55039c2"/>
                            <textFieldExpression><![CDATA[$V{Value_MEASURE}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
            </crosstab>
        </band>
    </summary>
</jasperReport>

输出

result

关于sorting - 如何在 jasper 中对交叉表的列标题值进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47509291/

相关文章:

algorithm - 排列数字对,使相邻对的成员相等

java - 我的报告在 Eclipse 中正常工作,但使用 jar 文件抛出 "No query executer factory registered for the ' MongoDbQuery'"

jasper-reports - 有没有办法在碧 Jade 报告上打印小计和总计?

python - 处理/转置 Pandas 数据框

postgresql - 如何在不编写函数的情况下在 postgresql 中进行数据透视表或交叉表?

php - Yii2 在 GridView 中的默认过滤器

php - PHP usort 的不良行为

c - 如何从 C 中的矩阵打印排序列

java - 如何在将现有模板加载到 JasperDesign 中然后更改它时更改页面大小/格式?

sorting - Jasper Reports 使用 comparatorExpression 进行交叉表排序