Rainer
Rainer

Reputation: 21

DynamicJasper - Order columnns vertically

I would like to print a table that contains a lot of columns (e.g. 30). Is it possible with DJ to order these columns vertically instead of horizontally.

Example:

  1. Column1 - Row1 - Row2
  2. Column2 - Row1 - Row2
  3. Column3 - Row1 - Row2
  4. Column4 - Row1 - Row2

I am using DynamicReportBuilder and ColumnBuilder to create a report with DJ.

Upvotes: 0

Views: 670

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

AFIK There is no property to achieve columns vertically like in your example.

The easiest way since you are using dynamic-jasper (java) is to convert your data source to represent the new data structure

Example

public static JRDataSource convertToVerticalDatasource(JRDataSource ds, List<String> columns) throws JRException{
    //This is are vertical rows
    List<Map<String,?>> rows = new ArrayList<Map<String,?>>();

    //add the rows for each column
    for (String column : columns) {
        Map<String,Object> row = new HashMap<String,Object>();
        row.put("colName", column);
        rows.add(row);
    }
    int curRow = 0;
    //Loop our dataset and get rows, put them in to correct column
    while(ds.next()){
        if (rows.size()<curRow){
            break;
        }
        Map<String,Object> rowMap= (Map<String, Object>) rows.get(curRow);
        for (int i = 0; i < columns.size(); i++) {
            JRDesignField field = new JRDesignField();

            field.setName(columns.get(i));
            Object value = ds.getFieldValue(field);
            rowMap.put("row" + (i+1), value);
        }
        curRow++;
    }

    //Lets be nice (if someone need's to use it)
    if (ds instanceof JRRewindableDataSource){
        ((JRRewindableDataSource) ds).moveFirst();
    }
    JRMapCollectionDataSource dsv = new JRMapCollectionDataSource(rows);
    return dsv;
}

This example provide is a general solution, where the column names will be in column field colName and row 1 in column row1 ecc

Naturally since you are using dynamic jasper you can probably find a more efficient conversion adapted to your solution when building the columns.

If similar solution needs to be used with crosstab's in jrxml, the converted datasource instead of having row1, row2 as column names should have a column name with only row (with the value of current row), hence repeat record.

Upvotes: 1

Related Questions