From ab0d0e768cf627249b38205b8c77d95a20b3a901 Mon Sep 17 00:00:00 2001 From: Jinke Li Date: Thu, 18 Apr 2024 15:04:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(layout):=20=E4=BF=AE=E5=A4=8D=E7=A9=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E6=83=85=E5=86=B5=E5=BC=80=E5=90=AF?= =?UTF-8?q?=E6=B1=87=E6=80=BB=E5=88=86=E7=BB=84=E6=B8=B2=E6=9F=93=E6=8A=A5?= =?UTF-8?q?=E9=94=99=20close=20#2661=20(#2662)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix(layout): 修复空数组的情况开启汇总分组渲染报错 close #2661 --- .../__tests__/spreadsheet/total-group-spec.ts | 36 ++++++++++++++++++- .../src/facet/layout/build-gird-hierarchy.ts | 8 +++-- packages/s2-core/src/facet/pivot-facet.ts | 11 +++--- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/packages/s2-core/__tests__/spreadsheet/total-group-spec.ts b/packages/s2-core/__tests__/spreadsheet/total-group-spec.ts index 1b16ebdc09..8ba7b9b2ca 100644 --- a/packages/s2-core/__tests__/spreadsheet/total-group-spec.ts +++ b/packages/s2-core/__tests__/spreadsheet/total-group-spec.ts @@ -13,7 +13,7 @@ describe('Total Group Dimension Test', () => { }); afterEach(() => { - s2?.destroy(); + // s2?.destroy(); }); test(`should get correct layout with row total group dimension 'type'`, () => { @@ -357,4 +357,38 @@ describe('Total Group Dimension Test', () => { [VALUE_FIELD]: 9999, }); }); + + // https://github.com/antvis/S2/issues/2661 + test.each([ + { totalsGroupDimensions: [] }, + { totalsGroupDimensions: ['city'] }, + ])( + 'should render correctly group totals layout if data is empty by %o', + (config) => { + s2 = new PivotSheet(container, dataCfg, { + ...config, + showGrandTotals: true, + showSubTotals: true, + reverseLayout: true, + reverseSubLayout: true, + }); + + s2.setDataCfg({ + fields: { + rows: ['type'], + columns: ['province', 'city'], + values: ['price', 'cost'], + }, + data: [], + }); + s2.render(); + + const { colNodes, colsHierarchy } = s2.facet.layoutResult; + + expect(colsHierarchy.height).toEqual(90); + expect(colNodes).toHaveLength(4); + expect(colNodes.find((node) => node.value === '总计')).toBeFalsy(); + expect(colNodes.find((node) => node.value === '小计')).toBeFalsy(); + }, + ); }); diff --git a/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts b/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts index ee90205031..eec9cbfbb5 100644 --- a/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts +++ b/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts @@ -26,9 +26,11 @@ const buildTotalGridHierarchy = (params: GridHeaderParams) => { let query: Record = {}; const totalsConfig = spreadsheet.getTotalsConfig(currentField); - const dimensionGroup = parentNode.isGrandTotals - ? totalsConfig.totalsGroupDimensions - : totalsConfig.subTotalsGroupDimensions; + const defaultDimensionGroup = parentNode.isGrandTotals + ? totalsConfig.totalsGroupDimensions || [] + : totalsConfig.subTotalsGroupDimensions || []; + const dimensionGroup = !dataSet.isEmpty() ? defaultDimensionGroup : []; + if (dimensionGroup?.includes(currentField)) { query = getDimsCondition(parentNode); const dimValues = dataSet.getDimensionValues(currentField, query); diff --git a/packages/s2-core/src/facet/pivot-facet.ts b/packages/s2-core/src/facet/pivot-facet.ts index d5de7f0919..4580829e72 100644 --- a/packages/s2-core/src/facet/pivot-facet.ts +++ b/packages/s2-core/src/facet/pivot-facet.ts @@ -675,10 +675,12 @@ export class PivotFacet extends FrozenFacet { const { rows, columns } = dataSet.fields; const fields = isRowHeader ? rows : columns; const totalConfig = isRowHeader ? totals.row : totals.col; - const dimensionGroup = isSubTotal + const defaultDimensionGroup = isSubTotal ? totalConfig.subTotalsGroupDimensions || [] : totalConfig.totalsGroupDimensions || []; + const dimensionGroup = !dataSet.isEmpty() ? defaultDimensionGroup : []; const multipleMap: number[] = Array.from({ length: maxLevel + 1 }, () => 1); + for (let level = maxLevel; level > 0; level--) { const currentField = fields[level] as string; // 若不符合【分组维度包含此维度】或【者指标维度下非单指标维度】,此表头单元格为空,将宽高合并到上级单元格 @@ -716,9 +718,10 @@ export class PivotFacet extends FrozenFacet { } let res = 0; for (let i = 0; i < multiple; i++) { - res += hierarchy.sampleNodesForAllLevels.find( - (sampleNode) => sampleNode.level === node.level + i, - )[key]; + res += + hierarchy.sampleNodesForAllLevels.find( + (sampleNode) => sampleNode.level === node.level + i, + )?.[key] || 0; } node[key] = res; });