Creating charts

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. We start creating a blank report based on the following SQL query (we use the JasperReports Sample db):

select count(*) orders, year(ORDERDATE) year, MONTH(ORDERDATE) month
from orders

The query returns the amount of orders placed in each month in the several years. Open the query dialog, paste the query (be sure the JasperReports Sample db is the active connection) and read the fields from the query.

From the palette drag the chart element inside the title band and select the Bar 3D chart.

Figure 1

Right click the chart and select Chart data. This will pop up the Chart data dialog. Select the tab details and add a new category. Set as Series expression the field Year ($F{YEAR}), as category the Month and as value the number of orders as shown in the following figure.

Figure 2

Run your report. The following picture shows the final result.

Figure 3