Adding charts

Adding charts with DynamicJasper is done through the addChart method of the FastReportBuilder class. The following snippet of code shows the full configuration of the Monthly Capacity Report:

DynamicReportBuilder reportBuilder = new FastReportBuilder()
.setUseFullPageWidth(true)
.setTitle("Monthly Capacity Report")
.setWhenNoData("(no data)", new Style())
.addAutoText("CONFIDENTIAL", AutoText.POSITION_HEADER, AutoText.ALIGMENT_LEFT, 200, new Style())
.addAutoText(LocalDateTime.now().toString(), AutoText.POSITION_HEADER, AutoText.ALIGNMENT_RIGHT, 200, new Style())
.addColumn(monthColumn = ColumnBuilder.getNew()
.setColumnProperty("monthName", String.class)
.setTitle("Month")
.build())
.addColumn(callsColumn = ColumnBuilder.getNew()
.setColumnProperty("calls", Integer.class)
.setTitle("Calls")
.build())
.addChart(new DJBar3DChartBuilder()
.setCategory((PropertyColumn) monthColumn)
.addSerie(callsColumn)
.build());

Notice how we need a reference to the columns containing the data we want to use in the chart. The setCategory and addSeries methods accept these references.

In order to render charts, we must configure an ImageServlet, which is provided by the JasperReports library. This servlet serves the images that make up the charts. In the example application for this chapter, the servlet is declared in the WebConfig class as a static inner class:

@WebServlet("/image")
public static class ReportsImageServlet extends ImageServlet {
}

You can use any suitable URL. This needs to be configured in the output used by the exporter class (for example, HTMLExporter). Additionally, the JasperPrint instance has to be set in the HTTP session. The following snippet of code shows the extra configuration needed when rendering charts:

...
JasperPrint print = ...
VaadinSession.getCurrent().getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);

SimpleHtmlExporterOutput exporterOutput = ...
exporterOutput.setImageHandler(new WebHtmlResourceHandler("image?image={0}"));

HtmlExporter exporter = new HtmlExporter();
exporter.setExporterOutput(exporterOutput);
...

The WebHtmlResourceHandler constructor accepts a string with the URL pattern to use by the internal image handler in the exporter. Notice how the pattern starts with image. This is the same value used in the ImageServlet mapping.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset