Number formatting in Angular Pivotview component
27 Apr 202424 minutes to read
Allows you to specify the required display format that should be used in values of the pivot table. Supported display formats are:
- Number
- Currency
- Percentage
- Custom
You can apply format for the numeric values using the following properties in the formatSettings
.
-
name
: It allows to specify the field name. -
format
: It allows to specify the format of the respective field.
Possible formatting values are:
- N - denotes numeric type.
- C - denotes currency type.
- P - denotes percentage type.
If no format is specified it takes number as default format type.
Other properties include:
-
useGrouping
: It allows to enable or disable the separator, for example, $100,000,000 or $100000000 respectively. By default, it will be set as true. -
currency
: It allows to set the currency code which needs to considered for the currency formatting.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { PivotViewAllModule, PivotFieldListAllModule } from '@syncfusion/ej2-angular-pivotview'
import { Component } from '@angular/core';
import { IDataOptions, IDataSet, PivotView, FieldListService } from '@syncfusion/ej2-angular-pivotview';
import { Pivot_Data } from './datasource';
@Component({
imports: [
PivotViewAllModule,
PivotFieldListAllModule
],
standalone: true,
selector: 'app-container',
providers: [FieldListService],
// specifies the template string for the pivot table component
template: `<div style="height: 480px;"><ejs-pivotview #pivotview id='PivotView' height='350' [dataSourceSettings]=dataSourceSettings showFieldList='true' width=width></ejs-pivotview></div>`
})
export class AppComponent {
public width?: string;
public dataSourceSettings?: IDataOptions;
ngOnInit(): void {
this.width = '100%';
this.dataSourceSettings = {
dataSource: Pivot_Data as IDataSet[],
expandAll: false,
allowLabelFilter: true,
allowValueFilter: true,
enableSorting: true,
drilledMembers: [{ name: 'Country', items: ['France'] }],
columns: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold', caption: 'Units Sold' }, { name: 'Amount', caption: 'Sold Amount' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
formatSettings: [{ name: 'Amount', format: 'C2', useGrouping: false, currency: 'EUR' }],
filters: []
};
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
You can also format the values at runtime using the formatting dialog. This option can be enabled by setting the allowNumberFormatting
property to true. The same has been discussed in some of the upcoming topics.
To use the formatting dialog, inject the
NumberFormatting
module into the pivot table.
Custom format
You can add any custom format directly to the format
property in the formatSettings
. Custom format can be achieved by using one or more format specifiers listed in the below table.
Specifier | Description | Input | Format Output |
---|---|---|---|
0 | Replaces the zero with the corresponding digit if it is present. Otherwise, zero will appear in the result string. | { format: ‘0000’ } | ‘0123’ |
# | Replaces the ‘ # ‘ symbol with the corresponding digit if it is present. Otherwise, no digits will appear in the result string. | { format: ‘####’ } | ‘1234’ |
. | Denotes the number of digits permitted after the decimal point. | { format: ‘###0.##0#’ } | ‘546321.000’ |
% | Percent specifier denotes the percentage type format. | { format: ‘0000 %’ } | ‘0100 %’ |
$ | Denotes the currency type format that is based on the global currency code specified. | { format: ‘$ ###.00’ } | ’$ 13.00’ |
; | Denotes separate formats for positive, negative and zero values. | { format: ‘###.##;(###.00);-0’ } | ‘(120.00)’ |
‘String’ (single Quotes) | Denotes the characters that are enclosed in the single quote (‘) to be replaced in the resulting string. | { format: “####.00 ‘@’” } | “123.00 @” |
NOTE: If custom format is defined, certain properties such as
useGrouping
andcurrency
will not be considered.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { PivotViewAllModule, PivotFieldListAllModule } from '@syncfusion/ej2-angular-pivotview'
import { Component } from '@angular/core';
import { IDataOptions, IDataSet, PivotView, FieldListService } from '@syncfusion/ej2-angular-pivotview';
import { Pivot_Data } from './datasource';
@Component({
imports: [
PivotViewAllModule,
PivotFieldListAllModule
],
standalone: true,
selector: 'app-container',
providers: [FieldListService],
// specifies the template string for the pivot table component
template: `<div style="height: 480px;"><ejs-pivotview #pivotview id='PivotView' height='350' [dataSourceSettings]=dataSourceSettings showFieldList='true' width=width></ejs-pivotview></div>`
})
export class AppComponent {
public width?: string;
public dataSourceSettings?: IDataOptions;
ngOnInit(): void {
this.width = '100%';
this.dataSourceSettings = {
dataSource: Pivot_Data as IDataSet[],
expandAll: false,
allowLabelFilter: true,
allowValueFilter: true,
enableSorting: true,
drilledMembers: [{ name: 'Country', items: ['France'] }],
columns: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold', caption: 'Units Sold' }, { name: 'Amount', caption: 'Sold Amount' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
formatSettings: [{ name: 'Sold', format: "####.00 'Nos'" }],
filters: []
};
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Toolbar
You can enable formatting dialog option in the toolbar by adding NumberFormatting
in the toolbar
. After that, you can see the option to invoke the formatting dialog in the toolbar.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { PivotViewAllModule, PivotFieldListAllModule } from '@syncfusion/ej2-angular-pivotview'
import { Component, ViewChild } from '@angular/core';
import {
IDataOptions, PivotView, FieldListService, CalculatedFieldService, NumberFormattingService,
ToolbarService, ConditionalFormattingService, ToolbarItems, DisplayOption, IDataSet, DataSourceSettings
} from '@syncfusion/ej2-angular-pivotview';
import { Pivot_Data } from './datasource';
@Component({
imports: [
PivotViewAllModule,
PivotFieldListAllModule
],
standalone: true,
selector: 'app-container',
providers: [CalculatedFieldService, ToolbarService, ConditionalFormattingService, FieldListService, NumberFormattingService],
// specifies the template string for the pivot table component
template: `<div style="height: 480px;"><ejs-pivotview #pivotview id='PivotView' [dataSourceSettings]=dataSourceSettings allowExcelExport='true' allowNumberFormatting='true' allowConditionalFormatting='true' allowPdfExport='true' showToolbar='true' allowCalculatedField='true' showFieldList='true' width='100%' [displayOption]='displayOption' height='350' [toolbar]='toolbarOptions' (saveReport)='saveReport($event)' (loadReport)='loadReport($event)' (fetchReport)='fetchReport($event)' (renameReport)='renameReport($event)' (removeReport)='removeReport($event)' (newReport)='newReport()'></ejs-pivotview></div>`
})
export class AppComponent {
public dataSourceSettings?: IDataOptions;
public toolbarOptions?: ToolbarItems[];
public displayOption?: DisplayOption;
@ViewChild('pivotview', {static: false})
public pivotGridObj?: PivotView;
saveReport(args: any) {
let reports = [];
let isSaved: boolean = false;
if (localStorage['pivotviewReports'] && localStorage['pivotviewReports'] !== "") {
reports = JSON.parse(localStorage['pivotviewReports']);
}
if (args.report && args.reportName && args.reportName !== '') {
reports.map(function (item: any): any {
if (args.reportName === item.reportName) {
item.report = args.report; isSaved = true;
}
});
if (!isSaved) {
reports.push(args);
}
localStorage['pivotviewReports'] = JSON.stringify(reports);
}
}
fetchReport(args: any) {
let reportCollection: string[] = [];
let reeportList: string[] = [];
if (localStorage['pivotviewReports'] && localStorage['pivotviewReports'] !== "") {
reportCollection = JSON.parse(localStorage['pivotviewReports']);
}
reportCollection.map(function (item: any): void { reeportList.push(item.reportName); });
args.reportName = reeportList;
}
loadReport(args: any) {
let reportCollection: string[] = [];
if (localStorage['pivotviewReports'] && localStorage['pivotviewReports'] !== "") {
reportCollection = JSON.parse(localStorage['pivotviewReports']);
}
reportCollection.map(function (item: any): void {
if (args.reportName === item.reportName) {
args.report = item.report;
}
});
if (args.report) {
(this.pivotGridObj!.dataSourceSettings as DataSourceSettings) = JSON.parse(args.report).dataSourceSettings;
}
}
removeReport(args: any) {
let reportCollection: any[] = [];
if (localStorage['pivotviewReports'] && localStorage['pivotviewReports'] !== "") {
reportCollection = JSON.parse(localStorage['pivotviewReports']);
}
for (let i: number = 0; i < reportCollection.length; i++) {
if (reportCollection[i].reportName === args.reportName) {
reportCollection.splice(i, 1);
}
}
if (localStorage['pivotviewReports'] && localStorage['pivotviewReports'] !== "") {
localStorage['pivotviewReports'] = JSON.stringify(reportCollection);
}
}
renameReport(args: any) {
let reportCollection: string[] = [];
if (localStorage['pivotviewReports'] && localStorage['pivotviewReports'] !== "") {
reportCollection = JSON.parse(localStorage['pivotviewReports']);
}
reportCollection.map(function (item: any): any { if (args.reportName === item.reportName) { item.reportName = args.rename; } });
if (localStorage['pivotviewReports'] && localStorage['pivotviewReports'] !== "") {
localStorage['pivotviewReports'] = JSON.stringify(reportCollection);
}
}
newReport() {
this.pivotGridObj?.setProperties({ dataSourceSettings: { columns: [], rows: [], values: [], filters: [] } }, false);
}
ngOnInit(): void {
this.displayOption = { view: 'Both' } as DisplayOption;
this.toolbarOptions = ['New', 'Save', 'SaveAs', 'Rename', 'Remove', 'Load',
'Grid', 'Chart', 'Export', 'SubTotal', 'GrandTotal', 'ConditionalFormatting', 'NumberFormatting', 'FieldList'] as ToolbarItems[];
this.dataSourceSettings = {
dataSource: Pivot_Data as IDataSet[],
expandAll: false,
enableSorting: true,
drilledMembers: [{ name: 'Country', items: ['France'] }],
columns: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold', caption: 'Units Sold' }, { name: 'Amount', caption: 'Sold Amount' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
formatSettings: [{ name: 'Amount', format: 'C0' }],
filters: []
};
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Invoking formatting dialog through external button
You can invoke the formatting dialog by clicking an external button using the ShowNumberFormattingDialog
method.
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 { IDataOptions, IDataSet, CalculatedFieldService, PivotView, NumberFormattingService } from '@syncfusion/ej2-angular-pivotview';
import { Pivot_Data } from './datasource';
import { Button } from '@syncfusion/ej2-buttons';
@Component({
imports: [
PivotViewAllModule,
PivotFieldListAllModule
],
standalone: true,
selector: 'app-container',
providers: [CalculatedFieldService, NumberFormattingService],
template: `<div class="col-md-8">
<ejs-pivotview #pivotview id='PivotView' height='350' [dataSourceSettings]=dataSourceSettings allowNumberFormatting='true' allowCalculatedField='true' width=width></ejs-pivotview></div>
<div class="col-md-2"><button ej-button id='calculatedfield'>Calculated Field</button></div>`
})
export class AppComponent implements OnInit {
public dataSourceSettings?: IDataOptions;
public button?: Button;
public width?: string;
@ViewChild('pivotview', {static: false})
public pivotGridObj?: PivotView;
ngOnInit(): void {
this.dataSourceSettings = {
dataSource: Pivot_Data as IDataSet[],
expandAll: false,
enableSorting: true,
drilledMembers: [{ name: 'Country', items: ['France'] }],
columns: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold', caption: 'Units Sold' }, { name: 'Amount', caption: 'Sold Amount' }, { name: 'Total', caption: 'Total Amount', type: 'CalculatedField' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
formatSettings: [{ name: 'Amount', format: 'C0' }],
filters: [],
calculatedFieldSettings: [{ name: 'Total', formula: '"Sum(Amount)"+"Sum(Sold)"' }]
};
this.width = '100%';
this.button = new Button({ isPrimary: true });
this.button.appendTo('#calculatedfield');
this.button.element.onclick = (): void => {
this.pivotGridObj?.numberFormattingModule.showNumberFormattingDialog();
};
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Events
NumberFormatting
The event numberFormatting
fires while closing the number formatting dialog on “OK” button click. It allows the user to restrict the customization settings done by the user. It has the following parameters
-
formatName
: It holds the name of the field. -
formatSettings
: It holds theformatSettings
property of the pivot report. -
cancel
: It is a boolean property and by setting this to true , the customization done in number formatting dialog won’t be applied.
In the below sample, the customization done in number formatting dialog for the field “Amount” won’t be applied.
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 { IDataOptions, IDataSet, CalculatedFieldService, PivotView, NumberFormattingService,NumberFormattingEventArgs } from '@syncfusion/ej2-angular-pivotview';
import { Pivot_Data } from './datasource';
import { Button } from '@syncfusion/ej2-buttons';
@Component({
imports: [
PivotViewAllModule,
PivotFieldListAllModule
],
standalone: true,
selector: 'app-container',
providers: [CalculatedFieldService, NumberFormattingService],
template: `<div class="col-md-8">
<ejs-pivotview #pivotview id='PivotView' height='350' [dataSourceSettings]=dataSourceSettings allowNumberFormatting='true' allowCalculatedField='true' (numberFormatting)='numberFormatting($event)' width=width></ejs-pivotview></div>
<div class="col-md-2"><button ej-button id='calculatedfield'>number Format</button></div>`
})
export class AppComponent implements OnInit {
public dataSourceSettings?: IDataOptions;
public button?: Button;
public width?: string;
@ViewChild('pivotview', {static: false})
public pivotGridObj?: PivotView;
numberFormatting (args: NumberFormattingEventArgs): void {
if (args.formatName == 'Amount' ) {
args.cancel = true;
}
}
ngOnInit(): void {
this.dataSourceSettings = {
dataSource: Pivot_Data as IDataSet[],
expandAll: false,
enableSorting: true,
drilledMembers: [{ name: 'Country', items: ['France'] }],
columns: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold', caption: 'Units Sold' }, { name: 'Amount', caption: 'Sold Amount' }, { name: 'Total', caption: 'Total Amount', type: 'CalculatedField' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
formatSettings: [{ name: 'Amount', format: 'C0' }],
filters: [],
calculatedFieldSettings: [{ name: 'Total', formula: '"Sum(Amount)"+"Sum(Sold)"' }]
};
this.width = '100%';
this.button = new Button({ isPrimary: true });
this.button.appendTo('#calculatedfield');
this.button.element.onclick = (): void => {
this.pivotGridObj?.numberFormattingModule.showNumberFormattingDialog();
};
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));