-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathdataset.ts
More file actions
2628 lines (2546 loc) · 103 KB
/
dataset.ts
File metadata and controls
2628 lines (2546 loc) · 103 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { isArray, isValid } from '@visactor/vutils';
import type {
FilterRules,
IPivotTableDataConfig,
SortRule,
AggregationRules,
AggregationRule,
SortRules,
DerivedFieldRule,
DerivedFieldRules,
SortByIndicatorRule,
SortByRule,
SortTypeRule,
SortFuncRule,
Totals,
MappingRules,
SortOrder,
IHeaderTreeDefine,
CollectValueBy,
CollectedValue,
IIndicator,
IPivotChartDataConfig,
CalculateddFieldRules,
SortType
} from '../ts-types';
import { AggregationType } from '../ts-types';
import type { Aggregator, IAggregator } from '../ts-types/dataset/aggregation';
import {
registeredAggregators,
AvgAggregator,
CountAggregator,
CustomAggregator,
MaxAggregator,
MinAggregator,
NoneAggregator,
RecalculateAggregator,
RecordAggregator,
SumAggregator,
naturalSort,
sortBy,
typeSort
} from '../ts-types/dataset/aggregation';
import { IndicatorDimensionKeyPlaceholder } from '../tools/global';
import { join } from '../tools/join';
/**
* 数据处理模块
*/
export class Dataset {
/**
* 用户配置
*/
dataConfig?: IPivotTableDataConfig | IPivotChartDataConfig;
// /**
// * 分页配置
// */
// pagination: IPagination;
/**
* 明细数据
*/
records?: any[] | Record<string, any[]>;
filteredRecords?: any[] | Record<string, any[]>;
/**
* 树形节点,最后的子节点对应到body部分的每个单元格 树结构: 行-列-单元格
*/
tree: Record<string, Record<string, Aggregator[]>> = {};
changedTree: Record<string, Record<string, any[]>> = {};
private colFlatKeys: Record<string, number> = {}; //记录某个colKey已经被添加过colKeys到
private rowFlatKeys: Record<string, number> = {}; //记录某个rowKey已经被添加过rowKeys到
//列表头的每列对应的表头键值
colKeys: string[][] = [];
//行表头的每行对应的表头键值
rowKeys: string[][] = [];
// 存储下未排序即初始normal下rowKeys和colKeys
colKeys_normal: string[][] = [];
rowKeys_normal: string[][] = [];
// /**
// * 对应dataset中的rowKeys,行表头的每行表头键值,包含小计总计
// */
// rowKeysPath: string[][];
// /**
// * 对应dataset中的colKeys,列表头的每列表头键值,包含小计总计
// */
// colKeysPath: string[][];
// allTotal: SumAggregator;
rowOrder = 'key_a_to_z';
colOrder = 'key_a_to_z';
//是否已排序
sorted = false;
//排序规则
sortRules?: SortRules;
//过滤规则
filterRules?: FilterRules;
//聚合规则
aggregationRules?: AggregationRules;
//派生字段规则
derivedFieldRules?: DerivedFieldRules;
mappingRules?: MappingRules;
calculatedFieldRules?: CalculateddFieldRules;
/** 计算字段 */
calculatedFiledKeys?: string[];
calculatedFieldDependIndicatorKeys?: string[];
//汇总配置
totals?: Totals;
//全局统计各指标的极值
indicatorStatistics: { max: Aggregator; min: Aggregator; total: Aggregator }[] = [];
stringJoinChar = String.fromCharCode(0);
//缓存rows对应每个值是否为汇总字段
private rowsIsTotal: boolean[] = [];
private colsIsTotal: boolean[] = [];
private colGrandTotalLabel: string;
private colSubTotalLabel: string;
private rowGrandTotalLabel: string;
private rowSubTotalLabel: string;
private needSplitPositiveAndNegative?: boolean;
collectValuesBy?: Record<string, CollectValueBy>; //收集维度值,field收集维度,by按什么进行分组收集
collectedValues: Record<string, Record<string, CollectedValue>> = {};
cacheCollectedValues: Record<string, Record<string, CollectedValue>> = {};
rows: string[];
rowsHasValue: boolean[]; //rows中的key是否有在records中体现
columns: string[];
columnsHasValue: boolean[]; //columns中的key是否有在records中体现
indicatorKeys: string[];
indicatorKeysIncludeCalculatedFieldDependIndicatorKeys: string[];
customRowTree?: IHeaderTreeDefine[];
customColTree?: IHeaderTreeDefine[];
// 存储自定义表头树 对应每一行的 key path
customRowTreeDimensionPaths: {
dimensionKey?: string | number;
value: string;
indicatorKey?: string | number;
isVirtual?: boolean;
childKeys?: (string | number)[];
}[][];
// 存储自定义表头树 对应每一行的 key path
customColTreeDimensionPaths: {
dimensionKey?: string | number;
value: string;
indicatorKey?: string | number;
isVirtual?: boolean;
}[][];
// // 存储行表头path 这个是全量的 对比于分页截取的rowKeysPath;
// private rowKeysPath_FULL: string[][];
colHeaderTree: any[];
rowHeaderTree: any[];
rowHierarchyType: 'grid' | 'tree' | 'grid-tree';
columnHierarchyType: 'grid' | 'grid-tree';
indicators?: (string | IIndicator)[];
indicatorsAsCol: boolean;
// 记录用户传入的汇总数据
totalRecordsTree: Record<string, Record<string, Aggregator[]>> = {};
hasExtensionRowTree?: boolean;
parseCustomTreeToMatchRecords?: boolean;
constructor(
dataConfig: IPivotTableDataConfig | IPivotChartDataConfig | undefined,
// pagination: IPagination,
rows: string[],
columns: string[],
indicatorKeys: string[],
indicators: (string | IIndicator)[] | undefined,
indicatorsAsCol: boolean,
records: any[] | Record<string, any[]> | undefined,
rowHierarchyType?: 'grid' | 'tree' | 'grid-tree',
columnHierarchyType?: 'grid' | 'grid-tree',
customColTree?: IHeaderTreeDefine[],
customRowTree?: IHeaderTreeDefine[],
needSplitPositiveAndNegative?: boolean,
hasExtensionRowTree?: boolean,
parseCustomTreeToMatchRecords?: boolean
) {
this.registerAggregators();
this.dataConfig = dataConfig;
this.filterRules = this.dataConfig?.filterRules;
this.rowHierarchyType = rowHierarchyType ?? 'grid';
this.columnHierarchyType = columnHierarchyType ?? 'grid';
// this.allTotal = new SumAggregator(this.indicators[0]);
this.sortRules = this.dataConfig?.sortRules;
this.aggregationRules = this.dataConfig?.aggregationRules;
this.derivedFieldRules = this.dataConfig?.derivedFieldRules;
this.mappingRules = this.dataConfig?.mappingRules;
this.calculatedFieldRules = this.dataConfig?.calculatedFieldRules;
this.calculatedFiledKeys = this.calculatedFieldRules?.map(rule => rule.key) ?? [];
this.calculatedFieldDependIndicatorKeys =
this.calculatedFieldRules?.reduce((arr: string[], rule) => {
for (let i = 0; i < rule.dependIndicatorKeys.length; i++) {
if (arr.indexOf(rule.dependIndicatorKeys[i]) === -1) {
arr.push(rule.dependIndicatorKeys[i]);
}
}
return arr;
}, []) ?? [];
this.totals = this.dataConfig?.totals;
this.rows = rows;
this.columns = columns;
this.indicatorKeys = indicatorKeys;
this.indicatorKeysIncludeCalculatedFieldDependIndicatorKeys = [...indicatorKeys];
for (let m = 0; m < this.calculatedFieldDependIndicatorKeys.length; m++) {
if (
this.indicatorKeysIncludeCalculatedFieldDependIndicatorKeys.indexOf(
this.calculatedFieldDependIndicatorKeys[m]
) === -1
) {
this.indicatorKeysIncludeCalculatedFieldDependIndicatorKeys.push(this.calculatedFieldDependIndicatorKeys[m]);
}
}
this.indicatorsAsCol = indicatorsAsCol;
this.indicators = indicators;
this.customColTree = customColTree;
this.customRowTree = customRowTree;
this.hasExtensionRowTree = hasExtensionRowTree;
this.parseCustomTreeToMatchRecords = parseCustomTreeToMatchRecords;
if (this.parseCustomTreeToMatchRecords) {
this.customColTreeDimensionPaths = this.customTreeToDimensionPathArr(this.customColTree, 'col');
if (!this.hasExtensionRowTree) {
this.customRowTreeDimensionPaths = this.customTreeToDimensionPathArr(this.customRowTree, 'row');
}
}
this.colGrandTotalLabel = this.totals?.column?.grandTotalLabel ?? '总计';
this.colSubTotalLabel = this.totals?.column?.subTotalLabel ?? '小计';
this.rowGrandTotalLabel = this.totals?.row?.grandTotalLabel ?? '总计';
this.rowSubTotalLabel = this.totals?.row?.subTotalLabel ?? '小计';
this.collectValuesBy = (this.dataConfig as IPivotChartDataConfig)?.collectValuesBy;
this.needSplitPositiveAndNegative = needSplitPositiveAndNegative ?? false;
this.rowsIsTotal = new Array(this.rows?.length ?? 0).fill(false);
this.colsIsTotal = new Array(this.columns?.length ?? 0).fill(false);
if (this.totals?.row && this.totals.row.showSubTotals !== false && this.totals.row.subTotalsDimensions) {
for (let i = 0, len = this.totals?.row?.subTotalsDimensions?.length ?? 0; i < len; i++) {
const dimension = this.totals.row.subTotalsDimensions[i];
const dimensionIndex = this.rows.indexOf(dimension);
this.rowsIsTotal[dimensionIndex] = true;
}
}
if (this.totals?.column && this.totals.column.showSubTotals !== false && this.totals.column.subTotalsDimensions) {
for (let i = 0, len = this.totals?.column?.subTotalsDimensions?.length ?? 0; i < len; i++) {
const dimension = this.totals.column.subTotalsDimensions[i];
const dimensionIndex = this.columns.indexOf(dimension);
this.colsIsTotal[dimensionIndex] = true;
}
}
// this.rowKeysPath = [];
// this.rowKeysPath_FULL = [];
// this.colKeysPath = [];
this.setRecords(records);
// this.updatePagination(pagination);
}
setRecords(records: any[] | Record<string, any[]>) {
this.records = records;
this.collectedValues = {};
this.cacheCollectedValues = {};
this.totalRecordsTree = {};
this.tree = {};
this.colFlatKeys = {};
this.rowFlatKeys = {};
this.colKeys = [];
this.rowKeys = [];
this.rowsHasValue = [];
this.columnsHasValue = [];
this.sorted = false;
if (records) {
//处理数据
this.records = records;
const t0 = typeof window !== 'undefined' ? window.performance.now() : 0;
// if (records?.[0]?.constructor !== Array) {
// 不能加这个判断来提升性能了,
// PivotChart 会有这种设置情况
// records: {
// "0": [
// {
// "10001": "数量",
// "10002": "37534",
// "10003": "sum_1700027602758",
// "30001": "数量",
// "1700046734980": "",
// sum_1700027602758: "37534",
// },
// ],
// },
this.processRecords();
// }
//processRecord中按照collectValuesBy 收集了维度值。现在需要对有聚合需求的sumby 处理收集维度值范围
this.processCollectedValuesWithSumBy();
//processRecord中按照collectValuesBy 收集了维度值。现在需要对有排序需求的处理sortby
this.generateCollectedValuesSortRule();
this.processCollectedValuesWithSortBy();
const t1 = typeof window !== 'undefined' ? window.performance.now() : 0;
console.log('processRecords:', t1 - t0);
// 处理汇总
const t4 = typeof window !== 'undefined' ? window.performance.now() : 0;
this.totalStatistics();
const t5 = typeof window !== 'undefined' ? window.performance.now() : 0;
console.log('totalStatistics:', t5 - t4);
this.rowKeys_normal = this.rowKeys.slice();
this.colKeys_normal = this.colKeys.slice();
//对维度排序
const t2 = typeof window !== 'undefined' ? window.performance.now() : 0;
this.sortKeys();
const t3 = typeof window !== 'undefined' ? window.performance.now() : 0;
console.log('sortKeys:', t3 - t2);
//转为树形
// const t4 = typeof window !== 'undefined' ? window.performance.now() : 0;
// this.madeTree(this.rowKeys);
// const t41 = typeof window !== 'undefined' ? window.performance.now() : 0;
// console.log('madeTree:', t41 - t4);
const t7 = typeof window !== 'undefined' ? window.performance.now() : 0;
if (this.customRowTree) {
// if (!this.indicatorsAsCol) {
// this.customRowTree = this._adjustCustomTree(this.customRowTree);
// }
this.rowHeaderTree = this.customRowTree;
} else {
if (this.rowHierarchyType === 'tree') {
this.rowHeaderTree = this.ArrToTree1(
this.rowKeys,
this.rows.filter((key, index) => {
return this.rowsHasValue[index];
}),
this.indicatorsAsCol ? undefined : this.indicators,
this.totals?.row?.showGrandTotals ||
(!this.indicatorsAsCol && this.columns.length === 0) ||
(this.indicatorsAsCol && this.rows.length === 0),
this.rowGrandTotalLabel,
this.totals?.row?.showGrandTotalsOnTop ?? false
);
} else {
this.rowHeaderTree = this.ArrToTree(
this.rowKeys,
this.rows.filter((key, index) => {
return this.rowsHasValue[index];
}),
this.indicatorsAsCol ? undefined : this.indicators,
this.rowsIsTotal,
this.totals?.row?.showGrandTotals || (this.indicatorsAsCol && this.rows.length === 0),
this.rowGrandTotalLabel,
this.rowSubTotalLabel,
this.totals?.row?.showGrandTotalsOnTop ?? false,
this.totals?.row?.showSubTotalsOnTop ?? false
);
}
}
if (this.customColTree) {
// if (this.indicatorsAsCol) {
// this.customColTree = this._adjustCustomTree(this.customColTree);
// }
this.colHeaderTree = this.customColTree;
} else {
// if (this.columnHierarchyType !== 'grid') {
// this.colHeaderTree = this.ArrToTree1(
// this.colKeys,
// this.columns.filter((key, index) => {
// return this.columnsHasValue[index];
// }),
// this.indicatorsAsCol ? this.indicators : undefined,
// this.totals?.column?.showGrandTotals ||
// (!this.indicatorsAsCol && this.columns.length === 0) ||
// (this.indicatorsAsCol && this.rows.length === 0),
// this.colGrandTotalLabel
// );
// } else {
this.colHeaderTree = this.ArrToTree(
this.colKeys,
this.columns.filter((key, index) => {
return this.columnsHasValue[index];
}),
this.indicatorsAsCol ? this.indicators : undefined,
this.colsIsTotal,
this.totals?.column?.showGrandTotals || (!this.indicatorsAsCol && this.columns.length === 0), // || this.rows.length === 0,//todo 这里原有逻辑暂时注释掉
this.colGrandTotalLabel,
this.colSubTotalLabel,
this.totals?.column?.showGrandTotalsOnLeft ?? false,
this.totals?.column?.showSubTotalsOnLeft ?? false
);
// }
}
const t8 = typeof window !== 'undefined' ? window.performance.now() : 0;
console.log('TreeToArr:', t8 - t7);
if ((this.dataConfig as IPivotChartDataConfig)?.isPivotChart) {
// 处理PivotChart双轴图0值对齐
// this.dealWithZeroAlign();
// 记录PivotChart维度对应的数据
this.cacheDeminsionCollectedValues();
}
}
}
//将聚合类型注册 收集到aggregators
registerAggregator(type: string, aggregator: any) {
registeredAggregators[type] = aggregator;
}
//将聚合类型注册
registerAggregators() {
this.registerAggregator(AggregationType.RECORD, RecordAggregator);
this.registerAggregator(AggregationType.SUM, SumAggregator);
this.registerAggregator(AggregationType.COUNT, CountAggregator);
this.registerAggregator(AggregationType.MAX, MaxAggregator);
this.registerAggregator(AggregationType.MIN, MinAggregator);
this.registerAggregator(AggregationType.AVG, AvgAggregator);
this.registerAggregator(AggregationType.NONE, NoneAggregator);
this.registerAggregator(AggregationType.RECALCULATE, RecalculateAggregator);
this.registerAggregator(AggregationType.CUSTOM, CustomAggregator);
}
/**processRecord中按照collectValuesBy 收集了维度值。现在需要对有聚合需求的 处理收集维度值范围 */
private processCollectedValuesWithSumBy() {
for (const field in this.collectedValues) {
if (this.collectValuesBy?.[field]?.sumBy) {
for (const byKeys in this.collectedValues[field]) {
let max;
//考虑有markLine设置sum的情况
if (this.collectValuesBy[field]?.extendRange === 'sum') {
max = Object.values(this.collectedValues[field][byKeys]).reduce((acc, cur) => {
return acc + cur.value();
}, 0);
max += Math.round(max / 20);
} else {
// 寻找最大值作为轴范围的max
max = Object.values(this.collectedValues[field][byKeys]).reduce((acc, cur) => {
return cur.value() > acc ? cur.value() : acc;
}, Number.MIN_SAFE_INTEGER);
//考虑有markLine设置max的情况
if (this.collectValuesBy[field]?.extendRange === 'max') {
max += Math.round(max / 20);
} else if (typeof this.collectValuesBy[field]?.extendRange === 'number') {
max = Math.max(max, this.collectValuesBy[field]?.extendRange as number);
}
}
const min = Object.values(this.collectedValues[field][byKeys]).reduce((acc, cur) => {
return cur.value() < acc ? cur.value() : acc;
}, Number.MAX_SAFE_INTEGER);
let positiveMax;
let negativeMin;
if (this.needSplitPositiveAndNegative) {
positiveMax = Object.values(this.collectedValues[field][byKeys]).reduce((acc, cur) => {
return cur.positiveValue() > acc ? cur.positiveValue() : acc;
}, Number.MIN_SAFE_INTEGER);
negativeMin = Object.values(this.collectedValues[field][byKeys]).reduce((acc, cur) => {
return cur.negativeValue() < acc ? cur.negativeValue() : acc;
}, Number.MAX_SAFE_INTEGER);
}
this.collectedValues[field][byKeys] = {};
(
this.collectedValues[field][byKeys] as {
max: number;
min: number;
positiveMax?: number;
negativeMin?: number;
}
).max = max;
(
this.collectedValues[field][byKeys] as {
max: number;
min: number;
positiveMax?: number;
negativeMin?: number;
}
).min = min;
if (this.needSplitPositiveAndNegative) {
(
this.collectedValues[field][byKeys] as {
max: number;
min: number;
positiveMax?: number;
negativeMin?: number;
}
).positiveMax = positiveMax;
(
this.collectedValues[field][byKeys] as {
max: number;
min: number;
positiveMax?: number;
negativeMin?: number;
}
).negativeMin = negativeMin;
}
}
}
}
}
/**processRecord中按照collectValuesBy 收集了维度值。现在需要对有排序需求的处理 */
private processCollectedValuesWithSortBy() {
const that = this;
for (const field in this.collectedValues) {
if (this.collectValuesBy?.[field]?.sortBy) {
for (const byKeys in this.collectedValues[field]) {
this.collectedValues[field][byKeys] = (this.collectedValues[field][byKeys] as Array<string>).sort(
(a, b) =>
(that.collectValuesBy![field].sortBy?.indexOf(a) ?? -1) -
(that.collectValuesBy![field].sortBy?.indexOf(b) ?? -1)
);
}
}
}
}
/**
* 为了轴顺序的一致 这里将收集到的轴范围进行排序 并写入sortBy。这样不同单元格的轴顺序保持一致 同时过滤数据updateFilterRules后也不影响排序
*/
private generateCollectedValuesSortRule() {
for (const field in this.collectedValues) {
if (this.collectValuesBy && this.collectValuesBy[field] && !this.collectValuesBy[field].sortBy) {
let sortByRule: string[] = [];
for (const byKeys in this.collectedValues[field]) {
if (Array.isArray(this.collectedValues[field][byKeys])) {
// 将数组中的元素合并到数组sortByRule中
sortByRule.push(...(this.collectedValues[field][byKeys] as Array<string>));
// 使用Set和Array.from()方法去除重复值
sortByRule = Array.from(new Set(sortByRule));
}
}
if (sortByRule.length > 0) {
this.collectValuesBy[field].sortBy = sortByRule;
}
}
}
}
/**
* 处理数据,遍历所有条目,过滤和派生字段的处理有待优化TODO
*/
private processRecords() {
let isNeedFilter = false;
if ((this.filterRules?.length ?? 0) >= 1) {
isNeedFilter = true;
}
//常规records是数组的情况
if (Array.isArray(this.records)) {
if (!this.filteredRecords) {
this.filteredRecords = [];
}
for (let i = 0, len = this.records.length; i < len; i++) {
const record = this.records[i];
if (!isNeedFilter || this.filterRecord(record)) {
(this.filteredRecords as any[]).push(record);
record && this.processRecord(record);
}
}
} else {
if (!this.filteredRecords) {
this.filteredRecords = {};
}
//records是用户传来的按指标分组后的数据
for (const key in this.records) {
for (let i = 0, len = this.records[key].length; i < len; i++) {
const record = this.records[key][i];
if (!isNeedFilter || this.filterRecord(record)) {
if (!(this.filteredRecords as Record<string, any[]>)[key]) {
(this.filteredRecords as Record<string, any[]>)[key] = [];
}
(this.filteredRecords as Record<string, any[]>)[key].push(record);
this.processRecord(record, key);
}
}
}
}
this.rowFlatKeys = {};
this.colFlatKeys = {};
}
private filterRecord(record: any) {
let isReserved = true;
if (this.filterRules) {
for (let i = 0; i < this.filterRules.length; i++) {
const filterRule = this.filterRules[i];
if (filterRule.filterKey) {
const filterValue = record[filterRule.filterKey];
if (filterRule.filteredValues?.indexOf(filterValue) === -1) {
isReserved = false;
break;
}
} else if (!filterRule.filterFunc?.(record)) {
isReserved = false;
break;
}
}
}
return isReserved;
}
/**
* 处理单条数据
* @param record
* @param assignedIndicatorKey 指定要计算的指标key 外部用户 用指标做records的key 分别存储不同指标对应的数据时 会传入这个参数
* @returns
*/
private processRecord(record: any, assignedIndicatorKey?: string) {
//这个派生字段的计算位置有待确定,是否应该放到filter之前
this.derivedFieldRules?.forEach((derivedFieldRule: DerivedFieldRule, i: number) => {
if (derivedFieldRule.fieldName && derivedFieldRule.derivedFunc) {
record[derivedFieldRule.fieldName] = derivedFieldRule.derivedFunc(record);
}
});
//#region 按照collectValuesBy 收集维度值
for (const field in this.collectValuesBy) {
if (isValid(record[field])) {
if (!this.collectedValues[field]) {
this.collectedValues[field] = {};
}
const collectKeys = this.collectValuesBy[field].by.map(byField => record[byField]).join(this.stringJoinChar);
if (!this.collectedValues[field][collectKeys]) {
if (this.collectValuesBy[field].sumBy) {
this.collectedValues[field][collectKeys] = {};
} else if (this.collectValuesBy[field].range) {
this.collectedValues[field][collectKeys] = {
min: Number.MAX_SAFE_INTEGER,
max: Number.MIN_SAFE_INTEGER
};
} else {
this.collectedValues[field][collectKeys] = [];
}
}
if (this.collectValuesBy[field].sumBy) {
const sumByKeys: string = this.collectValuesBy[field].sumBy
?.map(byField => record[byField])
.join(this.stringJoinChar);
if (!(this.collectedValues[field][collectKeys] as any)[sumByKeys]) {
(this.collectedValues[field][collectKeys] as any)[sumByKeys] = new registeredAggregators[
AggregationType.SUM
]({
key: field,
field: field,
isRecord: undefined,
needSplitPositiveAndNegative: this.needSplitPositiveAndNegative
});
}
(this.collectedValues[field][collectKeys] as any)[sumByKeys].push(record);
} else if (this.collectValuesBy[field].range) {
const fieldRange = this.collectedValues[field][collectKeys] as {
max: number;
min: number;
};
let max = Math.max(record[field], fieldRange.max);
let min = Math.min(record[field], fieldRange.min);
// 处理considerFields
if (this.collectValuesBy[field].considerFields) {
for (const considerField of this.collectValuesBy[field].considerFields) {
if (record[considerField]) {
if (typeof record[considerField] === 'number') {
max = Math.max(record[considerField], max);
min = Math.min(record[considerField], min);
} else if (Array.isArray(record[considerField])) {
max = Math.max(...record[considerField], max);
min = Math.min(...record[considerField], min);
}
}
}
}
if (!isNaN(max)) {
fieldRange.max = max;
fieldRange.min = min;
}
} else {
const fieldRange = this.collectedValues[field][collectKeys] as Array<string>;
if (fieldRange.indexOf(record[field]) === -1) {
fieldRange.push(record[field]);
}
}
}
}
//#endregion
let isToTalRecord = false;
//#region 收集rowKey colKey
// 原先的逻辑不关心customRowTree 只是根据rows 从record上收集维度path。现在考虑了rowTree和colTree的传入,需要依据colTree的真实定义的path来给数据做对应关系。
// 一条数据可能对应多个path(多列),所以这里收集rowKeys colKeys 是个path的数组,同时兼容path中有indicatorKey和没有indicatorKey的情况
const colKeys: { colKey: string[]; indicatorKey: string | number }[] = [];
const rowKeys: { rowKey: string[]; indicatorKey: string | number }[] = [];
if (
this.parseCustomTreeToMatchRecords &&
!(this.dataConfig as IPivotChartDataConfig)?.isPivotChart &&
this.customRowTree?.length &&
!assignedIndicatorKey && // 目前应该透视图才有可能传入assignedIndicatorKey 所以前面判断了isPivotChart 这个应该也没用了
!this.hasExtensionRowTree // 有扩展树的情况不走新处理逻辑 走旧的即可
) {
const rowTreePath = this.getFieldMatchRowDimensionPaths(record);
if (rowTreePath.length > 0) {
for (let i = 0, len = rowTreePath.length; i < len; i++) {
const rowPath = rowTreePath[i];
const rowKey: string[] = [];
let indicatorKey;
for (let j = 0, len1 = rowPath.length; j < len1; j++) {
if (isValid(rowPath[j].indicatorKey)) {
indicatorKey = rowPath[j].indicatorKey;
} else {
rowKey.push(rowPath[j].value);
}
}
rowKeys.push({ rowKey, indicatorKey });
}
}
} else {
const rowKey: string[] = [];
rowKeys.push({ rowKey, indicatorKey: assignedIndicatorKey });
for (let l = 0, len1 = this.rows.length; l < len1; l++) {
const rowAttr = this.rows[l];
if (rowAttr in record) {
this.rowsHasValue[l] = true;
rowKey.push(record[rowAttr]);
} else if (rowAttr !== IndicatorDimensionKeyPlaceholder) {
//如果数据中缺失某个维度的值 可以认为是用户传入的汇总数据
if (
this.dataConfig?.totals?.row?.showGrandTotals &&
l === 0 &&
!this.rows.find((rk: string) => {
// 判断没有其他字段在record中 例如rows中维度有省份和城市,当前在判断省份 数据中确实省份自动 可以认为是行总计的前提是城市也不应该存在
return rk in record;
})
) {
rowKey.push(this.rowGrandTotalLabel);
isToTalRecord = true;
break;
} else if (
// this.dataConfig?.totals?.row?.showSubTotals &&
this.dataConfig?.totals?.row?.subTotalsDimensions &&
this.dataConfig?.totals?.row?.subTotalsDimensions.indexOf(this.rows[l - 1]) >= 0
) {
if (this.rowHierarchyType !== 'tree') {
//如果是tree的话 不附加标签'小计'
rowKey.push(this.rowSubTotalLabel);
}
isToTalRecord = true;
break;
}
}
}
}
if (
this.parseCustomTreeToMatchRecords &&
!(this.dataConfig as IPivotChartDataConfig)?.isPivotChart &&
this.customColTree?.length &&
!assignedIndicatorKey &&
!this.hasExtensionRowTree
) {
const colTreePath = this.getFieldMatchColDimensionPaths(record);
if (colTreePath.length > 0) {
for (let i = 0, len = colTreePath.length; i < len; i++) {
const colPath = colTreePath[i];
const colKey: string[] = [];
let indicatorKey;
for (let j = 0, len1 = colPath.length; j < len1; j++) {
if (isValid(colPath[j].indicatorKey)) {
indicatorKey = colPath[j].indicatorKey;
} else {
colKey.push(colPath[j].value);
}
}
colKeys.push({ colKey: colKey, indicatorKey });
}
}
} else {
const colKey: string[] = [];
colKeys.push({ colKey, indicatorKey: assignedIndicatorKey });
for (let n = 0, len2 = this.columns.length; n < len2; n++) {
const colAttr = this.columns[n];
if (colAttr in record) {
this.columnsHasValue[n] = true;
colKey.push(record[colAttr]);
} else if (colAttr !== IndicatorDimensionKeyPlaceholder) {
//如果数据中缺失某个维度的值 可以认为是用户传入的汇总数据
if (
this.dataConfig?.totals?.column?.showGrandTotals &&
n === 0 &&
!this.columns.find((ck: string) => {
// 判断没有其他字段在record中
return ck in record;
})
) {
colKey.push(this.colGrandTotalLabel);
isToTalRecord = true;
break;
} else if (
// this.dataConfig?.totals?.column?.showSubTotals &&
this.dataConfig?.totals?.column?.subTotalsDimensions &&
this.dataConfig?.totals?.column?.subTotalsDimensions.indexOf(this.columns[n - 1]) >= 0
) {
// if (this.columnHierarchyType === 'grid') {
colKey.push(this.colSubTotalLabel);
// }
isToTalRecord = true;
break;
}
}
}
}
//#endregion
//#region 对path的数组 rowKeys和colKeys 做双重循环
for (let row_i = 0; row_i < rowKeys.length; row_i++) {
const rowKey = rowKeys[row_i].rowKey;
let assignedIndicatorKey_value;
if (!this.indicatorsAsCol) {
assignedIndicatorKey_value = rowKeys[row_i].indicatorKey;
}
for (let col_j = 0; col_j < colKeys.length; col_j++) {
const colKey = colKeys[col_j].colKey;
if (this.indicatorsAsCol) {
assignedIndicatorKey_value = colKeys[col_j].indicatorKey;
}
// 原生的数组join方法会将null转换为空字符串
const flatRowKey = join(rowKey, this.stringJoinChar);
const flatColKey = join(colKey, this.stringJoinChar);
//#region 收集用户传入的汇总数据到totalRecordsTree
//该条数据为汇总数据
if (isToTalRecord) {
if (!this.totalRecordsTree[flatRowKey]) {
this.totalRecordsTree[flatRowKey] = {};
}
if (!this.totalRecordsTree[flatRowKey][flatColKey]) {
this.totalRecordsTree[flatRowKey][flatColKey] = [];
}
const toComputeIndicatorKeys = this.indicatorKeysIncludeCalculatedFieldDependIndicatorKeys;
for (let i = 0; i < toComputeIndicatorKeys.length; i++) {
if (this.calculatedFiledKeys.indexOf(toComputeIndicatorKeys[i]) >= 0) {
const calculatedFieldRule = this.calculatedFieldRules?.find(
rule => rule.key === toComputeIndicatorKeys[i]
);
if (!this.totalRecordsTree[flatRowKey]?.[flatColKey]?.[i]) {
this.totalRecordsTree[flatRowKey][flatColKey][i] = new registeredAggregators[
AggregationType.RECALCULATE
]({
key: toComputeIndicatorKeys[i],
field: toComputeIndicatorKeys[i],
isRecord: true,
// single: true,
formatFun: (
this.indicators?.find((indicator: string | IIndicator) => {
if (typeof indicator !== 'string') {
return indicator.indicatorKey === toComputeIndicatorKeys[i];
}
return false;
}) as IIndicator
)?.format,
calculateFun: calculatedFieldRule?.calculateFun,
dependAggregators: this.totalRecordsTree[flatRowKey][flatColKey],
dependIndicatorKeys: calculatedFieldRule?.dependIndicatorKeys
});
}
toComputeIndicatorKeys[i] in record && this.totalRecordsTree[flatRowKey]?.[flatColKey]?.[i].push(record);
} else {
const aggRule = this.getAggregatorRule(toComputeIndicatorKeys[i]);
if (!this.totalRecordsTree[flatRowKey]?.[flatColKey]?.[i]) {
this.totalRecordsTree[flatRowKey][flatColKey][i] = new registeredAggregators[
aggRule?.aggregationType ?? AggregationType.SUM
]({
// single: true,
key: toComputeIndicatorKeys[i],
field: aggRule?.field ?? toComputeIndicatorKeys[i],
aggregationFun: aggRule?.aggregationFun,
formatFun:
aggRule?.formatFun ??
(
this.indicators?.find((indicator: string | IIndicator) => {
if (typeof indicator !== 'string') {
return indicator.indicatorKey === toComputeIndicatorKeys[i];
}
return false;
}) as IIndicator
)?.format
});
}
//push融合了计算过程
toComputeIndicatorKeys[i] in record && this.totalRecordsTree[flatRowKey]?.[flatColKey]?.[i].push(record);
}
}
return;
}
//#endregion
// 此方法判断效率很低
// if (this.rowKeys.indexOf(rowKey) === -1) this.rowKeys.push(rowKey);
// if (this.colKeys.indexOf(colKey) === -1) this.colKeys.push(colKey);
// 这一段代码需要再考虑下 目前isToTalRecord中没有 this.rowKeys.push逻辑 。造成的一个问题例如有列小计的相关自定义汇总数据,但没有push到this.rowKeys中
// 但是放到上面目前isToTalRecord逻辑return之前的话 会引起新的问题。这个this.rowKeys的补充是否需要从this.totalRecordsTree中获取到? TODO(pivot-tree demo加上列小计就能复现)
if (rowKey.length !== 0) {
if (!this.rowFlatKeys[flatRowKey]) {
this.rowKeys.push(rowKey);
this.rowFlatKeys[flatRowKey] = 1;
}
}
if (colKey.length !== 0) {
if (!this.colFlatKeys[flatColKey]) {
this.colKeys.push(colKey);
this.colFlatKeys[flatColKey] = 1;
}
}
//组织树结构: 行-列-单元格 行key为flatRowKey如’山东青岛‘ 列key为flatColKey如’家具椅子‘
if (!this.tree[flatRowKey]) {
this.tree[flatRowKey] = {};
}
//这里改成数组 因为可能是多个指标值 遍历indicators 生成对应类型的聚合对象
if (!this.tree[flatRowKey]?.[flatColKey]) {
this.tree[flatRowKey][flatColKey] = [];
}
const toComputeIndicatorKeys = this.indicatorKeysIncludeCalculatedFieldDependIndicatorKeys;
for (let i = 0; i < toComputeIndicatorKeys.length; i++) {
if (this.calculatedFiledKeys.indexOf(toComputeIndicatorKeys[i]) >= 0) {
const calculatedFieldRule = this.calculatedFieldRules?.find(rule => rule.key === toComputeIndicatorKeys[i]);
if (!this.tree[flatRowKey]?.[flatColKey]?.[i]) {
this.tree[flatRowKey][flatColKey][i] = new registeredAggregators[AggregationType.RECALCULATE]({
key: toComputeIndicatorKeys[i],
field: toComputeIndicatorKeys[i],
isRecord: true,
formatFun: (
this.indicators?.find((indicator: string | IIndicator) => {
if (typeof indicator !== 'string') {
return indicator.indicatorKey === toComputeIndicatorKeys[i];
}
return false;
}) as IIndicator
)?.format,
calculateFun: calculatedFieldRule?.calculateFun,
dependAggregators: this.tree[flatRowKey][flatColKey],
dependIndicatorKeys: calculatedFieldRule?.dependIndicatorKeys
});
}
this.tree[flatRowKey]?.[flatColKey]?.[i].push(record);
} else {
const aggRule = this.getAggregatorRule(toComputeIndicatorKeys[i]);
let needAddToAggregator = false;
if (assignedIndicatorKey_value) {
if (assignedIndicatorKey === assignedIndicatorKey_value) {
// 参数传入的assignedIndicatorKey 表示records是指标已经分好组的 组里一定要加入指标聚合对象中
toComputeIndicatorKeys[i] === assignedIndicatorKey_value && (needAddToAggregator = true);
} else {
toComputeIndicatorKeys[i] === assignedIndicatorKey_value &&
toComputeIndicatorKeys[i] in record &&
(needAddToAggregator = true);
}
}
//加入聚合结果 考虑field为数组的情况
else if (aggRule?.field) {
if (typeof aggRule?.field === 'string') {
aggRule?.field in record && (needAddToAggregator = true);
} else {
const isPush = aggRule?.field.find((field: string) => {
return field in record;
});
isPush && (needAddToAggregator = true);
}
} else {
//push融合了计算过程
toComputeIndicatorKeys[i] in record && (needAddToAggregator = true);
}
if (!this.tree[flatRowKey]?.[flatColKey]?.[i] && needAddToAggregator) {
this.tree[flatRowKey][flatColKey][i] = new registeredAggregators[
aggRule?.aggregationType ?? AggregationType.SUM
]({
key: toComputeIndicatorKeys[i],
field: aggRule?.field ?? toComputeIndicatorKeys[i],
aggregationFun: aggRule?.aggregationFun,
formatFun:
aggRule?.formatFun ??
(
this.indicators?.find((indicator: string | IIndicator) => {
if (typeof indicator !== 'string') {
return indicator.indicatorKey === toComputeIndicatorKeys[i];
}
return false;
}) as IIndicator
)?.format
});
}
if (needAddToAggregator) {
this.tree[flatRowKey]?.[flatColKey]?.[i].push(record);
}
}
}
//统计整体的最大最小值和总计值 共mapping使用
if (this.mappingRules) {
for (let i = 0; i < this.indicatorKeys.length; i++) {
if (!this.indicatorStatistics[i]) {
const aggRule = this.getAggregatorRule(this.indicatorKeys[i]);
this.indicatorStatistics[i] = {
max: new registeredAggregators[AggregationType.MAX]({
key: this.indicatorKeys[i],
field: this.indicatorKeys[i]
}),
min: new registeredAggregators[AggregationType.MIN]({
key: this.indicatorKeys[i],
field: this.indicatorKeys[i]
}),
total: new registeredAggregators[aggRule?.aggregationType ?? AggregationType.SUM]({
key: this.indicatorKeys[i],
field: aggRule?.field ?? this.indicatorKeys[i],
aggregationFun: aggRule?.aggregationFun,
formatFun:
aggRule?.formatFun ??