Skip to content

Commit b23408c

Browse files
committed
fix: aggregation error
1 parent e3816ab commit b23408c

2 files changed

Lines changed: 46 additions & 28 deletions

File tree

packages/vtable/__tests__/pivotChart.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9487,7 +9487,7 @@ describe('pivotChart init test', () => {
94879487
},
94889488
二级: {
94899489
max: 81585.58783792704,
9490-
min: -15135.001037597656,
9490+
min: -15135.001037597654,
94919491
positiveMax: 81585.58783792704,
94929492
negativeMin: -19659.724044799805
94939493
},
@@ -9598,7 +9598,7 @@ describe('pivotChart init test', () => {
95989598
},
95999599
'230713150305011': {
96009600
一级: {
9601-
max: 40780.32046222687,
9601+
max: 40780.32046222686,
96029602
min: -22801.40795326233,
96039603
positiveMax: 44028.34812831879,
96049604
negativeMin: -22801.40795326233

packages/vtable/src/ts-types/dataset/aggregation.ts

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,16 @@ export class SumAggregator extends Aggregator {
434434
deleteRecord(record: any) {
435435
if (record) {
436436
if (this.isRecord && this.records) {
437-
this.records = this.records.filter(item => item !== record);
437+
const index = this.records.indexOf(record);
438+
if (index !== -1) {
439+
this.records.splice(index, 1);
440+
}
438441
}
439442
if (record.isAggregator && this.children) {
440-
this.children = this.children.filter(item => item !== record);
443+
const index = this.children.indexOf(record);
444+
if (index !== -1) {
445+
this.children.splice(index, 1);
446+
}
441447
const value = record.value();
442448
this.sum = precisionSub(this.sum, value ?? 0);
443449
if (this.needSplitPositiveAndNegativeForSum) {
@@ -464,18 +470,18 @@ export class SumAggregator extends Aggregator {
464470
updateRecord(oldRecord: any, newRecord: any): void {
465471
if (oldRecord && newRecord) {
466472
if (this.isRecord && this.records) {
467-
this.records = this.records.map(item => {
468-
if (item === oldRecord) {
469-
return newRecord;
470-
}
471-
return item;
472-
});
473+
const index = this.records.indexOf(oldRecord);
474+
if (index !== -1) {
475+
this.records[index] = newRecord;
476+
}
473477
}
474478
if (oldRecord.isAggregator && this.children) {
475479
const oldValue = oldRecord.value();
476-
this.children = this.children.filter(item => item !== oldRecord);
480+
const index = this.children.indexOf(oldRecord);
481+
if (index !== -1) {
482+
this.children[index] = newRecord;
483+
}
477484
const newValue = newRecord.value();
478-
this.children.push(newRecord);
479485
this.sum = precisionAdd(this.sum, precisionSub(newValue, oldValue));
480486
if (this.needSplitPositiveAndNegativeForSum) {
481487
if (oldValue > 0) {
@@ -510,7 +516,10 @@ export class SumAggregator extends Aggregator {
510516
}
511517
}
512518
value() {
513-
return this.changedValue ?? (this.records?.length >= 1 ? this.sum : undefined);
519+
return (
520+
this.changedValue ??
521+
(this.records && this.records.length >= 1 ? this.sum : this.isRecord === false ? this.sum : undefined)
522+
);
514523
}
515524
positiveValue() {
516525
return this.positiveSum;
@@ -699,11 +708,17 @@ export class AvgAggregator extends Aggregator {
699708
deleteRecord(record: any) {
700709
if (record) {
701710
if (this.isRecord && this.records) {
702-
this.records = this.records.filter(item => item !== record);
711+
const index = this.records.indexOf(record);
712+
if (index !== -1) {
713+
this.records.splice(index, 1);
714+
}
703715
}
704716
if (record.isAggregator && record.type === AggregationType.AVG) {
705717
if (this.children) {
706-
this.children = this.children.filter(item => item !== record);
718+
const index = this.children.indexOf(record);
719+
if (index !== -1) {
720+
this.children.splice(index, 1);
721+
}
707722
}
708723
this.sum = precisionSub(this.sum, record.sum);
709724
this.count -= record.count;
@@ -717,21 +732,17 @@ export class AvgAggregator extends Aggregator {
717732
updateRecord(oldRecord: any, newRecord: any): void {
718733
if (oldRecord && newRecord) {
719734
if (this.isRecord && this.records) {
720-
this.records = this.records.map(item => {
721-
if (item === oldRecord) {
722-
return newRecord;
723-
}
724-
return item;
725-
});
735+
const index = this.records.indexOf(oldRecord);
736+
if (index !== -1) {
737+
this.records[index] = newRecord;
738+
}
726739
}
727740
if (oldRecord.isAggregator && oldRecord.type === AggregationType.AVG) {
728741
if (this.children && newRecord.isAggregator) {
729-
this.children = this.children.map(item => {
730-
if (item === oldRecord) {
731-
return newRecord;
732-
}
733-
return item;
734-
});
742+
const index = this.children.indexOf(oldRecord);
743+
if (index !== -1) {
744+
this.children[index] = newRecord;
745+
}
735746
}
736747
this.sum = precisionAdd(this.sum, precisionSub(newRecord.sum, oldRecord.sum));
737748
this.count += newRecord.count - oldRecord.count;
@@ -746,7 +757,14 @@ export class AvgAggregator extends Aggregator {
746757
}
747758
}
748759
value() {
749-
return this.changedValue ?? (this.records?.length >= 1 ? this.sum / this.count : undefined);
760+
return (
761+
this.changedValue ??
762+
(this.records && this.records.length >= 1
763+
? this.sum / this.count
764+
: this.isRecord === false && this.count > 0
765+
? this.sum / this.count
766+
: undefined)
767+
);
750768
}
751769
reset() {
752770
this.changedValue = undefined;

0 commit comments

Comments
 (0)