To design JasperReports report, select “File -> New
Document” from the menu. Enter the report name in the “Report name” field and
click on the “OK” button to open a blank report design.
Adding Static Text
To add Static text to the report, click on the
"T" icon at the top of the screen or select “Edit ->Insert Element
-> Static Text” from menu and place it where you want to display the text.
Figure 6
Adding Report Query
To retrieve data from the datasources select “Data->
Report query” from the menu to open the Report query dialog. Make sure that “Automatically
Retrieve Fields” checkbox is checked. Now write your SQL query in the textbox.
Click the “OK” button to close the dialog.
Figure 7
Adding Text Fields
Report fields are used to display data retrieved from the
datasources into the report. All the fields must have the same name and type as
the columns they map .To add fields to the report click on the "F"
icon at the top of the screen or select “Edit ->Insert Element-> Text
Field” from the menu and place it where you want to display the field. Or
simply drag and drop them from Fields onto the section of the report where you
want.
Figure 8
Adding Images
To add an image to the report, select "Edit->Insert
Element->Image" from the menu. Go to the properties of image field and
click the “Image” tab. Give the path for the image by clicking on the “Find”
button.
Figure 9
Adding Parameters
Parameters are used to dynamically modify SQL Query. To add
a parameter to the report select “View -> Parameters” from the menu, then
Click on the “New” tab. In the “Add/modify parameter” dialog enter/select the
following values and click the “OK” button afterwards.
·
Enter the Parameter Name (e.g. City).
·
Select theParameter class type (e.g. java.lang.String).
·
Make “Is for prompting” checkbox checked so that when you run the
report from iReport it will prompt you to enter the value for that parameter.
·
Enter the Default value expression (e.g. “Bombay”).
Figure 10
Adding variables
A report variable is used to perform various calculations on
the corresponding expressions. Here are some important things that you should
follow when designing the report using variables.
·
The variable class type must be matched with class type of the
field on which it will perform calculation.
·
The calculation type tells JasperReports what to do.
·
The variable expression represents the value to be evaluated in
the calculation. It may be Fields, Variables, Parameters, wizards, recent
expressions, etc.
·
As a variable can reference other variables, so the referenced
variables must be defined previously in the report design. So the order of
declaring variables is important in report design.
To add the report variables, select
"View->Variables" from menu and click on the “New” button. In the
“Add/modify variable” dialog enter/select the following and click the “OK”
button afterwards (see figure 11).
·
Enter the Variable Name (total).
·
Select the Variable Class Type (java.lang.BigDecimal).
·
Select the Calculation Type (Sum).
·
Select the Variable Expression ($F{QTY_SALES).
Figure 11
Now drag and drop this variable into the columnFooter, lastPageFooter
or Summary section to display result.
Adding Groups
Groups in a report define a collection of related records. A
report can contain more than one group and a group can contain another group, so
the order of declaring a group in a report design is important.
Suppose you want to create a report that will group salesman
by state and calculate total number of item sold by each group. For that, first
define a report group by selecting "View/Report Groups" menu and then
click the “New” button. In the “Add/modify group” dialog Enter Group name and Group
Expression, then click the “OK” button.
Figure 12
To create a report using the group you must write the SQL
query properly ("order by state"). When you add a new group, you will
see two new sections at the top and bottom of detail section. In this example
it is called "stateNameHeader" and "stateNameFooter." Suppose
you want to display the total number of items sold at the end of each group.
For this create a variable ($V{totalQty}) and make the “Reset type” as a group,
then drag the variable into the "stateNameFooter" section. The
generated report looks like the following figure.
Figure 13
Adding Charts
Using iReport you can design reports with charts.
JasperReports currently supports the following types of charts: Pie, Pie 3D,
Bar, Bar 3D, XY Bar, Stacked Bar, Stacked Bar 3D, Line, XY Line, Area, YX Area,
Scatter, Bubble, Time series, High Low, Candlestick, meter, thermometer, and MultiAxis.
Chart parameters are different for each type of chart.
Suppose you want to add a pie chart that shows the total
number of items sold by each salesperson at the end. For that, first add a new
chart in the summary section using the "Edit->Insert
Element->Chart" menu. Choose a chart. Open the properties by right
clicking on the chart then go to the “Chart” tab. Click on the “Edit Chart
Properties” button and go to the “Chart data -> Details.” In the Chart
properties dialog enter the three expression as shown in the following figure
and click the “Close” button afterwards.
Figure 14
Now the user has to make the desired visual settings for it.
When you run the report, you will get a pie chart at the end of your report as
shown in the following figure.
Figure 15