Billy Hope Billy Hope - 2 months ago 19
Java Question

how to sort columns in ireport crosstab in a specific scheme

The following image shows my crosstab in ireport:

crosstab

I want to sort my columns in a specific order, knowing that in the crosstab data, i put a list of objects

List<ChampEtatAteliers>
with the following attributes :

public class ChampEtatAteliers {

private String numero;
private String denominationFr;
private String etatEntreeSortie;

public ChampEtatAteliers(String numero, String denominationFr, String etatEntreeSortie) {
this.numero = numero;
this.denominationFr = denominationFr;
this.etatEntreeSortie = etatEntreeSortie;
}
}


the
etatEntreeSortie
is shown in the columns, the
denominationFr
is shown in the rows, and
numero
is used for the count in crosstab

String rapportLien = "/SituationEquipement.jasper";
Map parametres = new HashMap();
JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(atelierses);


in java I'm putting these objects in list
atelierses
List<ChampEtatAteliers>
, so is there any way to sort the columns in the list or in the crosstab ireport, my xml code on ireport is as follows:

<columnGroup name="nbreVehicule" height="66">
<bucket order="Descending" class="java.lang.String">
<bucketExpression><![CDATA[$F{nbreVehicule}]]></bucketExpression>
<orderByExpression><![CDATA[$V{MyOrderByField_measure}]]></orderByExpression>
</bucket>
<crosstabColumnHeader>
<cellContents backcolor="#F0F8FF" mode="Opaque">
<textField>
<reportElement style="Crosstab Data Text" x="0" y="0" width="85" height="66" uuid="2109b261-1b20-493a-b664-ce59731c4a20"/>
<box topPadding="1" leftPadding="1" bottomPadding="1" rightPadding="1"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{nbreVehicule}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabColumnHeader>
<crosstabTotalColumnHeader>
<cellContents/>
</crosstabTotalColumnHeader>
</columnGroup>
<measure name="MyOrderByField_measure" class="java.lang.Integer" calculation="Nothing" >
<measureExpression><![CDATA[$F{myOrderByField}]]></measureExpression>
</measure>

Answer

In jasper report the crosstab columnGroup can be sorted ascending or descending by setting the order attribute on the bucket

Example

<columnGroup name="GROUP" height="16" headerPosition="Center">
    <bucket order="Descending" class="java.lang.String">
        <bucketExpression><![CDATA[$F{MyField}]]></bucketExpression>
    </bucket>
    .....
</columnGroup>

If you need a user defined order, you should use the orderByExpression

  1. Define a measure for on what you like to order

    <measure name="myOrderByField_measure" class="java.lang.Integer" calculation="First">
        <measureExpression><![CDATA[$F{myOrderByField}]]></measureExpression>
    </measure>
    
  2. Use the measure in orderByExpression

    <columnGroup name="GROUP" height="16" headerPosition="Center">
      <bucket class="java.lang.String">
        <bucketExpression><![CDATA[$F{myField}]]></bucketExpression>
        <orderByExpression><![CDATA[$V{myOrderByField_measure}]]></orderByExpression>
    </bucket>
    .....
    </columnGroup>
    

In your example this could be achieved by introducing a new field in the ChampEtatAteliers class example private int myOrderByField; and then via code setting it as needed.

Another solution, would be to return an own Object (class) as MyField and implement Comparable, that generates the desired order.

EDIT: WORK AROUND, OP CAN NOT MAKE THE orderByExpression work correctly.

Since the myOrderByField is unique (related 1 to 1) to the nbreVehicule he can use myOrderByField in the bucketExpression

<measure name="nbreVehicule_measure" class="java.lang.String">
   <measureExpression><![CDATA[$F{nbreVehicule}]]></measureExpression>
</measure>

<columnGroup name="nbreVehicule" height="66">
   <bucket class="java.lang.Integer">
     <bucketExpression><![CDATA[$F{myOrderByField}]]></bucketExpression>
   </bucket>
   ....
   <textFieldExpression><![CDATA[$V{nbreVehicule_measure}]]></textFieldExpression>
   .....
</columnGroup>

Hence the bucketExpression does not need to be the same as the text displayed in the crosstabColumnHeader

Comments