Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Open Source Java Reporting with JasperReports and iReport : Page 3

JasperReports is a powerful and flexible open source reporting solution. The iReport visual designer enables you to take advantage of the full power of JasperReports without in-depth knowledge of the JasperReports native XML format.


advertisement

Adding a Report Title and Date

So now you can generate a report with real data. Next, add a title in the title bar. This title will be displayed only at the very start of the report. Suppose you want the title for this report to be "Employees/service", followed by the current date. To integrate the "Employees/Service" text, just place a static text zone in the title section (use "Edit/Insert Element/Static Text" or the "T" icon).

Figure 8. Add a Date Field

Now, suppose you want to display today's date as well. Insert a new text field ("Edit/Insert Element/Text Field"). Double-click on the text field object and go to the "TextField" tab in the window that just appeared (see Figure 8).



Here you get a glimpse of the power of JasperReports. Because a compiled JasperReport is a Java class, you can use any Java expression to help build your report, as well as JasperReports fields, variables, and parameters. For instance, the "TextField" expression is a Java expression and will be interpreted as such.

Figure 9. The Report with a Title Bar

To display the current date, you just create a new Date() object, which will automatically be instantiated to the current date and time. Then you tell JasperReports the expression type (in the Textfield Expression class: java.util.Date) to use, when to evaluate the expression (Evaluation time), and which format to use (the "Pattern field").

As a final touch, add a transparent border around the title ("Edit/Insert Element/Rounded Rectangle") and an image ("Edit/Insert Element/Image"), and then customize the colors and fonts (see Figure 9 for an example).

Calculating Totals

Now suppose you want to display the total cost of all employee salaries. To calculate values like this in JasperReports, you need to use report variables. You use report variables to store and calculate any temporary values you need in the report, such as totals, sub-totals, averages, and so on. Here are some important things you should know about report variables:
  • The variable class type must match the field type for calculations to work properly. So here, set it to Double.
  • The calculation type tells JasperReports how to calculate the variable. Here, set it to 'Sum' to find the total of all the emp_salary field values.
  • The variable expression represents the value to be evaluated in the calculation. This expression can take many forms:
    • A Java expression (e.g., New Date())
    • $F for fields (e.g., "$F{emp_salary}")
    • $V for variables (e.g., "$V{service_employee_count}")
    • $P for report parameters
    • $R for localized resources

Figure 10. Adding a New Variable

To display the report variables, open the "View/Report Variables" menu. You need to add a new variable to track employee salaries and calculate the total value. Let's call it total_salaries (see Figure 10). Set the variable class type to Double and the calculation type to Sum.

Figure 11. Calculating the Total Salaries

For now, you need to evaluate the 'emp_salary' field, so enter "$F{emp_salary}" as the variable expression.

Now drag this variable into the Column Footer section, and add an appropriate text title (see the generated report in Figure 11). Using the same approach, you can easily add other types of variables: averages, counts, lowest and highest values, and so on.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap