Creation of a Chart


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, using the datasource created in the tutorial Creation of a Dataset. With the report opened, drag and drop it from the palette into the Summary section:

Figure 1. Drag the chart into the report

Drag the chart into the report

After the chart is placed the creation wizard will appear. On the first step select a "Pie Chart", to have something different from the previous tutorial, and hit Next. In the following step find the field named "Dataset", normally with the value "[MainDataset]". You must change this value to "GroupData" (to create this dataset follow the create a dataset tutorial), so the chart will use the new dataset, as in the image below:

Figure 2. Select the dataset for the Chart

Select the dataset for the Chart

Now you have to configure the series of the chart, click the button with three dots on the right of the series combo, a new dialog will appear:

Figure 3. Add the Series

Add the Series

In this dialog you can define the series of the chart, even using an expression. Delete the default item by selecting it and clicking the "Delete" button, and then press the "Add" button. The expression editor will be opened, and here select the item "Fields" and double click on the item "SHIPCITY", finally click "Finish" to close the expression editor:

Figure 4. Select the Field SHIPCITY for the series

Select the Field SHIPCITY for the series

Now you will return to the dialog of the series, here just hit "Ok" to go to the chart configuration. Configure it as shown in the following image:

Figure 5. Finish to configure the Charts

Finish to configure the Charts

The chart configuration is finished, click the button "Finish" to complete the wizard. The only thing to do is give to the chart all the avaiable space in its band, so right click on the chart, select "Size to Container" and then "Fit Both". Doing this the chart will fill the Summary band, now go in the "Preview" tab to compile the report and go to the last page to see the chart:

Figure 6. Run the Report

Run the Report