Print in Angular Pivotview component
23 Aug 20258 minutes to read
The Angular PivotView component supports print functionality, allowing users to print the current state of the pivot table or pivot chart. This feature enables users to generate hard copies of pivot table reports for convenient review and data sharing.
Print pivot table
The rendered pivot table can be printed by invoking the print method from the underlying Grid component instance. The Grid control manages the print functionality and captures the current state of the pivot table, including all applied filters, sorting, and formatting. The sample code below demonstrates how to trigger the print operation using an external button click.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { PivotViewAllModule, PivotFieldListAllModule } from '@syncfusion/ej2-angular-pivotview'
import { Component, OnInit, ViewChild } from '@angular/core';
import { PivotView, FieldListService, IDataSet } from '@syncfusion/ej2-angular-pivotview';
import { Pivot_Data } from './datasource';
import { Button } from '@syncfusion/ej2-buttons';
import { DataSourceSettingsModel } from '@syncfusion/ej2-pivotview/src/model/datasourcesettings-model';
@Component({
imports: [
PivotViewAllModule,
PivotFieldListAllModule
],
standalone: true,
selector: 'app-container',
providers: [FieldListService],
template: `<div class="col-md-2"><button ej-button id='print'>Print</button></div>
<div class="col-md-8"><ejs-pivotview #pivotview id='PivotView' height=height [dataSourceSettings]=dataSourceSettings [width]=width></ejs-pivotview></div>`
})
export class AppComponent implements OnInit {
public width?: string;
public height?: number;
public dataSourceSettings?: DataSourceSettingsModel;
public button?: Button;
@ViewChild('pivotview', { static: false })
public pivotGridObj?: PivotView;
ngOnInit(): void {
this.width = "100%";
this.height = 350;
this.dataSourceSettings = {
expandAll: false,
dataSource: Pivot_Data as IDataSet[],
columns: [{ name: 'Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold' }, { name: 'Amount' }],
rows: [{ name: 'Country' }, { name: 'Products' }]
};
this.button = new Button({ isPrimary: true });
this.button.appendTo('#print');
this.button.element.onclick = (): void => {
this.pivotGridObj?.grid.print();
};
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Print pivot chart
To print the pivot chart, use the print method from the underlying Chart component instance. The Chart control manages the print functionality and preserves all visual elements, including colors, legends, and data labels, in the printed output.
To use pivot chart functionality, inject the
PivotChartServicemodule into the@NgModule.providerssection.
To display the pivot chart, set the displayOption property to either Chart or Both.
The sample code below illustrates how to print the pivot chart through an external button click.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { PivotViewAllModule, PivotFieldListAllModule } from '@syncfusion/ej2-angular-pivotview'
import { Component, OnInit, ViewChild } from '@angular/core';
import { PivotView, FieldListService, IDataSet, DisplayOption,PivotChartService } from '@syncfusion/ej2-angular-pivotview';
import { ChartSettings } from '@syncfusion/ej2-pivotview/src/pivotview/model/chartsettings';
import { Pivot_Data } from './datasource';
import { Button } from '@syncfusion/ej2-buttons';
import { DataSourceSettingsModel } from '@syncfusion/ej2-pivotview/src/model/datasourcesettings-model';
@Component({
imports: [
PivotViewAllModule,
PivotFieldListAllModule
],
standalone: true,
selector: 'app-container',
providers: [FieldListService, PivotChartService],
template: `<div class="col-md-2"><button ej-button id='print'>Print</button></div>
<div class="col-md-8"><ejs-pivotview #pivotview id='PivotView' height=height [dataSourceSettings]=dataSourceSettings
[displayOption]='displayOption' [chartSettings]='chartSettings' [width]=width></ejs-pivotview></div>`
})
export class AppComponent implements OnInit {
public width?: string;
public height?: number;
public dataSourceSettings?: DataSourceSettingsModel;
public button?: Button;
public displayOption?: DisplayOption;
public chartSettings?: ChartSettings;
@ViewChild('pivotview', { static: false })
public pivotGridObj?: PivotView;
ngOnInit(): void {
this.width = "100%";
this.height = 350;
this.displayOption = { view: 'Chart' } as DisplayOption;
this.chartSettings = { chartSeries: { type: 'Column' }} as ChartSettings;
this.dataSourceSettings = {
expandAll: false,
dataSource: Pivot_Data as IDataSet[],
columns: [{ name: 'Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold' }, { name: 'Amount' }],
rows: [{ name: 'Country' }, { name: 'Products' }]
};
this.button = new Button({ isPrimary: true });
this.button.appendTo('#print');
this.button.element.onclick = (): void => {
this.pivotGridObj?.chart.print();
};
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));