gpt4 book ai didi

java - DynamicJasper - 分组列似乎没有分组

转载 作者:行者123 更新时间:2023-12-01 04:27:44 24 4
gpt4 key购买 nike

我目前正在使用 DynamicJasper 开发一份报告。这是我第一次使用这个。我正在遵循几个教程,但我无法解决这个问题。我有一个已分组的列状态。该列只能有两个值 - state1 和 state2。基本上,我期待的是这样的

state1
branch productline columnItem columnCode
branch productline columnItem columnCode
branch productline columnItem columnCode
branch productline columnItem columnCode
branch productline columnItem columnCode

state2
branch productline columnItem columnCode
branch productline columnItem columnCode
branch productline columnItem columnCode
branch productline columnItem columnCode
branch productline columnItem columnCode

但目前,我所拥有的是

state1
branch productline columnItem columnCode

state2
branch productline columnItem columnCode

state1
branch productline columnItem columnCode

state2
branch productline columnItem columnCode

...

就好像它们根本没有被分组一样。

这是我的可运行代码:

GroupTest.java

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
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.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.builders.GroupBuilder;
import ar.com.fdvs.dj.domain.constants.GroupLayout;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.DJGroup;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;

public class GroupTest{

public DynamicReport buildReport() throws Exception {

DynamicReportBuilder drb = new DynamicReportBuilder();
Integer margin = new Integer(20);

drb.setTitle("November 2006 sales report")
.setSubtitle("This is a sample report")
.setDetailHeight(new Integer(15)).setLeftMargin(margin)
.setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
.setPrintBackgroundOnOddRows(true);


AbstractColumn columnState = ColumnBuilder.getNew()
.setColumnProperty("state", String.class.getName())
.setTitle("State")
.setWidth(new Integer(85))
.build();

AbstractColumn columnBranch = ColumnBuilder.getNew()
.setColumnProperty("branch", String.class.getName())
.setTitle("Branch")
.setWidth(new Integer(85))
.build();

AbstractColumn columnaProductLine = ColumnBuilder.getNew()
.setColumnProperty("productLine", String.class.getName())
.setTitle("Product Line")
.setWidth(new Integer(85))
.build();

AbstractColumn columnaItem = ColumnBuilder.getNew()
.setColumnProperty("item", String.class.getName()).setTitle(
"Item").setWidth(new Integer(85))
.build();

AbstractColumn columnCode = ColumnBuilder.getNew()
.setColumnProperty("id", Long.class.getName())
.setTitle("ID")
.setWidth(new Integer(40))
.build();

AbstractColumn columnaQuantity = ColumnBuilder.getNew()
.setColumnProperty("quantity", Long.class.getName())
.setTitle("Quantity")
.setWidth(new Integer(25))
.build();

AbstractColumn columnAmount = ColumnBuilder.getNew()
.setColumnProperty("amount", Float.class.getName())
.setTitle("Amount")
.setWidth(new Integer(100))
.setPattern("$ 0.00")
.build();

Style groupLabelStyle = new Style("groupLabel");
groupLabelStyle.setVerticalAlign(VerticalAlign.BOTTOM);

GroupBuilder gb1 = new GroupBuilder();
DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
.setGroupLayout(GroupLayout.VALUE_IN_HEADER)
.build();

drb.addColumn(columnState);
drb.addColumn(columnBranch);
drb.addColumn(columnaProductLine);
drb.addColumn(columnaItem);
drb.addColumn(columnCode);
drb.addColumn(columnaQuantity);
drb.addColumn(columnAmount);

drb.addGroup(g1);

drb.setUseFullPageWidth(true);

DynamicReport dr = drb.build();

return dr;
}

public static void main(String[] args) throws Exception {
GroupTest test = new GroupTest();
DynamicReport dr = test.buildReport();
JRDataSource ds = new JRBeanCollectionDataSource(TheBean.getDummyBeans());
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JasperViewer.viewReport(jp);
}

}

TheBean.java

import java.util.ArrayList;
import java.util.List;

public class TheBean{

private String state;
private String branch;
private String productLine;
private String item;
private Long id;
private Long quantity;
private Float amount;

public TheBean(String state, String branch, String productLine,
String item, Long id, Long quantity, Float amount) {
this.state = state;
this.branch = branch;
this.productLine = productLine;
this.item = item;
this.id = id;
this.quantity = quantity;
this.amount = amount;
}

public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public String getProductLine() {
return productLine;
}
public void setProductLine(String productLine) {
this.productLine = productLine;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getQuantity() {
return quantity;
}
public void setQuantity(Long quantity) {
this.quantity = quantity;
}
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
public static List getDummyBeans(){
List theBeans = new ArrayList();

for(int i = 0; i < 10; i++){
String s = "";
if(i % 2 == 0)
s = "state1";
else
s = "state2";
TheBean bean = new TheBean(s, "branch", "productLine", "item", new Long(10L), new Long(10L), new Float(10));
theBeans.add(bean);
}
return theBeans;
}
}

最佳答案

我认为您忘记按状态字段对数据进行排序。

尝试对数据进行排序,一切都会好起来的。

<小时/>

您可以在Data Grouping中找到有关分组的更多信息。主题。

关于java - DynamicJasper - 分组列似乎没有分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com