React PivotGrid - Summaries
Summaries are values (often numeric) displayed in the pivot grid cells. Each summary value is located at the intersection of a row header group and column header group. Headers describe the meaning of each summary value. Summaries are configured in the fields of the "data" area (data fields or measures in OLAP terminology). If the "data" area contains more than one field, the column header is supplemented with another row that helps to identify the data field to which the summary value belongs. The following image explains how to interpret summary values.
Client-Side Data Aggregation
Follow these steps to configure custom client-side data aggregation for a field:
Make sure that the remoteOperations property is not set or set to false.
Set the field's summaryType property to "custom".
Implement the calculateCustomSummary function as detailed in its description.
Server-Side Data Aggregation
Follow the instructions below to implement custom server-side data aggregation for a field. You will need the DevExtreme.AspNet.Data library.
Set the remoteOperations property to true.
Implement and register a custom server-side data aggregator using DevExtreme.AspNet.Data as described in this article.
Pass the string identifier that you used in registration to the field's summaryType property:
ASP.NET MVC Controls
Razor C#@(Html.DevExtreme().PivotGrid() .DataSource(ds => ds // ... .Fields(fields => { fields.Add() // ... .SummaryType("totalSales"); }) .RemoteOperations(true) ) )
jQuery
JavaScript$(function() { var pivotGridDataSource = new DevExpress.data.PivotGridDataSource({ // ... fields: [{ // ... summaryType: "totalSales" }], remoteOperations: true }); $("#pivotGridContainer").dxPivotGrid({ dataSource: pivotGridDataSource }); });
Angular
TypeScriptHTMLimport PivotGridDataSource from "devextreme/ui/pivot_grid/data_source"; import { DxPivotGridModule } from "devextreme-angular"; // ... export class AppComponent { pivotGridDataSource: PivotGridDataSource; constructor() { this.pivotGridDataSource = new PivotGridDataSource({ // ... fields: [{ // ... summaryType: "totalSales" }], remoteOperations: true }); } } @NgModule({ imports: [ // ... DxPivotGridModule ], // ... })
<dx-pivot-grid [dataSource]="pivotGridDataSource"> </dx-pivot-grid>
Predefined Summaries
To configure a data field for using one of the predefined aggregation methods, set the summaryType property to one of the following values.
- "sum"
Sums up all facts within an area. - "min"
Calculates the minimum fact's value. - "max"
Calculates the maximum fact's value. - "avg"
Calculates the average of all facts within an area. - "count"
Calculates the number of facts within an area.
You can configure the type of a value held in a field using the dataType property. This is useful when field data has an unusual format, for example, a date saved as a string. When the dataType is not set, it is defined automatically.
Custom Summaries
If the list of built-in summary types does not contain a property satisfying your requirements, you can implement your own aggregate function. Assign it to the calculateCustomSummary property and set the summaryType property to "custom". In this case, the function assigned to the calculateCustomSummary field will be called for each fact and the overall result will be used as a summary value. The example below shows how to implement the variance aggregation function.
var pivotGridSettings = { //... dataSource: { //... fields: [{ //... area: 'data', caption: 'Var', summaryType: 'custom', calculateCustomSummary: function (options) { if (options.summaryProcess == 'start') { options.totalValue = 0; //Sum options.sum2 = 0; //Sum of squared values options.n = 0; } if (options.summaryProcess == 'calculate') { options.totalValue += options.value; options.sum2 += Math.pow(options.value, 2); options.n++; } if (options.summaryProcess == 'finalize') { options.totalValue = options.sum2 - Math.pow(options.totalValue, 2) / options.n; options.totalValue /= options.n - 1; } } }, //... ] } };
Summary Post-Processing
If you need to perform additional calculations on summary values so that you can take into account the values in the neighboring cells and analyze the trends, you can apply a post-processing function called summary display mode. You can use one of the predefined summary display modes or implement your own one using the calculateSummaryValue property.
When implementing a custom post-processing function, you can use the cell object called Summary Cell, which allows you to easily collect data from the neighboring cells.
At the final step of summary values processing, you can enable the running total feature if such processing is applicable for your data.
If you have technical questions, please create a support ticket in the DevExpress Support Center.