簡體   English   中英

在DynamicJasper圖表中使用CustomExpression

[英]Using a CustomExpression in a DynamicJasper chart

我有一個使用DynamicJasper創建報告的項目。 到目前為止,它仍然可以正常工作,但是當我想向以前的功能報告中添加圖表時,遇到了問題。

我不斷得到這個:

net.sf.jasperreports.engine.design.JRValidationException:報告設計無效:1.找不到字段:net.sf處net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:258)處的customExpression_for_Ganancia。 net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215)上的jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:140)at ar.com.fdvs.dj.core.DynamicJasperHelper(generate) ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:279)上的DynamicJasperHelper.java:519)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();
        }
    };

就像我說過的那樣,該報告在沒有圖表的情況下正確顯示,只有在將表達式列作為系列包含時,該報告才會失敗。

任何想法都歡迎!

剛剛在提交05243a3中推送了DJ 4.0.1的更改

今天的某個時候還將推動DJ 3.X

我已經通過以下方法解決了這個非常相同的問題:

  1. 設置列的“ fieldDescription”。
  2. 重新編寫類AbstractLayoutManager的“受保護的映射registerChartVariable(ar.com.fdvs.dj.domain.chart.DJChart圖表)”方法:

JRDesignExpression表達式=新的JRDesignExpression();

 String property = ((PropertyColumn) col).getFieldDescription(); 

//(((PropertyColumn)col).getColumnProperty()。getProperty();

 expression.setText("$F{" + property + "}"); expression.setValueClass(clazz); 

3。 正如您已經知道的那樣,您必須為此任務創建自己的LayoutManager。 4。 這可能不是最佳解決方案,它只是如何填補DynamicJasper空白的一個示例。

我遇到了同樣的問題,但解決方案略有不同。 Column類的類型不同,但是AbstractLayoutManager僅支持針對圖表的PropertyColumn類。 我發現,當您使用CustomExpression時,使用的基礎Column類是ExpressionColumn 因此,我修改了ar.com.fdvs.dj.core.layout.AbstractLayoutManager的“受保護的map registerChartVariable()方法”以支持ExpressionColumn

我在該方法中更改了以下3行代碼:

JRDesignExpression expression = new JRDesignExpression();
expression.setText("$F{" + ((PropertyColumn) col).getColumnProperty().getProperty()  + "}");
expression.setValueClass(clazz);

要以下內容:

if (col instanceof ExpressionColumn) {
    ExpressionColumn expCol = (ExpressionColumn) col;
    expression.setText(expCol.getTextForExpression());
    expression.setValueClassName(expCol.getExpression().getClassName());
} else {
    expression.setText("$F{" + ((PropertyColumn) col).getColumnProperty().getProperty()  + "}");
    expression.setValueClass(clazz);
}

這為我解決了問題,並且我不再收到“ 找不到字段 ”消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM