From 4d0dfc9169405b67ce1783b10fa3493391e00478 Mon Sep 17 00:00:00 2001 From: Wenjun Xu <906626481@qq.com> Date: Mon, 25 Mar 2024 13:53:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=8E=BB=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=9A=84=20TotalsStatus=EF=BC=8C=E5=8F=AA=E4=BF=9D?= =?UTF-8?q?=E7=95=99=20TotalStatus=20(#2623)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data-set/pivot-data-set-total-spec.ts | 36 +++++++++---------- .../unit/utils/data-set-operate-spec.tsx | 24 ++++++------- .../s2-core/src/common/interface/basic.ts | 7 ---- packages/s2-core/src/data-set/interface.ts | 4 +-- .../s2-core/src/data-set/pivot-data-set.ts | 8 ++--- packages/s2-core/src/facet/pivot-facet.ts | 3 ++ .../s2-core/src/utils/data-set-operate.ts | 12 ++++--- .../src/utils/dataset/pivot-data-set.ts | 4 +-- 8 files changed, 48 insertions(+), 50 deletions(-) diff --git a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts index cd8f4a4f03..d1236fe44d 100644 --- a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts +++ b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts @@ -484,9 +484,9 @@ describe('Pivot Dataset Total Test', () => { test('should get correct total cell data when totals calculated by calcFunc and Existential dimension grouping', () => { const totalStatus = { - isRowTotal: true, - isColTotal: true, + isRowGrandTotal: true, isRowSubTotal: true, + isColGrandTotal: true, isColSubTotal: true, }; @@ -713,53 +713,53 @@ describe('Pivot Dataset Total Test', () => { test('should get correct total status', () => { const { - isRowTotal: isRowTotal1, + isRowGrandTotal: isRowGrandTotal1, isRowSubTotal: isRowSubTotal1, - isColTotal: isColTotal1, + isColGrandTotal: isColGrandTotal1, isColSubTotal: isColSubTotal1, } = dataSet.getTotalStatus({ [EXTRA_FIELD]: 'number', }); - expect(isRowTotal1).toBeTrue(); + expect(isRowGrandTotal1).toBeTrue(); expect(isRowSubTotal1).toBeFalse(); - expect(isColTotal1).toBeTrue(); + expect(isColGrandTotal1).toBeTrue(); expect(isColSubTotal1).toBeFalse(); const { - isRowTotal: isRowTotal2, + isRowGrandTotal: isRowGrandTotal2, isRowSubTotal: isRowSubTotal2, - isColTotal: isColTotal2, + isColGrandTotal: isColGrandTotal2, isColSubTotal: isColSubTotal2, } = dataSet.getTotalStatus({ type: '家具', [EXTRA_FIELD]: 'number', }); - expect(isRowTotal2).toBeTrue(); + expect(isRowGrandTotal2).toBeTrue(); expect(isRowSubTotal2).toBeFalse(); - expect(isColTotal2).toBeFalse(); + expect(isColGrandTotal2).toBeFalse(); expect(isColSubTotal2).toBeTrue(); const { - isRowTotal: isRowTotal3, + isRowGrandTotal: isRowGrandTotal3, isRowSubTotal: isRowSubTotal3, - isColTotal: isColTotal3, + isColGrandTotal: isColGrandTotal3, isColSubTotal: isColSubTotal3, } = dataSet.getTotalStatus({ province: '浙江', [EXTRA_FIELD]: 'number', }); - expect(isRowTotal3).toBeFalse(); + expect(isRowGrandTotal3).toBeFalse(); expect(isRowSubTotal3).toBeTrue(); - expect(isColTotal3).toBeTrue(); + expect(isColGrandTotal3).toBeTrue(); expect(isColSubTotal3).toBeFalse(); const { - isRowTotal: isRowTotal4, + isRowGrandTotal: isRowGrandTotal4, isRowSubTotal: isRowSubTotal4, - isColTotal: isColTotal4, + isColGrandTotal: isColGrandTotal4, isColSubTotal: isColSubTotal4, } = dataSet.getTotalStatus({ province: '浙江', @@ -767,9 +767,9 @@ describe('Pivot Dataset Total Test', () => { [EXTRA_FIELD]: 'number', }); - expect(isRowTotal4).toBeFalse(); + expect(isRowGrandTotal4).toBeFalse(); expect(isRowSubTotal4).toBeTrue(); - expect(isColTotal4).toBeFalse(); + expect(isColGrandTotal4).toBeFalse(); expect(isColSubTotal4).toBeTrue(); }); }); diff --git a/packages/s2-core/__tests__/unit/utils/data-set-operate-spec.tsx b/packages/s2-core/__tests__/unit/utils/data-set-operate-spec.tsx index 70efb9d846..527a10ed55 100644 --- a/packages/s2-core/__tests__/unit/utils/data-set-operate-spec.tsx +++ b/packages/s2-core/__tests__/unit/utils/data-set-operate-spec.tsx @@ -90,9 +90,9 @@ describe('Data Set Operate Test', () => { expect( getAggregationAndCalcFuncByQuery( { - isRowTotal: true, + isRowGrandTotal: true, isRowSubTotal: false, - isColTotal: false, + isColGrandTotal: false, isColSubTotal: false, }, totalsOptions, @@ -102,9 +102,9 @@ describe('Data Set Operate Test', () => { expect( getAggregationAndCalcFuncByQuery( { - isRowTotal: true, + isRowGrandTotal: true, isRowSubTotal: false, - isColTotal: true, + isColGrandTotal: true, isColSubTotal: false, }, totalsOptions, @@ -116,9 +116,9 @@ describe('Data Set Operate Test', () => { expect( getAggregationAndCalcFuncByQuery( { - isRowTotal: true, + isRowGrandTotal: true, isRowSubTotal: false, - isColTotal: false, + isColGrandTotal: false, isColSubTotal: true, }, totalsOptions, @@ -133,9 +133,9 @@ describe('Data Set Operate Test', () => { expect( getAggregationAndCalcFuncByQuery( { - isRowTotal: false, + isRowGrandTotal: false, isRowSubTotal: true, - isColTotal: false, + isColGrandTotal: false, isColSubTotal: false, }, totalsOptions, @@ -150,9 +150,9 @@ describe('Data Set Operate Test', () => { expect( getAggregationAndCalcFuncByQuery( { - isRowTotal: false, + isRowGrandTotal: false, isRowSubTotal: true, - isColTotal: true, + isColGrandTotal: true, isColSubTotal: false, }, totalsOptions, @@ -167,9 +167,9 @@ describe('Data Set Operate Test', () => { expect( getAggregationAndCalcFuncByQuery( { - isRowTotal: false, + isRowGrandTotal: false, isRowSubTotal: true, - isColTotal: false, + isColGrandTotal: false, isColSubTotal: true, }, totalsOptions, diff --git a/packages/s2-core/src/common/interface/basic.ts b/packages/s2-core/src/common/interface/basic.ts index 390db21b39..c20606702e 100644 --- a/packages/s2-core/src/common/interface/basic.ts +++ b/packages/s2-core/src/common/interface/basic.ts @@ -115,13 +115,6 @@ export interface Fields extends BaseFields { customValueOrder?: number; } -export interface TotalsStatus { - isRowTotal: boolean; - isRowSubTotal: boolean; - isColTotal: boolean; - isColSubTotal: boolean; -} - export enum Aggregation { SUM = 'SUM', MIN = 'MIN', diff --git a/packages/s2-core/src/data-set/interface.ts b/packages/s2-core/src/data-set/interface.ts index 02655d1db5..30c1719c9f 100644 --- a/packages/s2-core/src/data-set/interface.ts +++ b/packages/s2-core/src/data-set/interface.ts @@ -83,9 +83,9 @@ export interface CheckAccordQueryParams { } export interface TotalStatus { - isRowTotal: boolean; + isRowGrandTotal: boolean; isRowSubTotal: boolean; - isColTotal: boolean; + isColGrandTotal: boolean; isColSubTotal: boolean; } diff --git a/packages/s2-core/src/data-set/pivot-data-set.ts b/packages/s2-core/src/data-set/pivot-data-set.ts index 721f64f720..5b0f1e934d 100644 --- a/packages/s2-core/src/data-set/pivot-data-set.ts +++ b/packages/s2-core/src/data-set/pivot-data-set.ts @@ -398,7 +398,7 @@ export class PivotDataSet extends BaseDataSet { const calcAction = calcActionByType[aggregation!]; // 前端计算汇总值 - if (calcAction || !!calcFunc) { + if (calcAction || calcFunc) { const data = this.getCellMultiData({ query, queryType: QueryDataType.DetailOnly, @@ -467,7 +467,7 @@ export class PivotDataSet extends BaseDataSet { } } - public getTotalStatus = (query: Query) => { + public getTotalStatus = (query: Query): TotalStatus => { const { columns, rows } = this.fields; const isTotals = (dimensions: string[], isSubTotal?: boolean) => { if (isSubTotal) { @@ -480,9 +480,9 @@ export class PivotDataSet extends BaseDataSet { }; return { - isRowTotal: isTotals(filterExtraDimension(rows as string[])), + isRowGrandTotal: isTotals(filterExtraDimension(rows as string[])), isRowSubTotal: isTotals(rows as string[], true), - isColTotal: isTotals(filterExtraDimension(columns as string[])), + isColGrandTotal: isTotals(filterExtraDimension(columns as string[])), isColSubTotal: isTotals(columns as string[], true), }; }; diff --git a/packages/s2-core/src/facet/pivot-facet.ts b/packages/s2-core/src/facet/pivot-facet.ts index 77265f815a..034b1a63fe 100644 --- a/packages/s2-core/src/facet/pivot-facet.ts +++ b/packages/s2-core/src/facet/pivot-facet.ts @@ -40,6 +40,7 @@ import { getIndexRangeWithOffsets } from '../utils/facet'; import { getRowsForGrid } from '../utils/grid'; import { floor } from '../utils/math'; import { getCellWidth } from '../utils/text'; +import { getHeaderTotalStatus } from '../utils/dataset/pivot-data-set'; import { FrozenFacet } from './frozen-facet'; import { Frame } from './header'; import { buildHeaderHierarchy } from './layout/build-header-hierarchy'; @@ -112,6 +113,7 @@ export class PivotFacet extends FrozenFacet { const isTotals = row.isTotals || row.isTotalMeasure || col.isTotals || col.isTotalMeasure; + const totalStatus = getHeaderTotalStatus(row, col); const hideMeasure = options.style?.colCell?.hideValue ?? false; /* @@ -129,6 +131,7 @@ export class PivotFacet extends FrozenFacet { query: dataQuery, rowNode: row, isTotals, + totalStatus, }); const valueField = dataQuery[EXTRA_FIELD]!; diff --git a/packages/s2-core/src/utils/data-set-operate.ts b/packages/s2-core/src/utils/data-set-operate.ts index 753dbe2f4a..ac7369f447 100644 --- a/packages/s2-core/src/utils/data-set-operate.ts +++ b/packages/s2-core/src/utils/data-set-operate.ts @@ -3,7 +3,8 @@ import { EMPTY_EXTRA_FIELD_PLACEHOLDER, TOTAL_VALUE, } from '../common/constant/field'; -import type { CalcTotals, Totals, TotalsStatus } from '../common/interface'; +import type { CalcTotals, Totals } from '../common/interface'; +import type { TotalStatus } from '../data-set'; export const getListBySorted = ( list: string[], @@ -59,10 +60,11 @@ export const sortByItems = (arr1: string[], arr2: string[]) => { }; export function getAggregationAndCalcFuncByQuery( - totalsStatus: TotalsStatus, + totalsStatus: TotalStatus, totalsOptions?: Totals | null, ) { - const { isRowTotal, isRowSubTotal, isColTotal, isColSubTotal } = totalsStatus; + const { isRowGrandTotal, isRowSubTotal, isColGrandTotal, isColSubTotal } = + totalsStatus; const { row, col } = totalsOptions || {}; const { calcGrandTotals: rowCalcTotals = {}, @@ -84,9 +86,9 @@ export function getAggregationAndCalcFuncByQuery( // 优先级: 列总计/小计 > 行总计/小计 return ( - getCalcTotals(colCalcTotals, isColTotal) || + getCalcTotals(colCalcTotals, isColGrandTotal) || getCalcTotals(colCalcSubTotals, isColSubTotal) || - getCalcTotals(rowCalcTotals, isRowTotal) || + getCalcTotals(rowCalcTotals, isRowGrandTotal) || getCalcTotals(rowCalcSubTotals, isRowSubTotal) ); } diff --git a/packages/s2-core/src/utils/dataset/pivot-data-set.ts b/packages/s2-core/src/utils/dataset/pivot-data-set.ts index 356431fb3e..5e37e6bc0d 100644 --- a/packages/s2-core/src/utils/dataset/pivot-data-set.ts +++ b/packages/s2-core/src/utils/dataset/pivot-data-set.ts @@ -442,9 +442,9 @@ export function generateExtraFieldMeta( export function getHeaderTotalStatus(row: Node, col: Node): TotalStatus { return { - isRowTotal: row.isGrandTotals!, + isRowGrandTotal: row.isGrandTotals!, isRowSubTotal: row.isSubTotals!, - isColTotal: col.isGrandTotals!, + isColGrandTotal: col.isGrandTotals!, isColSubTotal: col.isSubTotals!, }; }