From c8d2c94c21527ae52ece3485524d374d09b1cba7 Mon Sep 17 00:00:00 2001 From: NoobNot <56724970+NoobNotN@users.noreply.github.com> Date: Wed, 11 Oct 2023 11:15:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(layout):=20=E4=BF=AE=E5=A4=8D=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E7=BB=93=E7=82=B9=E6=97=B6=E5=AF=B9=E7=88=B6=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=9A=84=E5=B8=83=E5=B1=80=E8=AE=A1=E7=AE=97=E9=94=99?= =?UTF-8?q?=E8=AF=AF=20close=20#2355=20(#2360)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(layout): 修复隐藏结点时对父节点的布局计算错误 close #2355 * chore: 增加废弃标记 --------- Co-authored-by: JuZe --- .../spreadsheet/hidden-columns-spec.ts | 28 +++++++++++++++++++ packages/s2-core/src/facet/layout/node.ts | 3 ++ packages/s2-core/src/facet/pivot-facet.ts | 4 +-- .../src/utils/layout/generate-header-nodes.ts | 1 + 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/s2-core/__tests__/spreadsheet/hidden-columns-spec.ts b/packages/s2-core/__tests__/spreadsheet/hidden-columns-spec.ts index b6c4a11dbd..7aecda2f1d 100644 --- a/packages/s2-core/__tests__/spreadsheet/hidden-columns-spec.ts +++ b/packages/s2-core/__tests__/spreadsheet/hidden-columns-spec.ts @@ -347,6 +347,34 @@ describe('SpreadSheet Hidden Columns Tests', () => { expect(hiddenColumnsInfo).toBeTruthy(); expect(parentNode.hiddenChildNodeInfo).toEqual(hiddenColumnsInfo); }); + // https://github.com/antvis/S2/issues/2355 + test('should render correctly x and width after hide columns when there is only one value for the higher-level dimension.', () => { + const nodeId = 'root[&]笔[&]义乌[&]price'; + + pivotSheet.setOptions({ + style: { + colCfg: { + width: 100, + }, + }, + }); + const data = pivotSheet.dataCfg.data.map((i) => ({ ...i, cost: 0 })); + pivotSheet.setDataCfg({ + data, + fields: { + values: ['cost', 'price'], + }, + }); + pivotSheet.render(); + + pivotSheet.interaction.hideColumns([nodeId]); + const rootNode = pivotSheet + .getColumnNodes() + .find((node) => node.id === 'root[&]笔'); + + expect(rootNode.width).toEqual(300); + expect(rootNode.x).toEqual(0); + }); // https://github.com/antvis/S2/issues/2194 test('should render correctly when always hidden last column', () => { diff --git a/packages/s2-core/src/facet/layout/node.ts b/packages/s2-core/src/facet/layout/node.ts index cfc09d5466..e9e7ee0c0f 100644 --- a/packages/s2-core/src/facet/layout/node.ts +++ b/packages/s2-core/src/facet/layout/node.ts @@ -306,6 +306,9 @@ export class Node { public isTotalRoot?: boolean; + /** + * @deprecated 已废弃, 该属性只记录相邻一级的隐藏信息,将会在未来版本中移除 + */ public hiddenChildNodeInfo?: HiddenColumnsInfo | null; public extra?: Record; diff --git a/packages/s2-core/src/facet/pivot-facet.ts b/packages/s2-core/src/facet/pivot-facet.ts index 3b432f44b5..c5da151310 100644 --- a/packages/s2-core/src/facet/pivot-facet.ts +++ b/packages/s2-core/src/facet/pivot-facet.ts @@ -252,9 +252,9 @@ export class PivotFacet extends BaseFacet { leafNodes.push(parentNode); const firstVisibleChildNode = parentNode.children?.find( - (childNode) => !childNode.hiddenChildNodeInfo, + (childNode) => childNode.width, ); - // 父节点 x 坐标 = 第一个未隐藏的子节点的 x 坐标 + // 父节点 x 坐标 = 第一个正常布局处理过的子节点 x 坐标(width 有值认为是正常布局过) const parentNodeX = firstVisibleChildNode?.x; // 父节点宽度 = 所有子节点宽度之和 const parentNodeWidth = sumBy(parentNode.children, 'width'); diff --git a/packages/s2-core/src/utils/layout/generate-header-nodes.ts b/packages/s2-core/src/utils/layout/generate-header-nodes.ts index 402eb59120..aa33a7bf42 100644 --- a/packages/s2-core/src/utils/layout/generate-header-nodes.ts +++ b/packages/s2-core/src/utils/layout/generate-header-nodes.ts @@ -113,6 +113,7 @@ export const generateHeaderNodes = (params: HeaderNodesParams) => { // 如果当前是隐藏节点, 给其父节点挂载相应信息 (兄弟节点, 当前哪个子节点隐藏了), 这样在 facet 层可以直接使用, 不用每次都去遍历 const hiddenColumnsInfo = spreadsheet?.facet?.getHiddenColumnsInfo(node); if (hiddenColumnsInfo && parentNode) { + // hiddenChildNodeInfo 属性在 S2 中没有用到,但是没删怕外部有使用,已标记为废弃 parentNode.hiddenChildNodeInfo = hiddenColumnsInfo; }