You should complete the tutorial "Creating Charts and Subreports with Jaspersoft Studio" before beginning this tutorial, as it introduces the Datasets and uses them in the creation of a chart. The goal of this tutorial is to create a report with a chart in its summary, with some grouped data. Although this can be done by using a subreport, in this tutorial we will use only one report and a "dataset" element.
In this tutorial, we will use the report created during the tutorial "Designing a Report with Jaspersoft Studio" which provides us a report including all the records in the table "orders", with every record including the field order id, shipment name, shipment address, shipment city, and shipment region. At the bottom of the records, in the Summary band, we want to include a chart that shows the number of shipments for every city. If you haven't followed the tutorial "Creating Charts and Subreports with Jaspersoft Studio" you need to resize the Summary band with the following procedure. The Summary band is already present in the report used as example, but it could have an height of 0. It is, however, visible from the outline view. Select the Summary band from the Outline view and change its height in the property tab to 400 pixels, as shown in the following image:
Every report has a main dataset defined during its creation. But sometimes we need fields that are not returned by a query from the main dataset, or the fields needed may be in a different data source. In the tutorial "Creating Charts and Subreports with Jaspersoft Studio" we saw a workaround where we define more reports using only the main dataset of each report, and including them in another report using the element "Subreport". Another way to do this is using a single report and the element "Dataset". The dataset element allows you to define many datasets inside the report, each one with its own fields and data source. Every dataset is independent from the others, so its fields are separated from the ones of the main dataset, and also from the ones of the other datasets
To create a dataset, right-click on the report root node in the outline view and choose Create Dataset:
A new wizard opens. In the first step, enter a name for the new dataset and if it uses a connection to a data source or it is simply empty. Since we want to read data from the database, select the first option and use as name "GroupData", as in the image below, then click Next.
Next, we have to select the data adapters for the new dataset, and since we are using the same database as the main report , we will select Sample DB - Database JBDC Connection. We must also provide a query to retrieve the fields. Enter the query "select count(*), SHIPCITY from orders group by SHIPCITY". It returns the records grouped by the shipment city. This ensures that every record read from the database that matches this query will contain two fields: the number of element for a city and the name of the city. After that we can click Next.
At this point, the fields discovered using the provided query are displayed, and we can choose which we want to add to the report. Since we need them all click the Add All button and then Next, as in the following image:
On the next screen in the wizard, we can define groups. For this task, we don't need them so click the Finish button. The dataset is created and is visible from the Outline view. If you explore the element you can see that it has its own fields, variables and groups. We can now drag and drop the chart from the palette into the Summary section:
After the chart is placed the, creation wizard appears. On the first screen, select a Pie Chart (to have something different from the previous tutorial), and click Next. On the next screen, find the field named Dataset, normally with the value [MainDataset]. You must change this value to GroupData, so the chart will use the new dataset, as in the image below:
Now you have to configure the series of the chart. Click the button with three dots on the right of the series combo to open a new dialog:
In this dialog you can define the series of the chart with an expression. Delete the default item by selecting it and clicking Delete, then press the Add button. The expression editor opens. Select, Fields and double-click on the item SHIPCITY. Finally, click "Finish" to close the expression editor:
Now you will return to the dialog of the series. Click Ok to go to the chart configuration. Configure it as shown in the following image:
When the chart configuration is finished, click the Finish to complete the wizard. The only thing left to do is resize the chart to take up all available space in its band. Right-click on the chart, select Size to Container, and then Fit Both. The chart fills the Summary band. Next, go in the Preview tab to compile the report, then go to the last page to see the chart: