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
GROUP BY YEAR(ORDERDATE), MONTH(ORDERDATE)
ORDER BY YEAR(ORDERDATE), MONTH(ORDERDATE)

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.

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.

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

Feedback