|
| 1 | +import { Component, ViewChild } from "@angular/core"; |
| 2 | + |
| 3 | +import { |
| 4 | + IPivotConfiguration, PivotAggregation, IgxPivotNumericAggregate, |
| 5 | + IgxPivotDateDimension, FilteringExpressionsTree, FilteringLogic, IgxStringFilteringOperand, |
| 6 | + PivotRowLayoutType, |
| 7 | + IPivotUISettings, |
| 8 | + IChangeCheckboxEventArgs, |
| 9 | + IgxPivotGridComponent, |
| 10 | + PivotSummaryPosition |
| 11 | +} from "igniteui-angular" |
| 12 | +import { SALES_DATA } from "../../data/dataToAnalyze"; |
| 13 | + |
| 14 | +export class IgxTotalSaleAggregate { |
| 15 | + public static totalSale: PivotAggregation = (members, data: any) => |
| 16 | + data.reduce((accumulator, value) => accumulator + value.Product.UnitPrice * value.NumberOfUnits, 0); |
| 17 | + |
| 18 | + public static totalMin: PivotAggregation = (members, data: any) => { |
| 19 | + let min = 0; |
| 20 | + if (data.length === 1) { |
| 21 | + min = data[0].Product.UnitPrice * data[0].NumberOfUnits; |
| 22 | + } else if (data.length > 1) { |
| 23 | + const mappedData = data.map(x => x.Product.UnitPrice * x.NumberOfUnits); |
| 24 | + min = mappedData.reduce((a, b) => Math.min(a, b)); |
| 25 | + } |
| 26 | + return min; |
| 27 | + }; |
| 28 | + |
| 29 | + public static totalMax: PivotAggregation = (members, data: any) => { |
| 30 | + let max = 0; |
| 31 | + if (data.length === 1) { |
| 32 | + max = data[0].Product.UnitPrice * data[0].NumberOfUnits; |
| 33 | + } else if (data.length > 1) { |
| 34 | + const mappedData = data.map(x => x.Product.UnitPrice * x.NumberOfUnits); |
| 35 | + max = mappedData.reduce((a, b) => Math.max(a, b)); |
| 36 | + } |
| 37 | + return max; |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +@Component({ |
| 42 | + selector: 'app-pivot-layout-sample', |
| 43 | + styleUrls: ['./pivot-layout.component.scss'], |
| 44 | + templateUrl: './pivot-layout.component.html' |
| 45 | +}) |
| 46 | +export class PivotGridLayoutComponent { |
| 47 | + public data = SALES_DATA; |
| 48 | + public enableSummaries = true; |
| 49 | + public pivotUI: IPivotUISettings = { showRowHeaders: true, rowLayout: PivotRowLayoutType.Horizontal }; |
| 50 | + |
| 51 | + @ViewChild("grid1") |
| 52 | + public grid1: IgxPivotGridComponent; |
| 53 | + |
| 54 | + public pivotConfig = this.buildPivotConfig(); |
| 55 | + |
| 56 | + public buildPivotConfig(): IPivotConfiguration { |
| 57 | + return { |
| 58 | + columns: [ |
| 59 | + new IgxPivotDateDimension( |
| 60 | + { |
| 61 | + memberName: 'Date', |
| 62 | + enabled: true |
| 63 | + }, |
| 64 | + { |
| 65 | + months: false, |
| 66 | + quarters: true, |
| 67 | + fullDate: false |
| 68 | + } |
| 69 | + ) |
| 70 | + ], |
| 71 | + rows: [ |
| 72 | + { |
| 73 | + memberFunction: () => 'All Products', |
| 74 | + memberName: 'AllProducts', |
| 75 | + enabled: true, |
| 76 | + horizontalSummary: this.enableSummaries, |
| 77 | + width: "150px", |
| 78 | + childLevel: { |
| 79 | + memberFunction: (data) => data.Product.Name, |
| 80 | + memberName: 'ProductCategory', |
| 81 | + horizontalSummary: this.enableSummaries, |
| 82 | + width: "150px", |
| 83 | + enabled: true, |
| 84 | + childLevel: { |
| 85 | + memberName: 'City', |
| 86 | + width: "150px", |
| 87 | + memberFunction: (data) => data.Seller.City, |
| 88 | + enabled: true |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + ], |
| 93 | + values: [ |
| 94 | + { |
| 95 | + member: 'Value', |
| 96 | + aggregate: { |
| 97 | + key: 'SUM', |
| 98 | + aggregator: IgxPivotNumericAggregate.sum, |
| 99 | + label: 'Sum' |
| 100 | + }, |
| 101 | + aggregateList: [{ |
| 102 | + key: 'SUM', |
| 103 | + aggregator: IgxPivotNumericAggregate.sum, |
| 104 | + label: 'Sum' |
| 105 | + }], |
| 106 | + enabled: true, |
| 107 | + styles: { |
| 108 | + downFontValue: (rowData: any, columnKey: any): boolean => parseFloat(rowData.aggregationValues.get(columnKey.field)) <= 150, |
| 109 | + upFontValue: (rowData: any, columnKey: any): boolean => parseFloat(rowData.aggregationValues.get(columnKey.field)) > 150 |
| 110 | + }, |
| 111 | + formatter: (value) => value ? '$' + parseFloat(value).toFixed(3) : undefined |
| 112 | + }, |
| 113 | + { |
| 114 | + member: 'AmountofSale', |
| 115 | + displayName: 'Amount of Sale', |
| 116 | + aggregate: { |
| 117 | + key: 'SUM', |
| 118 | + aggregator: IgxTotalSaleAggregate.totalSale, |
| 119 | + label: 'Sum of Sale' |
| 120 | + }, |
| 121 | + aggregateList: [{ |
| 122 | + key: 'SUM', |
| 123 | + aggregator: IgxTotalSaleAggregate.totalSale, |
| 124 | + label: 'Sum of Sale' |
| 125 | + }, { |
| 126 | + key: 'MIN', |
| 127 | + aggregator: IgxTotalSaleAggregate.totalMin, |
| 128 | + label: 'Minimum of Sale' |
| 129 | + }, { |
| 130 | + key: 'MAX', |
| 131 | + aggregator: IgxTotalSaleAggregate.totalMax, |
| 132 | + label: 'Maximum of Sale' |
| 133 | + }], |
| 134 | + enabled: true, |
| 135 | + dataType: 'currency' |
| 136 | + } |
| 137 | + ], |
| 138 | + filters: [ |
| 139 | + { |
| 140 | + memberName: 'SellerName', |
| 141 | + memberFunction: (data) => data.Seller.Name, |
| 142 | + enabled: true |
| 143 | + } |
| 144 | + ] |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + public onShowRowHeaders(event: IChangeCheckboxEventArgs) { |
| 149 | + this.pivotUI.showRowHeaders = event.checked; |
| 150 | + this.grid1.pivotUI = this.pivotUI; |
| 151 | + } |
| 152 | + |
| 153 | + public onLayoutToggle(event: IChangeCheckboxEventArgs) { |
| 154 | + this.pivotUI.rowLayout = event.checked ? PivotRowLayoutType.Horizontal : PivotRowLayoutType.Vertical; |
| 155 | + this.grid1.pivotUI = this.pivotUI; |
| 156 | + } |
| 157 | + |
| 158 | + public onSummariesToggle(event: IChangeCheckboxEventArgs) { |
| 159 | + this.enableSummaries = event.checked; |
| 160 | + this.grid1.pivotConfiguration = this.buildPivotConfig(); |
| 161 | + } |
| 162 | + |
| 163 | + public onSummariesPositionToggle(event: IChangeCheckboxEventArgs) { |
| 164 | + this.pivotUI.horizontalSummariesPosition = event.checked ? PivotSummaryPosition.Top : PivotSummaryPosition.Bottom ; |
| 165 | + this.grid1.pivotUI = this.pivotUI; |
| 166 | + } |
| 167 | +} |
0 commit comments