在 DynamicJasper 图表中使用 CustomExpression
我有一个使用 DynamicJasper 来创建报告的项目。到目前为止它工作得很好,但是当我想将图表添加到以前的功能报告中时,我遇到了问题。
我不断得到这个:
net.sf.jasperreports.engine.design.JRValidationException:报告设计无效: 1. 未找到字段:customExpression_for_Ganancia 在net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:258) 在net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:140) 在net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215) 在 ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperReport(DynamicJasperHelper.java:519) 在 ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:279) 在 ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:232)
Ganancia 是图表中唯一属于 CustomExpression 的列。如果我不将此列作为系列添加到图表中,图表将正确呈现。看来图表与表达式的配合不太好...
我的代码片段:
private DynamicReport buildSalesReport() throws ColumnBuilderException, ClassNotFoundException, ChartBuilderException {
DynamicReportBuilder drb = new DynamicReportBuilder();
drb.setReportName("Reporte de Ventas")
.setTitle("Reporte de ventas")
.setSubtitle("Este reporte fue generado el " + new Date())
.setPrintColumnNames(false)
.setIgnorePagination(true)
.setMargins(10, 10, 10, 10)
.setUseFullPageWidth(true);
Style groupOneStyle = new Style();
groupOneStyle.setFont(Font.ARIAL_BIG);
groupOneStyle.setHorizontalAlign(HorizontalAlign.LEFT);
groupOneStyle.setVerticalAlign(VerticalAlign.MIDDLE);
AbstractColumn columnDisplayName = ColumnBuilder.getNew()
.setColumnProperty("bookingType.displayName", String.class.getName())
.setTitle("Tipo").setWidth(new Integer(40))
.setStyle(groupOneStyle)
.build();
AbstractColumn columnDestiny = ColumnBuilder.getNew()
.setColumnProperty("bookedObject.destiny", String.class.getName())
.setTitle("Destino").setWidth(new Integer(40))
.build();
Style priceStyle = new Style();
priceStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
AbstractColumn columnCurrency = ColumnBuilder.getNew()
.setColumnProperty("bookedObject.currency.displayName", String.class.getName())
.setTitle("Cotizacion").setWidth(new Integer(5))
.setShowText(false)
.build();
Style footerStyle = new Style();
footerStyle.setFont(Font.ARIAL_MEDIUM);
footerStyle.setBorderTop(Border.THIN);
footerStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
footerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
AbstractColumn columnPrice = ColumnBuilder.getNew()
.setColumnProperty("bookedObject.price", Double.class.getName())
.setStyle(priceStyle)
.setPattern("$ 0.00")
.setTitle("Precio").setWidth(new Integer(25))
.build();
AbstractColumn columnCount = ColumnBuilder.getNew()
.setColumnProperty("count", Integer.class.getName())
.setStyle(priceStyle)
.setTitle("Cantidad").setWidth(new Integer(25))
.build();
columnCount.setName("Cantidad");
AbstractColumn columnProfit = ColumnBuilder.getNew()
.setCustomExpression(this.getProfitExpression())
.setStyle(priceStyle)
.setTitle("Ganancia").setWidth(new Integer(20))
.setPattern("$ 0.00")
.build();
columnProfit.setName("Ganancia");
GroupBuilder groupBookingTypeBuilder = new GroupBuilder();
DJGroup groupBookingType =
groupBookingTypeBuilder.setCriteriaColumn((PropertyColumn) columnDisplayName)
.setGroupLayout(GroupLayout.VALUE_IN_HEADER_WITH_HEADERS_AND_COLUMN_NAME)
.build();
GroupBuilder groupCurrencyBuilder = new GroupBuilder();
DJGroup groupCurrency =
groupCurrencyBuilder.setCriteriaColumn((PropertyColumn) columnCurrency)
.addFooterVariable(columnCount,DJCalculation.SUM,footerStyle)
.addFooterVariable(columnProfit,DJCalculation.SUM,footerStyle)
.setGroupLayout(GroupLayout.VALUE_IN_HEADER)
.build();
drb.addColumn(columnDisplayName)
.addColumn(columnCurrency)
.addColumn(columnDestiny)
.addColumn(columnCount)
.addColumn(columnPrice)
.addColumn(columnProfit)
.addGroup(groupBookingType)
.addGroup(groupCurrency)
.setPrintBackgroundOnOddRows(true);
DJAxisFormat categoryAxisFormat = new DJAxisFormat("Destino");
categoryAxisFormat.setLabelFont(Font.ARIAL_SMALL);
categoryAxisFormat.setLabelColor(Color.DARK_GRAY);
categoryAxisFormat.setTickLabelFont(Font.ARIAL_SMALL);
categoryAxisFormat.setTickLabelColor(Color.DARK_GRAY);
categoryAxisFormat.setTickLabelMask("");
categoryAxisFormat.setLineColor(Color.DARK_GRAY);
DJAxisFormat valueAxisFormat = new DJAxisFormat("Ventas / Ingresos");
valueAxisFormat.setLabelFont(Font.ARIAL_SMALL);
valueAxisFormat.setLabelColor(Color.DARK_GRAY);
valueAxisFormat.setTickLabelFont(Font.ARIAL_SMALL);
valueAxisFormat.setTickLabelColor(Color.DARK_GRAY);
valueAxisFormat.setTickLabelMask("#,##0");
valueAxisFormat.setLineColor(Color.DARK_GRAY);
DJChart djChart = new DJBarChartBuilder()
//chart
.setX(20)
.setY(10)
.setWidth(500)
.setHeight(250)
.setCentered(false)
.setBackColor(Color.LIGHT_GRAY)
.setShowLegend(true)
.setPosition(DJChartOptions.POSITION_FOOTER)
.setTitle(new StringExpression() {
@Override
public Object evaluate(Map fields, Map variables, Map parameters) {
return variables.get("bookingType.displayName");
}
})
.setTitleColor(Color.DARK_GRAY)
.setTitleFont(Font.ARIAL_BIG_BOLD)
.setSubtitle("subtitle")
.setSubtitleColor(Color.DARK_GRAY)
.setSubtitleFont(Font.COURIER_NEW_BIG_BOLD)
.setLegendColor(Color.DARK_GRAY)
.setLegendFont(Font.COURIER_NEW_MEDIUM_BOLD)
.setLegendBackgroundColor(Color.WHITE)
.setLegendPosition(DJChartOptions.EDGE_BOTTOM)
.setTitlePosition(DJChartOptions.EDGE_TOP)
.setLineStyle(DJChartOptions.LINE_STYLE_DOTTED)
.setLineWidth(1)
.setLineColor(Color.DARK_GRAY)
.setPadding(5)
//dataset
.setCategory((PropertyColumn) columnDestiny)
.addSerie(columnCount, "Cantidad")
.addSerie(columnProfit, "Ganancia") // IF I COMMENT THIS LINE THE CHART IS RENDERED
//plot
.setCategoryAxisFormat(categoryAxisFormat)
.setValueAxisFormat(valueAxisFormat)
.build();
drb.addChart(djChart);
HashMap vars = new HashMap();
vars.put(columnCount, new JRDesignVariable());
vars.put(columnProfit, new JRDesignVariable());
JRDesignGroup group = new JRDesignGroup();
djChart.transform(new DynamicJasperDesign(), "", group, group, vars, 0);
DynamicReport dr = drb.build();
return dr;
}
public JasperPrint getJasperPrint(String status, String userOwner,
String hourFrom, String hourTo, String dateFrom, String dateTo)
throws ColumnBuilderException, ClassNotFoundException, JRException, ChartBuilderException {
DynamicReport dr = this.buildSalesReport();
JRDataSource ds = new JRBeanCollectionDataSource(
this.bookService.getReportBooks(status, userOwner, hourFrom, hourTo, dateFrom, dateTo));
return DynamicJasperHelper.generateJasperPrint(dr , new ClassicLayoutManager(), ds);
}
/**
*
* @return
*/
@SuppressWarnings("serial")
private CustomExpression getProfitExpression() {
return new CustomExpression() {
@SuppressWarnings("rawtypes")
@Override
public Object evaluate(Map fields, Map variables, Map parameters) {
Double amount = (Integer)fields.get("count") * (Double)fields.get("bookedObject.price");
return amount;
}
@Override
public String getClassName() {
return Double.class.getName();
}
};
正如我所说,没有图表时报表可以正确显示,有了图表,只有当表达式列作为系列包含时才会失败。
欢迎任何想法!
I have a project using DynamicJasper to create reports. It works fine so far, but when I wanted to add a chart to a previously functional report I run into issues.
I keep getting this:
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid :
1. Field not found : customExpression_for_Ganancia
at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:258)
at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:140)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperReport(DynamicJasperHelper.java:519)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:279)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:232)
Ganancia being the only column in the chart that is a CustomExpression. If I don't add this column as a series to the chart, the chart renders properly. It seems the chart doesn't play well with expressions...
A snippet from my code:
private DynamicReport buildSalesReport() throws ColumnBuilderException, ClassNotFoundException, ChartBuilderException {
DynamicReportBuilder drb = new DynamicReportBuilder();
drb.setReportName("Reporte de Ventas")
.setTitle("Reporte de ventas")
.setSubtitle("Este reporte fue generado el " + new Date())
.setPrintColumnNames(false)
.setIgnorePagination(true)
.setMargins(10, 10, 10, 10)
.setUseFullPageWidth(true);
Style groupOneStyle = new Style();
groupOneStyle.setFont(Font.ARIAL_BIG);
groupOneStyle.setHorizontalAlign(HorizontalAlign.LEFT);
groupOneStyle.setVerticalAlign(VerticalAlign.MIDDLE);
AbstractColumn columnDisplayName = ColumnBuilder.getNew()
.setColumnProperty("bookingType.displayName", String.class.getName())
.setTitle("Tipo").setWidth(new Integer(40))
.setStyle(groupOneStyle)
.build();
AbstractColumn columnDestiny = ColumnBuilder.getNew()
.setColumnProperty("bookedObject.destiny", String.class.getName())
.setTitle("Destino").setWidth(new Integer(40))
.build();
Style priceStyle = new Style();
priceStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
AbstractColumn columnCurrency = ColumnBuilder.getNew()
.setColumnProperty("bookedObject.currency.displayName", String.class.getName())
.setTitle("Cotizacion").setWidth(new Integer(5))
.setShowText(false)
.build();
Style footerStyle = new Style();
footerStyle.setFont(Font.ARIAL_MEDIUM);
footerStyle.setBorderTop(Border.THIN);
footerStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
footerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
AbstractColumn columnPrice = ColumnBuilder.getNew()
.setColumnProperty("bookedObject.price", Double.class.getName())
.setStyle(priceStyle)
.setPattern("$ 0.00")
.setTitle("Precio").setWidth(new Integer(25))
.build();
AbstractColumn columnCount = ColumnBuilder.getNew()
.setColumnProperty("count", Integer.class.getName())
.setStyle(priceStyle)
.setTitle("Cantidad").setWidth(new Integer(25))
.build();
columnCount.setName("Cantidad");
AbstractColumn columnProfit = ColumnBuilder.getNew()
.setCustomExpression(this.getProfitExpression())
.setStyle(priceStyle)
.setTitle("Ganancia").setWidth(new Integer(20))
.setPattern("$ 0.00")
.build();
columnProfit.setName("Ganancia");
GroupBuilder groupBookingTypeBuilder = new GroupBuilder();
DJGroup groupBookingType =
groupBookingTypeBuilder.setCriteriaColumn((PropertyColumn) columnDisplayName)
.setGroupLayout(GroupLayout.VALUE_IN_HEADER_WITH_HEADERS_AND_COLUMN_NAME)
.build();
GroupBuilder groupCurrencyBuilder = new GroupBuilder();
DJGroup groupCurrency =
groupCurrencyBuilder.setCriteriaColumn((PropertyColumn) columnCurrency)
.addFooterVariable(columnCount,DJCalculation.SUM,footerStyle)
.addFooterVariable(columnProfit,DJCalculation.SUM,footerStyle)
.setGroupLayout(GroupLayout.VALUE_IN_HEADER)
.build();
drb.addColumn(columnDisplayName)
.addColumn(columnCurrency)
.addColumn(columnDestiny)
.addColumn(columnCount)
.addColumn(columnPrice)
.addColumn(columnProfit)
.addGroup(groupBookingType)
.addGroup(groupCurrency)
.setPrintBackgroundOnOddRows(true);
DJAxisFormat categoryAxisFormat = new DJAxisFormat("Destino");
categoryAxisFormat.setLabelFont(Font.ARIAL_SMALL);
categoryAxisFormat.setLabelColor(Color.DARK_GRAY);
categoryAxisFormat.setTickLabelFont(Font.ARIAL_SMALL);
categoryAxisFormat.setTickLabelColor(Color.DARK_GRAY);
categoryAxisFormat.setTickLabelMask("");
categoryAxisFormat.setLineColor(Color.DARK_GRAY);
DJAxisFormat valueAxisFormat = new DJAxisFormat("Ventas / Ingresos");
valueAxisFormat.setLabelFont(Font.ARIAL_SMALL);
valueAxisFormat.setLabelColor(Color.DARK_GRAY);
valueAxisFormat.setTickLabelFont(Font.ARIAL_SMALL);
valueAxisFormat.setTickLabelColor(Color.DARK_GRAY);
valueAxisFormat.setTickLabelMask("#,##0");
valueAxisFormat.setLineColor(Color.DARK_GRAY);
DJChart djChart = new DJBarChartBuilder()
//chart
.setX(20)
.setY(10)
.setWidth(500)
.setHeight(250)
.setCentered(false)
.setBackColor(Color.LIGHT_GRAY)
.setShowLegend(true)
.setPosition(DJChartOptions.POSITION_FOOTER)
.setTitle(new StringExpression() {
@Override
public Object evaluate(Map fields, Map variables, Map parameters) {
return variables.get("bookingType.displayName");
}
})
.setTitleColor(Color.DARK_GRAY)
.setTitleFont(Font.ARIAL_BIG_BOLD)
.setSubtitle("subtitle")
.setSubtitleColor(Color.DARK_GRAY)
.setSubtitleFont(Font.COURIER_NEW_BIG_BOLD)
.setLegendColor(Color.DARK_GRAY)
.setLegendFont(Font.COURIER_NEW_MEDIUM_BOLD)
.setLegendBackgroundColor(Color.WHITE)
.setLegendPosition(DJChartOptions.EDGE_BOTTOM)
.setTitlePosition(DJChartOptions.EDGE_TOP)
.setLineStyle(DJChartOptions.LINE_STYLE_DOTTED)
.setLineWidth(1)
.setLineColor(Color.DARK_GRAY)
.setPadding(5)
//dataset
.setCategory((PropertyColumn) columnDestiny)
.addSerie(columnCount, "Cantidad")
.addSerie(columnProfit, "Ganancia") // IF I COMMENT THIS LINE THE CHART IS RENDERED
//plot
.setCategoryAxisFormat(categoryAxisFormat)
.setValueAxisFormat(valueAxisFormat)
.build();
drb.addChart(djChart);
HashMap vars = new HashMap();
vars.put(columnCount, new JRDesignVariable());
vars.put(columnProfit, new JRDesignVariable());
JRDesignGroup group = new JRDesignGroup();
djChart.transform(new DynamicJasperDesign(), "", group, group, vars, 0);
DynamicReport dr = drb.build();
return dr;
}
public JasperPrint getJasperPrint(String status, String userOwner,
String hourFrom, String hourTo, String dateFrom, String dateTo)
throws ColumnBuilderException, ClassNotFoundException, JRException, ChartBuilderException {
DynamicReport dr = this.buildSalesReport();
JRDataSource ds = new JRBeanCollectionDataSource(
this.bookService.getReportBooks(status, userOwner, hourFrom, hourTo, dateFrom, dateTo));
return DynamicJasperHelper.generateJasperPrint(dr , new ClassicLayoutManager(), ds);
}
/**
*
* @return
*/
@SuppressWarnings("serial")
private CustomExpression getProfitExpression() {
return new CustomExpression() {
@SuppressWarnings("rawtypes")
@Override
public Object evaluate(Map fields, Map variables, Map parameters) {
Double amount = (Integer)fields.get("count") * (Double)fields.get("bookedObject.price");
return amount;
}
@Override
public String getClassName() {
return Double.class.getName();
}
};
As I said, the report is shown properly without the chart, with the chart, it fails only if the expression column is included as a series.
Any ideas are welcomed!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
刚刚在提交 05243a3 中推送了 DJ 4.0.1 的更改,
今天有时也会推送 DJ 3.X
Just pushed the change for DJ 4.0.1 in commit 05243a3
Sometime today will also push for DJ 3.X
我通过执行以下操作解决了同样的问题:
3.正如您已经想到的,您必须为此任务创建自己的 LayoutManager。
4.这可能不是最好的解决方案,它只是一个如何填补 DynamicJasper 空白的示例。
I've solved this very same problem doing the following:
3 . As you have already figured out, you'll have to create your own LayoutManager for this task.
4 . This may not be the best solution, it is just an example how to fill the gap of DynamicJasper.
我遇到了同样的问题,但我有一个稍微不同的解决方案。 Column 类有不同类型,但图表的
AbstractLayoutManager
中仅支持PropertyColumn
类。我发现当您使用CustomExpression
时,使用的底层Column
类是ExpressionColumn
。因此,我修改了ar.com.fdvs.dj.core.layout.AbstractLayoutManager
中的“protected MapregisterChartVariable()
”方法来支持ExpressionColumn
.我在该方法中更改了以下 3 行代码:
更改为以下内容:
这解决了我的问题,我不再收到“未找到字段”消息。
I ran into the same problem, but I had a slightly different solution. There are different types of Column classes, but only the
PropertyColumn
class is supported in theAbstractLayoutManager
for charts. I found out that when you use aCustomExpression
, the underlyingColumn
class that is used is anExpressionColumn
. So, I modified the "protected MapregisterChartVariable()
method in thear.com.fdvs.dj.core.layout.AbstractLayoutManager
to supportExpressionColumn
.I changed the following 3 lines of code in that method:
To the following:
This resolved the problem for me and I no longer get the "Field not found" message.