组头宽度

时间:2014-02-05 11:43:43

标签: java jasper-reports dynamic-jasper

如何设置组头宽度? 我有三列占据了约50%的纸张,但是组头跨越了整页宽度。 有没有办法控制组头的宽度?

以下是我的报告图片:

http://imageshack.com/a/img541/1723/jryp.png

这里,name是列标题,旁边是占整页宽度的字段值。

我尝试为分组列设置列宽,但它只影响列标题。内容仍然延伸到整页。

这是代码:

package cctintl.c3d.reporting.jasper.jaspergenerator;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.domain.builders.GroupBuilder;
import ar.com.fdvs.dj.domain.constants.GroupLayout;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;

public class DJTest {

public static Map<String, Object >createRecord(Integer id, String name, 
        Date date, Float price){
    Map<String, Object> rec = new HashMap<String, Object>();
    rec.put("id", id);
    rec.put("name", name);
    rec.put("date", date);
    rec.put("price", price);

    return rec;
}

public static Collection<Map<String, ?>> getData(){
    List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();

    data.add(createRecord(1, "Group1", new Date(), Float.valueOf(10)));
    data.add(createRecord(2, "Group1", new Date(), Float.valueOf(12)));
    data.add(createRecord(3, "Group1", new Date(), Float.valueOf(102)));

    data.add(createRecord(4, "Group2", new Date(), Float.valueOf(1002)));
    data.add(createRecord(5, "Group2", new Date(), Float.valueOf(5402)));

    return data;
}

public static void main(String[] args) {

    FastReportBuilder rb = new FastReportBuilder();

    Style groupStyle = new Style("groupStyle");

    Color color = new Color(230, 230, 230);
    groupStyle.setBackgroundColor(color);
    groupStyle.setTextColor(Color.BLACK);
    groupStyle.setTransparency(Transparency.OPAQUE);

    AbstractColumn colId = ColumnBuilder.getNew()
            .setColumnProperty("id", Integer.class)
            .setTitle("ID")
            .build();

    AbstractColumn colName = ColumnBuilder.getNew()
            .setColumnProperty("name", String.class)
            .setTitle("NAME")
            .setStyle(groupStyle)
            .setHeaderStyle(groupStyle)
            .build();

    AbstractColumn colDate = ColumnBuilder.getNew()
            .setColumnProperty("date", Date.class)
            .setTitle("DATE")
            .setPattern("MM/dd/yy hh:mm")
            .setWidth(90)
            .build();

    AbstractColumn colPrice = ColumnBuilder.getNew()
            .setColumnProperty("price", Float.class)
            .setTitle("PRICE")
            .setWidth(70)
            .build();

    GroupBuilder gb = new GroupBuilder();

    GroupLayout labelInHeader = new GroupLayout(
            true, false, true, true, false);

    gb.setCriteriaColumn((PropertyColumn)colName)
            .setGroupLayout(labelInHeader)
            .setHeaderVariablesHeight(new Integer(20))
            .setFooterHeight(30, false)
            .setFooterVariablesHeight(15);

    rb.addColumn(colId);
    rb.addColumn(colName);
    rb.addColumn(colDate);
    rb.addColumn(colPrice);

    rb.addGroup(gb.build());

    DynamicReport report = rb.build();

    JRDataSource ds = new JRMapCollectionDataSource(getData());


    Map<String, Object> params = new HashMap<String, Object>();

    try {
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(report, 
                new ClassicLayoutManager(), ds, params);

        JasperViewer jw = new JasperViewer(jp);
        jw.setVisible(true);

    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

0 个答案:

没有答案