JasperReports supports a large number of built-in charts created using the popular open source library JFreeChart. Chart types include Pie, Bar, Stacked Bar, Line, Area, Bubble, Gantt, Thermometer and Meter. In this tutorial we will learn how to add a multi-series chart to a report. This tutorial uses the same example explained in Designing a Report with Jaspersoft Studio. We suggest you complete that tutorial before beginning this one.
The goal is to have the same report described in the tutorial Designing a Report with Jaspersoft Studio, with a bar chart showing the number of shipments for every city.
Placing the Graph
The report created in the earlier tutorial is composed of multiple bands, each with different print times. For example, the detail band is printed for every record read from the data source, the page header and footer are printed at the start and at the end of every page and so on. For this tutorial we will use the Summary band that is printed at the end of the report. Note that in this report the Summary band is not visible from the designer because its height is set to 0. It is, however, visible from the outline view, so select it from there and change its height in the property tab to 400 pixels, as shown in the following image:
A subreport is an element that includes a report inside another report. The idea in this tutorial is to have a subreport containing just the graph, and a main report that includes the subreport in the summary section.
For the purposes of this tutorial, we want to use two reports because in the main report we need a list of records without any aggregation so we had used the query "select * from orders" and from all the fields read we had selected the order id, the shipment name, shipment address, shipment city, shipment region. But for the graph in the subreport we don't need a list of records, just a group of them. In other words, we need the records grouped by the shipment city. For this reason we need two queries, one that returns all the records (for the main report) and one for the group (for the subreport). Because a report can have only one main query, we need to use another report for the additional query. Note that there are other ways to use more than a query on the report, but for now we choose this one to introduce the concept of subreport.
In the first step of the wizard we have three choices:
- Create a new report as subreport.
- Use an existing report as subreport.
- Put an empty report as subreport, as a placeholder.
Since we are doing this from scratch select the first option, Create a new report, and click Next.
Now we will seen a wizard almost identical to the one seen when we created the first report. This time, since we need only a blank paper with a graph select BlankA4 and hit Next.
Now we need to choose the location and the filename of the new report, put it in the standard location MyReports and as name use "GraphReport" and click Next.
Now we have to select the data adapter. Since we need to use the same data source for both reports, select the same data adapter as used in the main report. We must also provide the query to retrive the fields, so enter the query that returns the records grouped by the shipment city. Every record read from the database with this query contains two fields: the number of elements for a city and the name of the city. After that we can click Next.
At this point we can see the fields included by the second query. Click the add all button or double-click on each field until they are in the right list. After that click Next. The following step in the wizard is to create groups into the report, but this is unnecessary for our goal. Click Next again.
Now we have to select the connection to the database. Since we are using the same database as in the main report we can select the first option and click Next. In the following step in the wizard, we could create some parameters that can be passed from the main report to the subereport, but we don't need them so click the button Finish.
Now we have a new report with all his bands, but we need only one of them. In fact, we need only a band for the graph, and the band must be printed only once, so the detail band is excluded. We can choose the Title or Summary band. If we decide to choose Title, we need to remove the other bands because otherwise they will take up space unnecessarily. So for each band except Title right-click on its element in the outline view and choose Delete. The name of a deleted band appears in light gray text in the outline view, so at the end of the process the only band with a black name must be Title.
Now we can place the chart. Drag its element from the palette into the Title band to open the wizard, select the bar chart and click Next.
In the next step you can configure the series of the chart, as shown in the following image:
Then click Finish to create the chart. Since we need space, resize the Title band with an height of 350 pixels. Right-click on the chart, select Size to Container and then Fill Both. The chart will take all the space in the band. Now we can switch to the Preview tab and if it is all correct, save this subreport and switch to the main report as shown in the following image:
Now the only thing to do is to resize the subreport element to fill the Summary band, and compile the report. On the last page you will get a result like this: