PivotTable Timeline Slicer

SpreadJS PivotTables supports timeline slicers, which can carry out date-slicing operations conveniently.

Try clicking on slicers in the below demo to see all of the available properties.

Description
app.component.ts
index.html
app.component.html
styles.css
Copy to CodeMine

The Pivot Table Timeline Slicer is managed by SlicerCollection(WorkSheet.slicers), same as the Table Slicers.

Timeline Slicers can only be added to date field. The changes made on Timeline Slicers are the same as using the condition filter, which means "condition" in the label filter.

Add Timeline

If we want to add a Pivot Table Timeline Slicer, create a PivotTable named "pt" (The specific implementation of initPivotTable can be found at the end of the article.):

    var spread = GC.Spread.Sheets.findControl(document.getElementById('ss'));
    var sheet = spread.getActiveSheet();
    initPivotTable();

then add an Timeline slicer to the "date" field.

    var timeline_date = sheet.slicers.add("timeline_date", pt.name(), "date", GC.Spread.Sheets.Slicers.TimelineStyles.light1().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTimeline);

Using Timeline

Then we can control the timeline_date: For example, if you don't need to show the horizontal scrollbar:

    timeline_date.showHorizontalScrollbar(false);

If you want to scroll the timeline to some specific day:

    timeline_date.scrollPosition(new Date('2021-6-23'));

If you want to filter by year:

    timeline_date.level(GC.Spread.Sheets.Slicers.TimelineLevel.years);

A Sample to Create a Pivot Table

function initPivotTable () {
    var dataSource = [
        [ "name", "product", "date", "amount", "price", "sales" ],
        [ "chris", "desk", new Date("2020-10-08T16:00:00.000Z"), 5, 199, 995 ],
        [ "radow", "pen", new Date("2020-09-15T16:00:00.000Z"), 2, 5, 10 ],
        [ "peyton", "pencil", new Date("2021-06-22T16:00:00.000Z"), 6, 1.5, 9 ],
        [ "johnson", "chair", new Date("2021-07-19T16:00:00.000Z"), 7, 68, 476 ],
        [ "vic", "notebook", new Date("2021-01-13T16:00:00.000Z"), 7, 3.2, 22.4 ],
        [ "lan", "desk", new Date("2021-03-12T16:00:00.000Z"), 9, 199, 1791 ],
        [ "chris", "pen", new Date("2021-03-06T16:00:00.000Z"), 4, 5, 20 ],
        [ "chris", "pencil", new Date("2020-09-02T16:00:00.000Z"), 10, 1.5, 15 ],
        [ "radow", "chair", new Date("2020-08-09T16:00:00.000Z"), 3, 68, 204 ],
        [ "peyton", "notebook", new Date("2021-02-08T16:00:00.000Z"), 9, 3.2, 28.8 ],
        [ "johnson", "desk", new Date("2021-07-03T16:00:00.000Z"), 7, 199, 1393],
        [ "vic", "pen", new Date("2021-06-27T16:00:00.000Z"), 8, 5, 40],
        [ "lan", "pencil", new Date("2020-10-10T16:00:00.000Z"), 2, 1.5, 3],
        [ "chris", "chair", new Date("2021-03-04T16:00:00.000Z"), 2, 68, 136],
        [ "chris", "notebook", new Date("2021-02-21T16:00:00.000Z"), 11, 3.2, 35.2],
        [ "radow", "desk", new Date("2021-06-03T16:00:00.000Z"), 6, 199, 1194]
    ];
    var sourceSheet = spread.sheets[0];
    sourceSheet.setArray(0, 0, dataSource);
    sourceSheet.tables.add("table1", 0, 0, 17, 6);
    spread.sheets[0].name("sourceSheet");

    var pivotSheet = new GC.Spread.Sheets.Worksheet('pivotTable1');
    pivotSheet.setRowCount(2000);
    pivotSheet.setColumnCount(30);
    spread.addSheet(1, pivotSheet);

    var pt = pivotSheet.pivotTables.add("pivotTable1", "table1", 1, 1, GC.Spread.Pivot.PivotTableLayoutType.compact, GC.Spread.Pivot.PivotTableThemes.medium1.name(), null);
    pt.suspendLayout();
    pt.add("name", "name", GC.Spread.Pivot.PivotTableFieldType.rowField);
    pt.add("product", "product", GC.Spread.Pivot.PivotTableFieldType.rowField);
    pt.group({
        originFieldName: 'date',
        dateGroups: [
            {
                by: GC.Pivot.DateGroupType.years
            },
            {
                by: GC.Pivot.DateGroupType.quarters
            },
            {
                by: GC.Pivot.DateGroupType.months
            },
        ]
    });
    pt.add("Quarters (date)", "Quarters (date)", GC.Spread.Pivot.PivotTableFieldType.columnField);
    pt.add("sales", "sales", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
    pt.resumeLayout();
    pt.autoFitColumn();
    spread.setActiveSheet("pivotTable1");
}

Custom Timeline Slicer Themes

SpreadJS supports customize the theme of the pivot table timeline slicer. Using code such as the following:

    const customTheme = new GC.Spread.Sheets.Slicers.TimelineStyle();
    customTheme.name("custom0");
    const wholeSlicerStyle = new GC.Spread.Sheets.Slicers.SlicerStyleInfo('red', 'white', '16px Calibri');
    customTheme.wholeSlicerStyle(wholeSlicerStyle);
    spread.customTimelineThemes.add(customTheme);

    slicer.style("custom0");

    slicer.getStyleName();  // "custom0"

    // set default table theme
    spread.defaultTimelineTheme('custom0');
The Pivot Table Timeline Slicer is managed by SlicerCollection(WorkSheet.slicers), same as the Table Slicers. Timeline Slicers can only be added to date field. The changes made on Timeline Slicers are the same as using the condition filter, which means "condition" in the label filter. Add Timeline If we want to add a Pivot Table Timeline Slicer, create a PivotTable named "pt" (The specific implementation of initPivotTable can be found at the end of the article.): then add an Timeline slicer to the "date" field. Using Timeline Then we can control the timeline_date: For example, if you don't need to show the horizontal scrollbar: If you want to scroll the timeline to some specific day: If you want to filter by year: A Sample to Create a Pivot Table Custom Timeline Slicer Themes SpreadJS supports customize the theme of the pivot table timeline slicer. Using code such as the following:
import { Component, NgModule, enableProdMode } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { SpreadSheetsModule } from '@mescius/spread-sheets-angular'; import GC from '@mescius/spread-sheets'; import "@mescius/spread-sheets-shapes"; import "@mescius/spread-sheets-slicers"; import "@mescius/spread-sheets-pivot-addon"; import './styles.css'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { hostStyle = { width: 'calc(100% - 330px)', height: '100%', overflow: 'hidden', float: 'left' }; showHeader: boolean; showHorizontalScrollbar: boolean; showSelectionLabel: boolean; showTimeLevel: boolean; styleName: string; activeSlicer: any; ptName: string; sheet: GC.Spread.Sheets.Worksheet; slicerCount = 0; constructor() { } initSpread($event: any) { let spread = $event.spread; this.initCustomThemes(spread); this.initSheets(spread); this.sheet = spread.getSheet(0); this.initPivotTable(); this.initSlicers(); this.bindEvents(); } initCustomThemes (spread: GC.Spread.Sheets.Workbook) { const theme1 = new GC.Spread.Sheets.Slicers.TimelineStyle(); theme1.fromJSON(GC.Spread.Sheets.Slicers.TimelineStyles.light1().toJSON()); theme1.name('custom1'); theme1.wholeSlicerStyle(new GC.Spread.Sheets.Slicers.SlicerStyleInfo('Accent 5 80')); const theme2 = new GC.Spread.Sheets.Slicers.TimelineStyle(); theme2.fromJSON(GC.Spread.Sheets.Slicers.TimelineStyles.light2().toJSON()); theme2.name('custom2'); theme2.wholeSlicerStyle(new GC.Spread.Sheets.Slicers.SlicerStyleInfo('Accent 2 80')); spread.customTimelineThemes.add(theme1); spread.customTimelineThemes.add(theme2); } initSheets (spread: GC.Spread.Sheets.Workbook) { spread.suspendPaint(); spread.setSheetCount(2); let sheet = spread.getSheet(1); sheet.name("DataSource"); sheet.setRowCount(650); sheet.setColumnWidth(5, 120); sheet.getCell(-1, 5).formatter("YYYY-mm-DD"); sheet.getRange(-1,4,0,1).formatter("$ #,##0"); sheet.setArray(0, 0, pivotSales); let table = sheet.tables.add('tableSales', 0, 0, 637, 6); table.style(GC.Spread.Sheets.Tables.TableThemes["none"]); let sheet0 = spread.getSheet(0); sheet0.name("PivotLayout"); spread.resumePaint(); } initPivotTable() { let sheet = this.sheet; sheet.setRowCount(1000); var option = { showRowHeader: true, showColumnHeader: true, bandRows: true, bandColumns: true }; var pivotTable = sheet.pivotTables.add("pivotTable", "tableSales", 1, 0, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.medium8.name(), option); pivotTable.suspendLayout(); var groupInfo = { originFieldName: "date", dateGroups: [ { by: GC.Pivot.DateGroupType.years }, { by: GC.Pivot.DateGroupType.quarters }, { by: GC.Pivot.DateGroupType.months }, ] }; pivotTable.group(groupInfo); pivotTable.add("Years (date)", "Years (date)", GC.Spread.Pivot.PivotTableFieldType.rowField); pivotTable.add("Quarters (date)", "Quarters (date)", GC.Spread.Pivot.PivotTableFieldType.rowField); pivotTable.add("Months (date)", "Months (date)", GC.Spread.Pivot.PivotTableFieldType.rowField); pivotTable.add("amount", "amount", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum); pivotTable.options.subtotalsPosition = GC.Spread.Pivot.SubtotalsPosition.none; pivotTable.resumeLayout(); pivotTable.autoFitColumn(); this.ptName = pivotTable.name(); return pivotTable; } initSlicers () { let sheet = this.sheet; sheet.suspendPaint(); var timeline_year = sheet.slicers.add("timeline_year", this.ptName, "date", GC.Spread.Sheets.Slicers.TimelineStyles.dark6().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTimeline); timeline_year.position(new GC.Spread.Sheets.Point(355, 20)); timeline_year.level(GC.Spread.Sheets.Slicers.TimelineLevel.years); timeline_year.showSelectionLabel(false); timeline_year.showTimeLevel(false); timeline_year.showHorizontalScrollbar(false); timeline_year.height(100); timeline_year.captionName("Years"); var timeline_quarter = sheet.slicers.add("timeline_quarter", this.ptName, "date", GC.Spread.Sheets.Slicers.TimelineStyles.dark1().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTimeline); timeline_quarter.position(new GC.Spread.Sheets.Point(355, 130)); timeline_quarter.level(GC.Spread.Sheets.Slicers.TimelineLevel.quarters); timeline_quarter.captionName("Quarters"); timeline_quarter.showSelectionLabel(false); var timeline_month = sheet.slicers.add("timeline_month", this.ptName, "date", GC.Spread.Sheets.Slicers.TimelineStyles.light4().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTimeline); timeline_month.position(new GC.Spread.Sheets.Point(355, 290)); timeline_month.captionName("Months"); timeline_month.showTimeLevel(false); sheet.resumePaint(); } bindEvents(pt: any) { let self = this; let sheet = this.sheet as any; sheet.bind(GC.Spread.Sheets.Events.SlicerChanged, function () { let slicers = sheet.slicers.all(); for (let i = 0; i < slicers.length; i++) { if (slicers[i].isSelected()) { self.activeSlicer = slicers[i]; self.updateSlicerInfo(pt); break; } } }); } updateSlicerInfo(pt: GC.Spread.Pivot.PivotTable) { if (!this.activeSlicer) { return; } let slicer = this.activeSlicer; this.showHeader = slicer.showHeader(); this.showHorizontalScrollbar = slicer.showHorizontalScrollbar(); this.showSelectionLabel = slicer.showSelectionLabel(); this.showTimeLevel = slicer.showTimeLevel(); const slicerStyleName = slicer.style().name().toLowerCase(); this.styleName = slicerStyleName.includes('custom') ? slicerStyleName : slicerStyleName.substr(15); } changeProperty(prop: string, v?: any) { if (!this.activeSlicer) { return; } v = v || this[prop]; if (v !== null && v !== undefined) { this.activeSlicer[prop](v); } } setStyle () { if (!this.activeSlicer) { return; } this.activeSlicer.style(this.styleName); } addSlicer () { let sheet = this.sheet as any; sheet.slicers.add("timeline_" + this.slicerCount, this.ptName, "date", GC.Spread.Sheets.Slicers.TimelineStyles.light1().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTimeline); this.slicerCount += 1; } } @NgModule({ imports: [BrowserModule, SpreadSheetsModule, FormsModule], declarations: [AppComponent], exports: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {} enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule);
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/angular/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/spread/source/data/pivotSales.js" type="text/javascript"></script> <!-- Polyfills --> <script src="$DEMOROOT$/en/angular/node_modules/core-js/client/shim.min.js"></script> <script src="$DEMOROOT$/en/angular/node_modules/zone.js/fesm2015/zone.min.js"></script> <!-- SystemJS --> <script src="$DEMOROOT$/en/angular/node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.import('@angular/compiler'); System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('$DEMOROOT$/en/lib/angular/license.ts'); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html>
<div class="sample-tutorial"> <gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)"> </gc-spread-sheets> <div class="options-container"> <div class="block slicer-infos"> <div>Current Selected Timeline Info:</div><br> <div class="slicer-info"> <input type="checkbox" id="showHeader" [(ngModel)]="showHeader" (change)="changeProperty('showHeader')"> <label for="showHeader">Show Header</label> </div> <div class="slicer-info"> <input type="checkbox" id="showHorizontalScrollbar" [(ngModel)]="showHorizontalScrollbar" (change)="changeProperty('showHorizontalScrollbar')"> <label for="showHorizontalScrollbar">Show Horizontal Scrollbar</label> </div> <div class="slicer-info"> <input type="checkbox" id="showSelectionLabel" [(ngModel)]="showSelectionLabel" (change)="changeProperty('showSelectionLabel')"> <label for="showSelectionLabel">Show Selection Label</label> </div> <div class="slicer-info"> <input type="checkbox" id="showTimeLevel" [(ngModel)]="showTimeLevel" (change)="changeProperty('showTimeLevel')"> <label for="showTimeLevel">Show Time Level</label> </div> </div> <div class="block"> <div>Add Timeline</div><br> <button id="addSlicerBtn" (click)="addSlicer()">Add Timeline Slicer</button> </div> <div class="block"> <div>Change Current Timeline Style</div><br> <div class="slicerStyle"> <select class="select-list" name="slicerStyle" id="slicerStyle" [(ngModel)]="styleName"> <option value="light1">light1</option> <option value="light2">light2</option> <option value="light3">light3</option> <option value="light4">light4</option> <option value="light5">light5</option> <option value="light6">light6</option> <option value="dark1">dark1</option> <option value="dark2">dark2</option> <option value="dark3">dark3</option> <option value="dark4">dark4</option> <option value="dark5">dark5</option> <option value="dark6">dark6</option> <option value="custom1">custom1</option> <option value="custom2">custom2</option> </select> <button class="select-button" id="changeStyle" (click)="setStyle()">Change</button> </div> </div> </div> </div>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 330px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 330px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .pivottable-filter{ height: 100px; } .pivot-filter{ width: 200px; height: 25px; display: block; margin-bottom: 10px; float: left; } .filter-input{ width: 200px; height: 20px; display: block; /* margin-left: 15px; */ margin-top: 10px; } .set-filter{ width: 200px; margin-top: 20px; /* float: right; */ } .dateFilterSettingItemDiv{ margin: 11px; } .set-filter{ width: 200px; margin-top: 20px; /* float: right; */ } .filter-input{ width: 200px; height: 20px; display: block; /* margin-left: 15px; */ margin-top: 10px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .whole-field{ margin-bottom: 10px; } .block { border: 1px solid gray; padding-left: 5px; padding-top: 10px; padding-bottom: 10px; margin-bottom: 1px; } .select-list { width: 120px; } .select-button { width: 80px; }
(function (global) { System.config({ transpiler: 'ts', typescriptOptions: { tsconfig: true }, meta: { 'typescript': { "exports": "ts" }, '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'core-js': 'npm:core-js/client/shim.min.js', 'zone': 'npm:zone.js/fesm2015/zone.min.js', 'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js', '@angular/core': 'npm:@angular/core/fesm2022', '@angular/common': 'npm:@angular/common/fesm2022/common.mjs', '@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs', '@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs', '@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs', '@angular/router': 'npm:@angular/router/fesm2022/router.mjs', '@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs', 'jszip': 'npm:jszip/dist/jszip.min.js', 'typescript': 'npm:typescript/lib/typescript.js', 'ts': './plugin.js', 'tslib':'npm:tslib/tslib.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js', '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/index.js', '@mescius/spread-sheets-slicers': 'npm:@mescius/spread-sheets-slicers/index.js', '@mescius/spread-sheets-pivot-addon': 'npm:@mescius/spread-sheets-pivot-addon/index.js', '@mescius/spread-sheets-angular': 'npm:@mescius/spread-sheets-angular/fesm2020/mescius-spread-sheets-angular.mjs', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'ts' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, "node_modules/@angular": { defaultExtension: 'mjs' }, "@mescius/spread-sheets-angular": { defaultExtension: 'mjs' }, '@angular/core': { defaultExtension: 'mjs', main: 'core.mjs' } } }); })(this);