iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library

13.07.2015 Views

iReport Ultimate Guide6.2.4 Relative DatesiReport 5.0 introduces relative dates. This feature enables you create reports that to filter information based on a date rangerelative to the current system date. To do this, use the following template: where:• indicates the time span you want to use. Options include: DAY, WEEK, MONTH, QUARTER, SEMI, and YEAR.• indicates whether the time span occurs before or after the chosen time span.• indicates the number of the above-mentioned time spans you want to include in the filter.For example, if you want to look at Sales for the prior month, your expression would be MONTH - 1.Relative dates are sensitive to time zones. The relative date expression is calculated in the time zone of the logged-inuser.Only one property is configurable: the start day of the week does not depend on locale.The class attribute of JR Parameter of type Relative Date should have one of the following values:• net.sf.jasperreports.engine.rd.DateRange – if you want to use java.util.Date (Date only).For example: If you want to set a relative date as a default value expression of a JR parameter, use the following relative date-builderpattern:• new DateRangeBuilder("DAY-1").toDateRange()• new DateRangeBuilder(”WEEK").set(Timestamp.class).toDateRange()• new DateRangeBuilder("2012-08-01").toDateRange()• new DateRangeBuilder("2012-08-01 12:34:56").toDateRange();For queries, Relative dates are not supported by the $P{} function because it can handle only a limited number of classes(standard Java classes Integer, String etc.). Instead, a new implementation of pluggable functions and parameters inUR supports relative dates with $X{} functions.The following is a JRXML example that shows data from the previous day:Older reports which have date parameters will work just as before, and Relative Dates functionality will beunavailable. In order to make older reports support relative dates, you will need to modify the JRXML: change theparameter class, and, if needed, set a default value expression. In addition, remember to use the $X{} functioninstead of $P{}.104

Fields, Parameters, and VariablesRelative dates currently do not support keywords like “Week-To-Date”(from the start of the current week to the end of thecurrent day). However, you can emulate that by using IS_BETWEEN:• In JRXML, the query use is:$X{IS_BETWEEN, column, startParam, endParam}where startParam has value WEEK and endParam has value DAY.• Likewise, you can do the same for other time ranges: Year-To-Week, Year-To-Month, etc.6.2.5 Passing Parameters from a ProgramiReport passes parameters from a program “caller” to the print generator using a class that extends the java.util.Mapinterface. Consider the code in Code Example 3-2 on page 43, in particular the following lines:...HashMap hm = new HashMap();...JasperPrint print = JasperFillManager.fillReport(fileName,hm,new JREmptyDataSource());...fillReport is a key method that allows you to create a report instance by specifying the file name as a parameter, aparameter map, and a data source. (This example uses a dummy data source created with the class JREmptyDataSource andan empty parameter map created using a java.util.HashMap object.)Let’s see how to pass a simple parameter to a reporting order to specify the title of a report.The first step is to create a parameter in the report to host the title (that will be a String). We can name this parameterREPORT_TITLE and the class will be java.lang.String (see Figure 6-9).Figure 6-9Definition of the parameter to host the title105

<strong>iReport</strong> <strong>Ultimate</strong> <strong>Guide</strong>6.2.4 Relative Dates<strong>iReport</strong> 5.0 introduces relative dates. This feature enables you create reports that to filter information based on a date rangerelative to the current system date. To do this, use the following template: where:• indicates the time span you want to use. Options include: DAY, WEEK, MONTH, QUARTER, SEMI, and YEAR.• indicates whether the time span occurs before or after the chosen time span.• indicates the number of the above-mentioned time spans you want to include in the filter.For example, if you want to look at Sales for the prior month, your expression would be MONTH - 1.Relative dates are sensitive to time zones. The relative date expression is calculated in the time zone of the logged-inuser.Only one property is configurable: the start day of the week does not depend on locale.The class attribute of JR Parameter of type Relative Date should have one of the following values:• net.sf.jasperreports.engine.rd.DateRange – if you want to use java.util.Date (Date only).For example: If you want to set a relative date as a default value expression of a JR parameter, use the following relative date-builderpattern:• new DateRangeBuilder("DAY-1").toDateRange()• new DateRangeBuilder(”WEEK").set(Timestamp.class).toDateRange()• new DateRangeBuilder("2012-08-01").toDateRange()• new DateRangeBuilder("2012-08-01 12:34:56").toDateRange();For queries, Relative dates are not supported by the $P{} function because it can handle only a limited number of classes(standard Java classes Integer, String etc.). Instead, a new implementation of pluggable functions and parameters inUR supports relative dates with $X{} functions.The following is a JRXML example that shows data from the previous day:Older reports which have date parameters will work just as before, and Relative Dates functionality will beunavailable. In order to make older reports support relative dates, you will need to modify the JRXML: change theparameter class, and, if needed, set a default value expression. In addition, remember to use the $X{} functioninstead of $P{}.104

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!