- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用交叉表报告,在应用行和列排序后,它破坏了列分组。我在交叉表上调用 setDataPreSorted(true),在行和列上调用 SortBuilder:
SortBuilder rowSortBuilder = asc(field("rowOrder", Integer.class));
SortBuilder columnSortBuilder = asc(field("colOrder", Integer.class));
report.sortBy(rowSortBuilder, columnSortBuilder)
这是我的完整代码:
import java.util.ArrayList;
import java.util.List;
import net.sf.dynamicreports.examples.Templates;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import static net.sf.dynamicreports.report.builder.DynamicReports.asc;
import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.ctab;
import static net.sf.dynamicreports.report.builder.DynamicReports.field;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import net.sf.dynamicreports.report.builder.SortBuilder;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.crosstab.CrosstabBuilder;
import net.sf.dynamicreports.report.builder.crosstab.CrosstabColumnGroupBuilder;
import net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder;
import net.sf.dynamicreports.report.builder.crosstab.CrosstabRowGroupBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.Calculation;
import net.sf.dynamicreports.report.constant.HorizontalTextAlignment;
import net.sf.dynamicreports.report.constant.PageOrientation;
import net.sf.dynamicreports.report.constant.PageType;
import net.sf.dynamicreports.report.constant.VerticalTextAlignment;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class CrosstabGroupTest {
public JasperReportBuilder build() throws DRException {
JasperReportBuilder report = report();
// styles
String reportFont = "Roboto";
int cellPadding = 3;
StyleBuilder headerStyleCenter = stl.style().setFontName(reportFont).setFontSize(10).setBold(true)
.setBorder(stl.pen1Point()).setPadding(cellPadding)
.setHorizontalTextAlignment(HorizontalTextAlignment.CENTER)
.setVerticalTextAlignment(VerticalTextAlignment.MIDDLE);
StyleBuilder rowStyleData = stl.style().setFontName(reportFont).setFontSize(10)
.setBorder(stl.pen1Point()).setPadding(cellPadding)
.setHorizontalTextAlignment(HorizontalTextAlignment.CENTER)
.setVerticalTextAlignment(VerticalTextAlignment.MIDDLE);
// row groups
CrosstabRowGroupBuilder<String> rowGroupItem = ctab.rowGroup("product", String.class).setShowTotal(false)
.setHeaderStyle(rowStyleData).setHeaderWidth(120);
CrosstabRowGroupBuilder<String> rowGroupUnit = ctab.rowGroup("unit", String.class).setShowTotal(false)
.setHeaderStyle(rowStyleData).setHeaderWidth(50);
CrosstabRowGroupBuilder<String> rowGroupDate = ctab.rowGroup("date", String.class).setShowTotal(false)
.setHeaderStyle(rowStyleData).setHeaderWidth(50);
// column groups
CrosstabColumnGroupBuilder col_type = ctab.columnGroup("branch", String.class).setTotalHeader("Total quantity")
.setShowTotal(true).setHeaderStyle(rowStyleData);
// measure
CrosstabMeasureBuilder quantityMeasure = ctab.measure("", "amount", Double.class, Calculation.NOTHING).setTitleStyle(stl.style().setFontSize(0));
CrosstabBuilder crosstab = ctab.crosstab()
.setCellWidth(220)
.addHeaderCell(cmp.horizontalList(
cmp.text("Product").setStyle(headerStyleCenter).setFixedWidth(120),
cmp.text("Unit").setStyle(headerStyleCenter).setFixedWidth(50),
cmp.text("Year").setStyle(headerStyleCenter).setFixedWidth(50)).
setHeight(13))
.setCellStyle(rowStyleData).setCellWidth(60)
.rowGroups(rowGroupItem, rowGroupUnit, rowGroupDate)
.columnGroups(col_type)
.measures(quantityMeasure)
.setDataPreSorted(true);
// data
List<ProductQuantityData> dataList = new ArrayList<>();
int rowOrder = 1;
int colOrder = 1;
dataList.add(new ProductQuantityData("Water", "litre", "2016.02", "Branch 1", 50d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Water", "litre", "2017.02", "Branch 1", 150d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Water", "litre", "diff", "Branch 1", 100d, rowOrder, colOrder));
colOrder = 2;
dataList.add(new ProductQuantityData("Water", "litre", "2016.02", "Branch 2", 150d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Water", "litre", "2017.02", "Branch 2", 140d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Water", "litre", "diff", "Branch 2", -10d, rowOrder, colOrder));
rowOrder = 2;
colOrder = 1;
dataList.add(new ProductQuantityData("Coffee bean", "kg", "2016.02", "Branch 1", 80d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Coffee bean", "kg", "2017.02", "Branch 1", 75d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Coffee bean", "kg", "diff", "Branch 1", -5d, rowOrder, colOrder));
colOrder = 2;
dataList.add(new ProductQuantityData("Coffee bean", "kg", "2016.02", "Branch 2", 77d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Coffee bean", "kg", "2017.02", "Branch 2", 77d, rowOrder, colOrder));
dataList.add(new ProductQuantityData("Coffee bean", "kg", "diff", "Branch 2", 0d, rowOrder, colOrder));
// sort
SortBuilder rowSortBuilder = asc(field("rowOrder", Integer.class));
SortBuilder columnSortBuilder = asc(field("colOrder", Integer.class));
report
.sortBy(rowSortBuilder, columnSortBuilder)
.setPageFormat(PageType.A4, PageOrientation.LANDSCAPE)
.setTemplate(Templates.reportTemplate)
.title(Components.text("Product quantity by branch"))
.summary(crosstab)
.pageFooter(Components.pageXslashY())
.setDataSource(new JRBeanCollectionDataSource(dataList));
return report;
}
public static void main(String[] args) {
CrosstabGroupTest design = new CrosstabGroupTest();
try {
JasperReportBuilder report = design.build();
report.show();
} catch (DRException e) {
e.printStackTrace();
}
}
}
这是我的 java bean 代码:
public class ProductQuantityData {
private String product;
private String unit;
private String date;
private String branch;
private Double amount;
private int rowOrder;
private int colOrder;
public ProductQuantityData(String product, String unit, String date, String branch, Double amount, int rowOrder, int colOrder) {
this.product = product;
this.date = date;
this.unit = unit;
this.branch = branch;
this.amount = amount;
this.rowOrder = rowOrder;
this.colOrder = colOrder;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
public int getRowOrder() {
return rowOrder;
}
public void setRowOrder(int rowOrder) {
this.rowOrder = rowOrder;
}
public int getColOrder() {
return colOrder;
}
public void setColOrder(int colOrder) {
this.colOrder = colOrder;
}
}
最佳答案
幸运地找到了一个有用的 forum post .
添加以下 2 个类。
public class CTOValueFormatter extends AbstractValueFormatter<String, String>{
@Override
public String format(String value, ReportParameters reportParameters) {
return value.split("-")[1];
}
}
public class CTOrderExpression extends AbstractComplexExpression<String> {
public CTOrderExpression(String sortField, String showField) {
addExpression(field(sortField, Integer.class));
addExpression(field(showField, String.class));
}
@Override
public String evaluate(List<?> list, ReportParameters rp) {
return list.get(0) + "-" + list.get(1);
}
}
并且不要使用 .sortBy(rowSortBuilder, columnSortBuilder)
和 .setDataPreSorted(true)
。
改为使用以下代码创建行组项目。
CrosstabRowGroupBuilder<String> rowGroupItem = ctab.rowGroup(new CTOrderExpression("rowOrder", "product")).setShowTotal(false)
.setHeaderStyle(rowStyleData)
.setHeaderWidth(120)
.setHeaderValueFormatter(new CTOValueFormatter());
您也可以对列组执行相同的操作。
CrosstabColumnGroupBuilder col_type = ctab.columnGroup(new CTOrderExpression("colOrder", "branch")).setTotalHeader("Total quantity")
.setShowTotal(true).setHeaderStyle(rowStyleData);
获取完整源代码 here .
关于java - 调用 setDataPreSorted(true) 后 DynamicReports 交叉表报告列分组中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131459/
#include int main() { int i; for( i = 0; i< 10 ; i++ ) { printf("test1 &d", i);
我有一个连接到远程服务器以查询数据的 C# 程序。数据很大,所以查询大约需要 2 分钟才能完成。在这 2 分钟的时间内,互联网中断了。这导致作业无法完成,程序卡在获取数据例程中。 它建立了连接,但在选
语句 1: [2,4,6,7,8].each do |i| (i % 2 == 0) || (puts "Not even" && break) puts i end 声明 2: [2
我想知道 C++ 是否有任何处理中断的方法。我希望一个程序将信息存储在文本文件中,而另一个程序根据文本文件中的内容打印一条语句。由于我希望它尽可能准确,因此我需要在更新程序更新文件时中断打印程序。 最
我正在尝试终止一个线程,但它不会中断或停止。所有这些都是名为 Webots 的软件 Controller 的一部分。我用它来模拟多机器人系统。在每个机器人的 Controller 中,我启动了一个线程
中断10h可以使用的服务有很多, 我想问一下关于其中两个我很困惑的问题 首先是, AH = 06H SCROLL UP WINDOW AH = 07H SCROLL DOWN WINDOW 在 D
我有一个基本的 SQL 问题,如果有两个事件连接,"A"和 "B"到 SQL 服务器,假设两者之间发生死锁,那么为了避免死锁,SQL 服务器将回滚其中一个连接“A”或“B”的交易。假设 SQL Ser
我有一个 mapkit/ View ,它工作正常 - 但我滚动并在 2 - 10 次移动后我的应用程序崩溃了......而且这只是一个“中断”。 这是我的代码的一部分。我认为这是后台线程和数组释放/覆
有什么办法可以做到,比如 C/C# ? 例如(C#风格) for (int i = 0; i Seq.tryFind (fun i -> printfn "%d" i i=66) 在实践中,
我的问题是Haskell如何与系统交互。 例如,IO如何发生? 我对IO monad不感兴趣。或者如何使Haskell打印字符串或读取文件。 我对Haskell和底层操作系统之间的层(如果是这样的话)
你能确认下一个break是否取消了内部for循环吗? for (out in 1:n_old){ id_velho <- old_table_df$id[out] for
我有一个程序可以进行一些数据分析,并且有几百行长。 在程序的早期,我想做一些质量控制,如果没有足够的数据,我希望程序终止并返回到 R 控制台。否则,我希望执行其余代码。 我尝试过break、brows
我有一个 if 语句,用于检查我的对象(向导)是否命中了项目。如果他这样做了,巫师的大小将会改变,他应该能够在与障碍物的 1 次碰撞中幸存。 现在我陷入了“在 1 个障碍物碰撞中幸存”,因为在我的碰撞
我正在尝试使用迭代器来显示很多内容。我不断收到“break;”错误线。它说这是一个无法访问的声明。如有任何帮助,我们将不胜感激。 public Lot getLot(int number) {
我正在创建一个应用程序,我需要在其中处理可能非常庞大且可能需要一些时间的数据。 现在我阅读了很多关于 IntentService 的资料,实际上我已经将它实现为处理 REST 调用的通信类,但现在我试
我有一个自定义的 UITableViewCell。该单元具有三个标签。最左边的“金额”标签具有以下约束。 在单元格的右侧,我有另一个标签,“Label Dollar Amount”。它具有以下约束:
我有以下不和谐嵌入: message.reply({ content: '', embed: { color: 11416728, author
JavaScript 不是我最擅长的技能,但我会尽力解释,所以就这样吧。我有人在我的网站上创建了一个幻灯片菜单,我也使用 jplayer 音乐播放器。 现在一切正常,直到我在顶部添加此脚本。由于某种原
我已经在 Ubuntu 上安装了 android studio,有一个带有损坏图像的 API,我也尝试过重新安装。我应该怎么做才能克服这个问题。 删除它后,它没有在 sdk 管理器中显示 提前致谢。
假设我的站点上有大约 10 个 css 文件。我想把它们合二为一。但是当我组合它们时(只是“连接”文件,以便将它们包含到 html 中),我的样式/布局中断了。这不是路径问题或其他问题,只是选择器无法
我是一名优秀的程序员,十分优秀!