Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Chart with no Table

I'm trying to generate a Jasper Report using DynamicJasper which is just a chart, with no table appearing above it. There doesn't seem to be a method for hiding a column or for suppressing the table. What are my options for generating just a chart? I would be open to a solution that does not use DynamicJasper, just the 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();
}
like image 471
Eric Stein Avatar asked Oct 31 '22 21:10

Eric Stein


2 Answers

If DynamicJasper is no must for you, you could try it with the DynamicReports library.
I only did some few tests with both, but I found the DynamicReports much more easier to use.

Example code from my tests back then, a report with chart only (or uncomment the .columns section to get the columns back):

    // 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;
    }

For more examples have a look here: dynamicreports.org examples

like image 88
enricog Avatar answered Nov 08 '22 04:11

enricog


Instead of addColumn use addField. Thanks to Petter Friberg for this comment

like image 40
Raysen Jia Avatar answered Nov 08 '22 04:11

Raysen Jia