Jasper Reports Crosstabs - JasperReports

What are Crosstab reports in Jasper Reports?

The reports that contain the tables which enables to arrange the data in a tabular form across rows and columns is termed as Crosstab or cross-tabulation. The crosstab report can be inserted within the main report using the crosstab object. The crosstabs supports to any level of data like nominal, ordinal, interval, or ratio and displays the summarized data which is present in the variables of the report. The aggregate data like sum, average etc is displayed by the Variables.

What are the Properties of Crosstabs?

The properties or attributes of the crosstab elements are as follows:

  • isRepeatColumnHeaders – This attribute highlights that the column header should be reprinted after a page break and the default value is set to be true.
  • isRepeatRowHeaders – This attribute indicates whether the headers of the rows to be reprinted after the crosstab column break and the default value is set to be true.
  • columnBreakOffset – This attribute indicates the amount of vertical space, measured in pixels, before the subsequent crosstab piece is placed below the previous one on the same page and the default value is set to be 10.
  • runDirection − Indicates whether the crosstab data should be filled from left to right (LTR) or from right to left (RTL). The default value is LTR.
  • ignoreWidth – This attribute indicates whether the crosstab can stretch beyond the width limit and the column breaks are not generated. Otherwise the crosstab stops rendering of the columns width limit and continue to be with the remaining columns only when all the rows have started offering and the default value is set to be false.

What are the Sub Elements of a Crosstab?

The following are the sub elements of a cross tab:

  • <reportElement> − The position, width and height of the crosstab are defined by this element. This element is associated with the reportElement attribute.
  • <crosstabParameter> − This element is used to access report variables and parameters from within the crosstab. Attributes for this element includename and class
  • <parametersMapExpression> − A report variable or a parameter is passed by this element by an instance of java.util.Map, which represent a set of parameters for the crosstab and has no attributes.
  • <crosstabDataset> − This element defines the dataset to use to populate the crosstab. Attributes for this element include isDataPreSorted − This indicates whether the data in the dataset is pre-sorted. Default value is false.
  • <crosstabHeaderCell> − The content of the region is defined by this element at the upper-left corner of the crosstab where both the column and the row headers meet. On the basis of the width and the height defined, the size of the cell is automatically calculated.
  • <rowGroup> − A group that is used to split the data into rows is defined by this element. The attributes of this element includename, width, headerposition and totalposition

This element contains the following sub elements −

  • <bucket>
  • <crosstabRowHeader>
  • <crosstabTotalRowHeader>
  • <columnGroup> − A group used for splitting the data into columns is defined by this element. The attributes of this element arename, height, headerposition and total position

This element contains the following sub elements −

  • <bucket>
  • <crosstabColumnHeader>
  • <crosstabTotalColumnHeader>
  • <measure> − The calculations that are performed across the rows and columns by using this element. The attribute of this element includename, class and calculation
  • <crosstabCell> − The data present in the non-header cells is laid out by this element. The attributes of this element include:

columnTotalGroup − indicates the group to use to calculate the column total.

height − defines the height of the cell.

rowTotalGroup − indicates the group to use to calculate the row total.

width − defines the width of the cell.

  • <whenNoDataCell> − The content to be displayed on the empty crosstab cell is defined by this element and has no attributes.

How Data Grouping is done in Jasper Reports Crosstab?

The data is grouped and aggregated in the crosstab by iterating with the associated dataset records. The data is aggregated by grouping them. The rows and columns of the crosstab are based on the group items which are called as buckets. A bucket contains the following attributes:

  • bucketExpression – This expression is used for evaluation for extracting the data group items.
  • comparatorExpression – The expression is used when the values of the ordering is not satisfactory.
  • orderByExpression − Indicates the value used to sort data.

The crosstab row and column group depend on buckets.

How the Total Variables are built-in in Crosstabs?

The variables enable to list out the values of the measures and the aggregate of different levels which correspond to the cell and which can be accessed by the variables that are specifically named:

  • The current value of a measure calculation is stored in a variable having the same name as the measure.
  • <Measure>_<Column Group>_ALL – The total of all the column entries from the same row group is yielded by this element.
  • <Measure>_<Row Group>_ALL − The total of all the row entries from the same column group is yielded by this element.
  • <Measure>_<Row Group>_<Column Group>_ALL − The combined total corresponding to all the entries in both row and column groups is yielded by this element.

Example

The crosstab is illustrated by the following example by writing a new report template, jasper_report_template.jrxml. The crosstab is added to the summary section and is saved to the directory C:\tools\jasperreports-5.0.1\test and the contents of the file are as follows:

The above code provides the following details:

  • The <crosstab> element defines the crosstab.
  • A group is defined by the <rowgroup> element to split the data into rows. The data for each row is displayed by a different name.
  • The <bucket> and <bucketExpression> elements are used to defined the report expression used to group a delimeter. The name field is used as delimeter to split the rows by name.
  • The expression that is used as a row header is defined by the <crosstabRowHeader> element, which has a sub-element called <cellContents>, and which acts as a inner band for the crosstab. A name is assigned to <rowGroup> by the name attribute and in implicit variable is created. The contents of the header cell for the rows are defined by the <crosstabRowHeader>.
  • The column group element and the row group element are the same except that the functionalities are performed for columns instead of rows.
  • The calculation performed across the rows and columns is defined by the <measure> element and the attribute for calculation is set to Count.
  • The <crosstabCell> element defines how data in non-header cells will be laid out.

There is no change in the Java codes for report filling. The contents of the file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\JasperReportFill.java are as follows:

The contents of the POJO file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\DataBean.java are as follows −

The contents of the file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\DataBeanList.java are as follows −

How the report is generated using Jasper Reports Crosstabs?

The above files are compiled and executed by using the ANT build process and the content of the file biuild.xml are saved under C:\tools\jasperreports-5.0.1\test

The import file - baseBuild.xml is selected and placed in the same directory as build.xml.

The command line window is opened and visit the directory where the build.xml is placed and execute the command ant -Dmain-class=com.wisdomjobs.JasperReportFill

On compilation of the above code, the JasperViewer window pops up as shown below:

Report Crosstab Example

It is observed that the country and the name are tabulated.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JasperReports Topics