显示没有表格的图表

时间:2015-08-19 18:10:21

标签: jasper-reports dynamic-jasper

我尝试使用DynamicJasper生成一个Jasper报告,这只是一个图表,上面没有任何表格。似乎不是隐藏列或抑制表的方法。我只选择生成图表有哪些选择?我会接受一个不使用DynamicJasper的解决方案,只使用JasperReports java API。

public void go() throws ColumnBuilderException, ClassNotFoundException, JRException {
    super();

    final AbstractColumn areas = ColumnBuilder.getNew()
            .setColumnProperty("area", String.class.getName())
            .setTitle("Area")
            .setWidth(30)
            .build();

    final AbstractColumn exercises = ColumnBuilder.getNew()
            .setColumnProperty("exercises", Integer.class.getName())
            .setTitle("Exercises")
            .setWidth(30)
            .build();

    final DynamicReportBuilder reportBuilder = new DynamicReportBuilder();
    reportBuilder.addColumn(areas);
    reportBuilder.addColumn(exercises);
    reportBuilder.setUseFullPageWidth(true);

    final GroupBuilder groupBuilder = new GroupBuilder();
    groupBuilder.setCriteriaColumn((PropertyColumn) areas);
    groupBuilder.setGroupLayout(GroupLayout.EMPTY);
    final DJGroup group = groupBuilder.build();

    reportBuilder.addGroup(group);
    reportBuilder.addChart(this.chart((PropertyColumn) areas, exercises));

    final DynamicReport dynamicReport = reportBuilder.build();
    final JasperPrint jasperPrint =
            DynamicJasperHelper.generateJasperPrint(
                    dynamicReport,
                    new ClassicLayoutManager(),
                    new JRBeanCollectionDataSource(this.getData()));

    /* PDF */
    final JRPdfExporter exporter = new JRPdfExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("/tmp/my.pdf"));
    exporter.exportReport();
}

3 个答案:

答案 0 :(得分:1)

如果您必须DynamicJasper,则可以使用DynamicReports库进行试用 我只对两者进行了一些测试,但我发现DynamicReports更容易使用。

我的测试中的示例代码,仅包含图表的报告(或取消注释.columns部分以取回列):

    // First simple test
    public static void createDynamicReportTest1()
    {
        StyleBuilder boldStyle = stl.style().bold();
        StyleBuilder boldCenteredStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
        StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle)
                .setBorder(stl.pen1Point())
                .setBackgroundColor(Color.LIGHT_GRAY);
        FontBuilder boldFont = stl.fontArialBold().setFontSize(12);

        // title,     field name     data type
        TextColumnBuilder<String> itemColumn = col.column("Item", "item", type.stringType());
        TextColumnBuilder<Integer> quantityColumn = col.column("Quantity", "quantity", type.integerType());
        TextColumnBuilder<BigDecimal> unitPriceColumn = col.column("Unit price", "unitprice", type.bigDecimalType());
        //price = unitPrice * quantity
        TextColumnBuilder<BigDecimal> priceColumn = unitPriceColumn.multiply(quantityColumn).setTitle("Price");
        PercentageColumnBuilder pricePercColumn = col.percentageColumn("Price %", priceColumn);
        TextColumnBuilder<Integer> rowNumberColumn = col.reportRowNumberColumn("No.")
                //sets the fixed width of a column, width = 2 * character width
                .setFixedColumns(2)
                .setHorizontalAlignment(HorizontalAlignment.CENTER);

         AbstractBaseChartBuilder chart = cht.barChart()
                .setTitle("Bar chart")
                //.setTitleFont(boldFont)
                .setCategory(itemColumn)
                .series(
                        cht.serie(quantityColumn))
                .setCategoryAxisFormat(
                        cht.axisFormat().setLabel("Item"));

        try {
            report()//create new report design
                    .setColumnTitleStyle(columnTitleStyle)
                    .highlightDetailEvenRows()
//                    .columns(//add columns
//                            rowNumberColumn, itemColumn, quantityColumn, unitPriceColumn, priceColumn, pricePercColumn)
                    .title(cmp.text("Getting started").setStyle(boldCenteredStyle))//shows report title
                    .summary(chart)
                    .pageFooter(cmp.pageXofY().setStyle(boldCenteredStyle))//shows number of page at page footer
                    .setDataSource(createDRDataSource())//set datasource
                    .show();//create and show report
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

     public static JRDataSource createDRDataSource() {
        DRDataSource dataSource = new DRDataSource("item", "quantity", "unitprice");
        dataSource.add("Notebook", 1, new BigDecimal(500));
        dataSource.add("DVD", 5, new BigDecimal(30));
        dataSource.add("DVD", 1, new BigDecimal(28));
        dataSource.add("DVD", 5, new BigDecimal(32));
        dataSource.add("Book", 3, new BigDecimal(11));
        dataSource.add("Book", 1, new BigDecimal(15));
        dataSource.add("Book", 5, new BigDecimal(10));
        dataSource.add("Book", 8, new BigDecimal(9));
        return dataSource;
    }

有关更多示例,请查看此处: dynamicreports.org examples

答案 1 :(得分:1)

而不是if ([messageStr length] > 0) { NSString *jabberID = [UserDefaults stringForKey:kUserID]; NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName]; XMPPMessage * message = [[XMPPMessage alloc] init]; [message addAttributeWithName:@"body" stringValue:messageStr]; [message addAttributeWithName:@"sender" stringValue:userName]; [DELEGATE.xmppClient.xmppRoom sendMessage:message]; self.txtMessageField.text = @""; NSMutableDictionary *m = [[NSMutableDictionary alloc] init]; [m setObject:[messageStr substituteEmoticons] forKey:@"msg"]; [m setObject:@"you" forKey:@"sender"]; [m setObject:[NSString getCurrentTime] forKey:@"time"]; [self.messages addObject:m]; [self.messagesTableView reloadData]; } 使用addColumn。感谢此评论的Petter Friberg

答案 2 :(得分:0)

我知道这是一个老问题,但是在花了数小时试图找出解决方案之后,我想分享我的解决方案。

不幸的是,在使用DynamicJasper 5.1.0时,以上两个答案对我不起作用。 DynamicReports似乎不再维护并且链接断开,仅使用addField而不是addColumn会导致无用的“重复声明参数:dataset_null_chart”错误-尽管我只是在事实中,这似乎与重复图表有关使用单个图表。 (话虽这么说,我很想听听别人是否/如何使用DynamicJasper 5.1.x克服了这些问题)

所以我想出了以下解决方案-创建一个不呈现表的LayoutManager

public class NoTableLayoutManager extends ClassicLayoutManager {
    @Override
    protected List<AbstractColumn> getVisibleColumns() {
        return new ArrayList<>(); // hide all columns
    }
}

然后在生成JasperPrint对象时使用它:

JasperPrint print = DynamicJasperHelper.generateJasperPrint(report, new NoTableLayoutManager(), data);

这对于我创建不带表的堆积条形图的简单方案非常适用。隐藏表时,我没有尝试过更复杂的报表,所以我不知道这在每种情况下是否都可以使用,但是至少在DynamicJasper 5.1.0中,它应该可以在您要求的情况下使用。