Graphical Report Design with iReport: Part 2

Exclusive offer: get 50% off this eBook here
JasperReports 3.5 for Java Developers

JasperReports 3.5 for Java Developers — Save 50%

Create, Design, Format, and Export Reports with the world's most popular Java reporting library

$26.99    $13.50
by David R. Heffelfinger | August 2009 | Java Open Source

In the previous part of the article by David Heffelfinger, we briefed you about an overview of iReport, obtaining and setting up the right iReport for your machine environment, and creating a simple report using iReport. In this second part of the article, we will learn to create more elaborate reports by using iReport's graphical user interface to set report attributes.

By the end of this article, you should be able to:

  • Add multiple columns to a report
  • Group report data
  • Add images and charts to a report

Creating more elaborate reports

In this section, we will modify the simple report we created in the previous article to illustrate how to add images, charts, and multiple columns to a report. We will also see how to group report data. We will perform all of these tasks graphically with iReport.

Adding images to a report

Adding static images to a report is very simple with iReport. Just drag the Image component from the Palette to the band where it will be rendered in the report.

Graphical Report Design with iReport: Part 2

When we drop the image component into the appropriate band, a window pops up asking us to specify the location of the image file to display.

Graphical Report Design with iReport: Part 2

After we select the image, we can drag it to its exact location where it will be rendered.

Graphical Report Design with iReport: Part 2

As we can see, adding images to a report using iReport couldn't be any simpler.

Adding multiple columns to a report

The report we've been creating so far in this article (continued from the first part of this article, Graphical Report Design with iReport: Part 1, for database details, refer to Database for our reports section of Creating Dynamic Reports from Databases) contains over 11,000 records. It spans over 300 pages. As we can see, there is a lot of space between the text fields. Perhaps it would be a good idea to place the text fields closer together and add an additional column. This would cut the number of pages in the report by half.

To change the number of columns in the report, we simply need to select the root report node in the Report Inspector window at the top left and then modify its Columns property in the Properties window at the bottom right.

Graphical Report Design with iReport: Part 2

When we modify the Columns property, iReport automatically modifies the Column Width property to an appropriate value. We are free, of course, to modify this value if it doesn't meet our needs.

As our report now contains more than one column, it makes sense to re-add the Column Header band we deleted earlier. This can be done by right-clicking on the band in the Report Inspector window and selecting Add Band.

Graphical Report Design with iReport: Part 2

Next, we need to move the static text in the page header to the Column Header band. To move any element from one band to another, all we need to do is drag it to the appropriate band in the Report Inspector window.

Graphical Report Design with iReport: Part 2

Next, we need to resize and reposition the text fields in the Detail band and the static text elements in the Column Header band so that they fit in the new, narrower width of the columns. Also, resize the Column Header band to avoid having too much whitespace between the elements of the Column Header and Detail bands. Our report now looks like this:

Graphical Report Design with iReport: Part 2

We can see the resulting report by clicking on Preview.

Graphical Report Design with iReport: Part 2

Grouping report data

Suppose we are asked to modify our report so that data is divided by the state where the aircraft is registered. This is a perfect situation to apply report groups. Report groups allow us to divide report data when a report expression changes.

To define a report group, we need to right-click on the root report node in the Report Inspector window, and then select Add Report Group.

Graphical Report Design with iReport: Part 2

Then, enter the Group name and indicate whether we want to group by a field or by a report expression. In our case, we want to group the data by state field.

Graphical Report Design with iReport: Part 2

After clicking on Next>, we need to indicate whether we want to add a group header and/or footer to our report.

Graphical Report Design with iReport: Part 2

For aesthetic purposes, we move the static text fields in the Column Header band to the Group Header band, remove the column and page header bands, and add additional information to the Group Header band. After making all of these changes, our report preview will look like this:

Graphical Report Design with iReport: Part 2

We can preview the report by clicking Preview.

Graphical Report Design with iReport: Part 2

JasperReports 3.5 for Java Developers Create, Design, Format, and Export Reports with the world's most popular Java reporting library
Published: August 2009
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

Adding charts to a report

To add a chart to a report, we need to drag the Chart component from the Palette into the approximate location where the chart will be rendered in the report.

Graphical Report Design with iReport: Part 2

When dropping the chart component into the report, the following window will pop up, allowing us to select the type of chart we want to add to the report:

Graphical Report Design with iReport: Part 2

For this example, we will add a 3D bar chart to the report. All that needs to be done is to click on the appropriate chart type, and then click on the OK button.

Our chart will graphically illustrate the number of aircraft registered in each state of the United States. (We will explain how to have the chart display the appropriate data later in this section.) We will place the chart in the Summary band at the end of the report. As the chart will illustrate a lot of data, we need to resize the Summary band so that our chart can fit. After resizing the Summary band, outlining the area of the report to be covered by the chart, and selecting the chart type, the Summary section of our report preview looks like this:

Graphical Report Design with iReport: Part 2

To fine-tune the appearance of the chart, we can select it in the Report Inspector window and then modify its properties as necessary in the Properties window.

Graphical Report Design with iReport: Part 2

To specify the data that will be displayed in the chart, we need to right-click on the chart in the Report Inspector window and select Chart Data. We then need to click on the Details tab in the resulting pop-up window.

Graphical Report Design with iReport: Part 2

We then need to click on the Add button to add a new Category series.

Graphical Report Design with iReport: Part 2

The Series expression field is the name of the series. Its value can be any object that implements java.lang.Comparable. In most cases, the value of this field is a string.

The Category expression field is the label of each value in the chart. The value of this field is typically a string. In our example, each state is a different category, so we will use the state field ($F{state}) as our category expression.

The Value expression field is a numeric value representing the value to be charted for a particular category. In our example, the number of aircraft in a particular state is the value we want to chart. Therefore, we use the implicit stateGroup_COUNT variable ($V{stateGroup_COUNT}) as our value expression.

The optional Label Expression field allows us to customize item labels in the chart.

Every time we create a group in a report template, an implicit variable named groupName_COUNT is created, where groupName is the name of the group.

We can either type in a value for the Series expression, Category expression, and Value expression fields, or we can click on the Graphical Report Design with iReport: Part 2 icon to be able to graphically select the appropriate expression using iReport's Expression editor.

Graphical Report Design with iReport: Part 2

Using the Expression editor, we can select any parameter, field, or variable as our expression. We can also use user-defined expressions to fill out any of the fields that require a valid JasperReports expression.

After selecting the appropriate expressions for each of the fields, our chart details are as follows:

Graphical Report Design with iReport: Part 2

After clicking on OK and closing the Chart details window, we are ready to view our chart in action, which can be done simply by clicking on Preview.

Graphical Report Design with iReport: Part 2

Help and support

Although this article didn't discuss every iReport feature, I'm confident that iReport is intuitive enough after you get comfortable with it. Some of the iReport features not covered in this article include subreport creation and adding crosstabs, lines, ellipses, and rectangles to a report. However, we have learned all these features the "hard" way by creating a JRXML template by hand. For someone familiar with JasperReports, adding these features to a report created by iReport should be trivial. If more help is needed, JasperSoft provides additional documentation for iReport, and lots of knowledgeable people frequent the iReport forums at http://jasperforge.org/plugins/espforum/browse.php?group_id=83&forumid=101.

Summary

This two-part particle taught us how to install and set up iReport, use iReport's Report Wizard to quickly generate a report, and graphically design custom reports. Moreover, we learned how to group report data graphically with iReport, to add multiple columns to a report, and to add images and charts to a report graphically with iReport.

iReport is a very powerful tool that can significantly reduce report design time. To use all of the features of iReport effectively, however, an iReport user must be familiar with basic JasperReports concepts, such as bands, report variables, report fields, and so on.

JasperReports 3.5 for Java Developers Create, Design, Format, and Export Reports with the world's most popular Java reporting library
Published: August 2009
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing and developing software professionally since 1995, he has been using Java as his primary programming language since 1996. He has worked on many large scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae and the US Department of Defense. He has a Masters degree in Software Engineering from Southern Methodist University. David is editor in chief of Ensode.net (http://www.ensode.net), a web site about Java, Linux and other technology topics.

Books From Packt

  WebSphere Application Server 7.0 Administration Guide
WebSphere Application Server 7.0 Administration Guide

WordPress MU 2.7: Beginner's Guide
WordPress MU 2.7: Beginner's Guide

Papervision3D Essentials
Papervision3D Essentials

Pentaho Reporting 3.5 for Java Developers
Pentaho Reporting 3.5 for Java Developers

Drupal 6 Site Blueprints
Drupal 6 Site Blueprints

Drools JBoss Rules 5.0 Developer's Guide
Drools JBoss Rules 5.0 Developer's Guide

jQuery UI 1.6: The User Interface Library for jQuery
jQuery UI 1.6: The User Interface Library for jQuery

LWUIT 1.1 for Java ME Developers
LWUIT 1.1 for Java ME Developers

 

Your rating: None Average: 4 (2 votes)
how to reduce the width b/w bars Using Bar chart (not 3D) by
hi , i am using jasper jar using which i am generating bar charts from our DB . report is generating properly but i am seeing large width b/w 2 bars. how to reduce this width . could anyone please help here? please reply to my mail id. Thanks, Praveen Kumar. mailid: praveenkumar.duggirala@gmail.com
Thanks a good start by
Thanks for the article. Been looking for a good intro and this article gave me that.
Error in iReport by
Errors compiling C:ShwetaProjectRajElectSticker Printingdist eport2.jasper! Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector@1d97b0d net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, calculator_report2_1285929781864_964278: 151: unexpected token: Ashay @ line 151, column 40. 1 error

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
z
W
n
u
q
8
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software