Angular Pivot Grid Summaries

    The Angular UI grid in Ignite UI for Angular has a summaries feature that functions on a per-column level as group footer. Angular grid summaries is powerful feature which enables the user to see column information in a separate container with a predefined set of default summary items, depending on the type of data within the column or by implementing a custom angular template in the Pivot Grid.

    Angular Pivot Grid Summaries Overview Example

    Note

    The summary of the column is a function of all column values, unless filtering is applied, then the summary of the column will be function of the filtered result values

    Pivot Grid summaries can also be enabled on a per-column level in Ignite UI for Angular, which means that you can activate it only for columns that you need. Pivot Grid summaries gives you a predefined set of default summaries, depending on the type of data in the column, so that you can save some time:

    For string and boolean data types, the following function is available:

    • count

    For number, currency and percent data types, the following functions are available:

    • count
    • min
    • max
    • average
    • sum

    For date data type, the following functions are available:

    • count
    • earliest
    • latest

    All available column data types could be found in the official Column types topic.

    Pivot Grid summaries are enabled per-column by setting hasSummary property to true. It is also important to keep in mind that the summaries for each column are resolved according to the column data type. In the igx-pivot-grid the default column data type is string, so if you want number or date specific summaries you should specify the dataType property as number or date. Note that the summary values will be displayed localized, according to the grid locale and column pipeArgs.

    The other way to enable/disable summaries for a specific column or a list of columns is to use the public method enableSummaries/disableSummaries of the igx-pivot-grid.

    Custom Pivot Grid Summaries

    If these functions do not fulfill your requirements you can provide a custom summary for the specific columns. In order to achieve this you have to override one of the base classes IgxSummaryOperand, IgxNumberSummaryOperand or IgxDateSummaryOperand according to the column data type and your needs. This way you can redefine the existing function or you can add new functions. IgxSummaryOperand class provides the default implementation only for the count method. IgxNumberSummaryOperand extends IgxSummaryOperand and provides implementation for the min, max, sum and average. IgxDateSummaryOperand extends IgxSummaryOperand and additionally gives you earliest and latest.

    import { IgxSummaryResult, IgxSummaryOperand, IgxNumberSummaryOperand, IgxDateSummaryOperand } from 'igniteui-angular';
    // import { IgxSummaryResult, IgxSummaryOperand, IgxNumberSummaryOperand, IgxDateSummaryOperand } from '@infragistics/igniteui-angular'; for licensed package
    
    class MySummary extends IgxNumberSummaryOperand {
        constructor() {
            super();
        }
    
        operate(data?: any[]): IgxSummaryResult[] {
            const result = super.operate(data);
            result.push({
                key: 'test',
                label: 'Test',
                summaryResult: data.filter(rec => rec > 10 && rec < 30).length
            });
            return result;
        }
    }
    

    As seen in the examples, the base classes expose the operate method, so you can choose to get all default summaries and modify the result, or calculate entirely new summary results. The method returns a list of IgxSummaryResult.

    interface IgxSummaryResult {
        key: string;
        label: string;
        summaryResult: any;
    }
    

    and take optional parameters for calculating the summaries. See Custom summaries, which access all data section below.

    Note

    In order to calculate the summary row height properly, the Pivot Grid needs the operate method to always return an array of IgxSummaryResult with the proper length even when the data is empty.

    Custom summaries, which access all data

    Now you can access all Pivot Grid data inside the custom column summary. Two additional optional parameters are introduced in the IgxSummaryOperand operate method. As you can see in the code snippet below the operate method has the following three parameters:

    • columnData - gives you an array that contains the values only for the current column
    • allGridData - gives you the whole grid data source
    • fieldName - current column field
    class MySummary extends IgxNumberSummaryOperand {
        constructor() {
            super();
        }
        operate(columnData: any[], allGridData = [], fieldName?): IgxSummaryResult[] {
            const result = super.operate(allData.map(r => r[fieldName]));
            result.push({ key: 'test', label: 'Total Discontinued', summaryResult: allData.filter((rec) => rec.Discontinued).length });
            return result;
        }
    }
    

    Summary Template

    igxSummary targets the column summary providing as a context the column summary results.

    <igx-column ... [hasSummary]="true">
        <ng-template igxSummary let-summaryResults>
            <span> My custom summary template</span>
            <span>{{ summaryResults[0].label }} - {{ summaryResults[0].summaryResult }}</span>
        </ng-template>
    </igx-column>
    

    When a default summary is defined, the height of the summary area is calculated by design depending on the column with the largest number of summaries and the size of the grid. Use the summaryRowHeight input property to override the default value. As an argument it expects a number value, and setting a falsy value will trigger the default sizing behavior of the grid footer.

    Note

    Column summary template could be defined through API by setting the column summaryTemplate property to the required TemplateRef.

    Formatting summaries

    By default, summary results, produced by the built-in summary operands, are localized and formatted according to the grid locale and column pipeArgs. When using custom operands, the locale and pipeArgs are not applied. If you want to change the default appearance of the summary results, you may format them using the summaryFormatter property.

    public dateSummaryFormat(summary: IgxSummaryResult, summaryOperand: IgxSummaryOperand): string {
        const result = summary.summaryResult;
        if(summaryOperand instanceof IgxDateSummaryOperand && summary.key !== 'count'
            && result !== null && result !== undefined) {
            const pipe = new DatePipe('en-US');
            return pipe.transform(result,'MMM YYYY');
        }
        return result;
    }
    
    <igx-column ... [summaryFormatter]="dateSummaryFormat"></igx-column>
    

    Exporting Summaries

    There is an exportSummaries option in IgxExcelExporterOptions that specifies whether the exported data should include the grid's summaries. Default exportSummaries value is false.

    The IgxExcelExporterService will export the default summaries for all column types as their equivalent excel functions so they will continue working properly when the sheet is modified. Try it for yourself in the example below:

    The exported file includes a hidden column that holds the level of each DataRecord in the sheet. This level is used in the summaries to filter out the cells that need to be included in the summary function.

    In the table below, you can find the corresponding Excel formula for each of the default summaries.

    Data Type Function Excel Function
    string, boolean count ="Count: "&COUNTIF(start:end, recordLevel)
    number, currency, percent count ="Count: "&COUNTIF(start:end, recordLevel)
    min ="Min: "&MIN(IF(start:end=recordLevel, rangeStart:rangeEnd))
    max ="Max: "&MAX(IF(start:end=recordLevel, rangeStart:rangeEnd))
    average ="Avg: "&AVERAGEIF(start:end, recordLevel, rangeStart:rangeEnd)
    sum ="Sum: "&SUMIF(start:end, recordLevel, rangeStart:rangeEnd)
    date count ="Count: "&COUNTIF(start:end, recordLevel)
    earliest ="Earliest: "& TEXT(MIN(IF(start:end=recordLevel, rangeStart:rangeEnd)), format)
    latest ="Latest: "&TEXT(MAX(IF(start:end=recordLevel, rangeStart:rangeEnd)), format)

    Known Limitations

    Limitation Description
    Exporting custom summaries Custom summaries will be exported as strings instead of Excel functions.
    Exporting templated summaries Templated summaries are not supported and will be exported as the default ones.

    Keyboard Navigation

    The summary rows can be navigated with the following keyboard interactions:

    • UP - navigates one cell up
    • DOWN - navigates one cell down
    • LEFT - navigates one cell left
    • RIGHT - navigates one cell right
    • CTRL + LEFT or HOME - navigates to the leftmost cell
    • CTRL + RIGHT or END - navigates to the rightmost cell

    Styling

    To get started with styling the sorting behavior, we need to import the index file, where all the theme functions and component mixins live:

    @use "igniteui-angular/theming" as *;
    
    // IMPORTANT: Prior to Ignite UI for Angular version 13 use:
    // @import '~igniteui-angular/lib/core/styles/themes/index';
    

    Following the simplest approach, we create a new theme that extends the grid-summary-theme and accepts the $background-color, $focus-background-color, $label-color, $result-color, $pinned-border-width, $pinned-border-style and $pinned-border-color parameters.

    $custom-theme: grid-summary-theme(
        $background-color: #e0f3ff,
        $focus-background-color: rgba( #94d1f7, .3 ),
        $label-color: #e41c77,
        $result-color: black,
        $pinned-border-width: 2px,
        $pinned-border-style: dotted,
        $pinned-border-color: #e41c77
    );
    

    The last step is to include the component mixins:

    @include grid-summary($custom-theme);
    
    Note

    If the component is using an Emulated ViewEncapsulation, it is necessary to penetrate this encapsulation using ::ng-deep:

    :host {
       ::ng-deep {
           @include grid-summary($custom-theme);
       }
    }
    

    Defining a color palette

    Instead of hardcoding the color values like we just did, we can achieve greater flexibility in terms of colors by using the igx-palette and igx-color functions.

    igx-palette generates a color palette based on the primary and secondary colors that are passed:

    $blue-color: #7793b1;
    $green-color: #00ff2d;
    
    $my-custom-palette: palette($primary: $blue-color, $secondary: $green-color);
    

    And then with igx-color we can easily retrieve color from the palette.

    $custom-theme: grid-summary-theme(
        $background-color: color($my-custom-palette, "primary", 700),
        $focus-background-color: color($my-custom-palette, "primary", 800),
        $label-color: color($my-custom-palette, "secondary", 500),
        $result-color: color($my-custom-palette, "grays", 900),
        $pinned-border-width: 2px,
        $pinned-border-style: dotted,
        $pinned-border-color: color($my-custom-palette, "secondary", 500)
    );
    
    Note

    The igx-color and igx-palette are powerful functions for generating and retrieving colors. Please refer to Palettes topic for detailed guidance on how to use them.

    Using Schemas

    Going further with the theming engine, you can build a robust and flexible structure that benefits from schemas. A schema is a recipe of a theme.

    Extend one of the two predefined schemas, that are provided for every component, in this case - _light-grid-summary:

    // Extending the light grid summary schema
    $my-summary-schema: extend($_light-grid-summary,
        (
            background-color: (igx-color: ('primary', 700)),
            focus-background-color: (igx-color: ('primary', 800)),
            label-color: (igx-color: ('secondary', 500)),
            result-color: (igx-color: ('grays', 900)),
            pinned-border-width: 2px,
            pinned-border-style: dotted,
            pinned-border-color: (igx-color: ('secondary', 500))
        )
    );
    

    In order to apply our custom schema we have to extend one of the globals (light or dark), which is basically pointing out the components with a custom schema, and after that add it to the respective component themes:

    // Extending the global light-schema
    $my-custom-schema: extend($light-schema,
        (
            igx-grid-summary: $my-summary-schema
        )
    );
    
    // Defining our custom theme with the custom schema
    $custom-theme: grid-summary-theme(
        $palette: $my-custom-palette,
        $schema: $my-custom-schema
    );
    

    Don't forget to include the themes in the same way as it was demonstrated above.

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.