| JasperReports Ultimate Guide - Samples - Schema - Configuration - Functions - FAQ - API (Javadoc)
|
|
|
|
|
| JasperReports - Crosstabs Sample (version master-SNAPSHOT) | |
|
|
|
|
|
|
| Main Features in This Sample | |
|
| Crosstabs |
|
| Datasets |
|
| Dynamic Styles |
|
|
|
|||||
| top | ||||||
|
|
||||||
![]() | Crosstabs | Documented by | ||||
|
|
||||||
|
| Description / Goal |
| How to use the built-in crosstab element to display aggregated data. | |||
|
| Since |
| 1.1.0 | |||
|
|
||||||
|
|
Crosstabs Overview
Crosstabs are very helpful tools used to aggregate data into 2-dimensional grids. Like other data aggregating elements (charts, for instance), they are organized as regular report elements, but with a complex structure and granularity. Crosstabs can be used with any level of data (nominal, ordinal, interval, or ratio), and usually display the summarized data, contained in report variables, in the form of a dynamic table. Variables are used to display aggregate data such as sums, counts, average values. Crosstabs are also interesting due to their flexible layout capabilities. When a crosstab does not fit on a page, then either a column or row break occurs, and the crosstab splits into several pieces. Depending on the remaining available space, the crosstab could continue on the same page, or it could overflow onto a new page. A crosstab element is characterized by the following attributes:
name and class attributes and
by the <parameterValueExpression> tag.
One can declare crosstab parameters either one by one in the crosstab element, or grouping them in a parameters map referenced by the <parametersMapExpression> tag.
Crosstab Datasets Like charts, crosstabs can handle by themselves collections of data. They can access data either in the report main dataset, or they can handle their own subdataset. All subdataset information required by the crosstab element is contained in the <crosstabDataset> child element. To faster
perform calculations on data it is recommended that data come already sorted within dataset, according
to the row and column groups. If this is not the case, then the crosstab internal engine should be
instructed to sort data during the aggregation process. One can do this using the isDataPreSorted
dataset attribute. If no <crosstabDataset> is declared, then the crosstab is using the
main dataset. More on datasets can be found further in the datasets section.
Data Grouping in Crosstab The crosstab calculation engine aggregates data by iterating through the associated dataset records. In order to aggregate data, one need to group them first. In a crosstab rows and columns are based on specific group items, called buckets. A bucket definition should contain:
Any number of row groups can be declared within a crosstab. The first row group will enclose the second one, the second one will enclose the third one, and so on. A row group is characterized by the following attributes:
Any number of column groups can be declared within a crosstab. Like row groups, outer column groups are enclosing inner column groups, in the same order as they were declared. A column group is characterized by the following attributes:
A measure is a result of aggregate data calculation. Its value is typically displayed in the crosstab cells. A crosstab measure behaves just like a report variable and is completely characterized by:
ROW_COUNT and COLUMN_COUNT one can generate alternate colors for rows or columns
in a crosstab, using conditional styles:
<style name="RowStyle" isDefault="false" mode="Opaque">
<conditionalStyle>
<conditionExpression><![CDATA[$V{ROW_COUNT} % 2 == 0]] ></conditionExpression>
<style backcolor="#E0E0E0"/>
</conditionalStyle>
</style>
or:
<style name="ColumnStyle" isDefault="false" mode="Opaque">
<conditionalStyle>
<conditionExpression><![CDATA[$V{COLUMN_COUNT} % 2 == 0]] ></conditionExpression>
<style backcolor="#E0E0E0"/>
</conditionalStyle>
</style>
Crosstab Cells
A crosstab cell is a rectangular report element that can contain any kind of other report element, except subreports, charts, and crosstabs. Usually, crosstab cells can be either detail cells (when its both row/column correspond to a bucket value), or total cells (when at least the row or column corresponds to a group total value). Depending on their position within the crosstab, or on their special meaning, crosstab cell types are described below:
This sample contains 4 JRXML files illustrating various crosstab properties and usability.
Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/hsqldb within the JasperReports source project and run the > ant runServer command.
It will start the HSQLDB server shipped with the JasperReports distribution package. Let this terminal running the HSQLDB server.
Open a new command prompt/terminal window and set the current folder to demo/samples/crosstabs within the JasperReports source project and run the > ant test view command.
It will generate all supported document types for the four sample reports and save them in the demo/samples/crosstabs/build/reports directory.
Then the ShipmentsReport will open in the JasperReports internal viewer. |
|||||
|
|
||||||
| top | ||||||
|
|
||||||
![]() | Datasets | Documented by Sanda Zaharia | ||||
|
|
||||||
|
| Description / Goal |
| How to make use of subdatasets for chart elements and crosstab elements. | |||
|
| Since |
| 1.1.0 | |||
|
| Other Samples |
|
|
|||
|
|
||||||
|
|
Report Datasets
A dataset is an entity that intermediates data source and subreport characteristics. Datasets allow the engine to iterate through some virtual records, just as data sources do, but they also enable calculations and data grouping during this iteration using variables and groups. A dataset declaration, containing parameters, fields, variables, and groups, is similar to subreport declarations, but datasets are not related to any visual content. There are no sections or layout information at the dataset level. The report data source, along with the parameters, fields, variables, and groups declared at the report level, represent a special dataset declaration, implicitely used in every report template, the so-called main dataset. One can consider the main dataset responsible for iterating through the report data source records, calculating variables, filtering out records, and estimating group breaks during the report-filling process. Subdatasets and Dataset Runs User-defined datasets are declared as <subDataset/> elements. To completely
characterize a subdataset
one needs to set:
Anytime a dataset is referenced by another report element, a dataset run is instantiated. A dataset run supplies additional information about either the appropriate data source to be iterated, or the database connection string. It comes also with its own parameters and/or parameters map. Dataset runs are similar to subreports in the way parameters and the data source/connection are passed in:
<element name="datasetRun">
<annotation>
<documentation>Subdataset instantiation information for a chart/crosstab dataset.</documentation>
</annotation>
<complexType>
<sequence>
<element ref="jr:parametersMapExpression" minOccurs="0" maxOccurs="1"/>
<element ref="jr:datasetParameter" minOccurs="0" maxOccurs="unbounded"/>
<choice minOccurs="0" maxOccurs="1">
<element ref="jr:connectionExpression"/>
<element ref="jr:dataSourceExpression"/>
</choice>
</sequence>
<attribute name="subDataset" type="string" use="required">
<annotation>
<documentation>The name of the <elem>subdataset</elem> to instantiate.</documentation>
</annotation>
</attribute>
</complexType>
</element>
The subDataset attribute is mandatory and contains the name of the subdataset to be used during the
chart or crosstab filling process. If no dataset run is specified for a chart or crosstab, the main dataset of
the report is used by default.
|
|||||
|
|
||||||
| top | ||||||
|
|
||||||
![]() | Dynamic Styles | Documented by Sanda Zaharia | ||||
|
|
||||||
|
| Description / Goal |
| How to make use of dynamic style features in to customize the appearance of report elements. | |||
|
| Since |
| 4.8.0 | |||
|
|
||||||
|
|
Overview
Since report styles are declared once per report, dynamic expressions cannot be used to set their attributes. However, in some situations the element's appearance may depend on certain conditions determined only at runtime, so it needs to be set dynamically. A common example is the necessity to use different foreground/background colors in table cells, depending on the cell content. Conditional styles based on a dynamic condition expression are very helpful in such situations, but style attributes in a conditional style still don't support expressions. To solve this problem were introduced dynamic style features in JasperReports, starting with v.4.8.0. These features come in form of properties or property expressions having their names prefixed with net.sf.jasperreports.style,
that are set per report element. Each feature is associated with a style attribute. If such a property is found at element level,
its value will override the related style attribute of the element.
The following style properties are defined in PropertyStyleProvider class. They are also presented in this page, in a sequence starting with the net.sf.jasperreports.style.backcolor property. Style properties are grouped in 4 categories: common, box, pen and paragraph style:
net.sf.jasperreports.style.name and
net.sf.jasperreports.style.style are not supported. They have no meaning and no effects in this implementation.
This is because styles cannot be defined at element level, and dynamic style features are intended only for overriding an existing style attribute for that element. For dynamic style definitions one can use conditional styles in the report. Following is an example of dynamic style features declared for a given element in JRXML:
<reportElement mode="Opaque" x="0" y="0" width="60" height="10" uuid="7a6d4e33-8395-467d-8f6e-bef91a959af5">
<property name="net.sf.jasperreports.style.isBold" value="true"/>
<propertyExpression name="net.sf.jasperreports.style.forecolor"><![CDATA[$F{Forecolor}]] ></propertyExpression>
<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$P{Backcolor}]] ></propertyExpression>
<propertyExpression name="net.sf.jasperreports.style.fontName"><![CDATA[$V{FontName}]] ></propertyExpression>
</reportElement>
Dynamic Styles Example
The OrdersReport.jrxml file in the crosstabs sample uses a dynamic style feature in order to allow different background color tones in crosstab cells, based on the value of the cell content relative to the grand total. For elements in detail cells the backcolor style feature was set using the following property expression:
<reportElement style="OrderCountStyle" mode="Opaque" x="0" y="0" width="60" height="10" uuid="7a6d4e33-8395-467d-8f6e-bef91a959af5">
<propertyExpression name="net.sf.jasperreports.style.backcolor">
<![CDATA["#" + net.sf.jasperreports.engine.util.JRColorUtil.getColorHexa(new java.awt.Color(
255,
255 - Math.min(255,(int)(255 * 20 * $V{OrderCount} / $V{OrderCount_CountryRow_FreightColumn_ALL})),
255 - Math.min(255,(int)(255 * 20 * $V{OrderCount} / $V{OrderCount_CountryRow_FreightColumn_ALL}))))]] >
</propertyExpression>
</reportElement>
After running the sample we'll see the red background of crosstab cells colored more or less intensely depending on the value of the
OrderCount measure as percent of OrderCount_CountryRow_FreightColumn_ALL quantity. Cells with OrderCount = 0 display a white background.
|
|||||
|
|
||||||
|
|
| © 2001- Cloud Software Group, Inc. www.jaspersoft.com |