Skip to content

Commit

Permalink
fix(layout): 修复无列头时行头对应的角头不显示 close #1929 (#2026)
Browse files Browse the repository at this point in the history
* fix(layout): 修复无列头时行头对应的角头不显示 close #1929

* test: 修复测试
  • Loading branch information
lijinke666 authored Jan 4, 2023
1 parent 1089288 commit c073578
Show file tree
Hide file tree
Showing 14 changed files with 508 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PivotSheet Corner Tests should render row corner when columns and values is empty 1`] = `
Array [
Object {
"belongsCell": undefined,
"children": Array [],
"colIndex": -1,
"cornerType": "row",
"extra": undefined,
"field": "province",
"height": 30,
"id": "",
"inCollapseNode": undefined,
"isCollapsed": undefined,
"isGrandTotals": undefined,
"isLeaf": undefined,
"isPivotMode": true,
"isSubTotals": undefined,
"isTotalMeasure": undefined,
"isTotals": undefined,
"key": "province",
"label": "province",
"level": undefined,
"padding": 0,
"query": undefined,
"rowIndex": undefined,
"seriesNumberWidth": undefined,
"value": "province",
"width": 100,
"x": 0,
"y": 0,
},
Object {
"belongsCell": undefined,
"children": Array [],
"colIndex": -1,
"cornerType": "row",
"extra": undefined,
"field": "city",
"height": 30,
"id": "",
"inCollapseNode": undefined,
"isCollapsed": undefined,
"isGrandTotals": undefined,
"isLeaf": undefined,
"isPivotMode": true,
"isSubTotals": undefined,
"isTotalMeasure": undefined,
"isTotals": undefined,
"key": "city",
"label": "city",
"level": undefined,
"padding": 0,
"query": undefined,
"rowIndex": undefined,
"seriesNumberWidth": undefined,
"value": "city",
"width": 100,
"x": 100,
"y": 0,
},
]
`;

exports[`PivotSheet Corner Tests should render row corner when columns is empty and measure hidden 1`] = `
Array [
Object {
"belongsCell": undefined,
"children": Array [],
"colIndex": -1,
"cornerType": "row",
"extra": undefined,
"field": "province",
"height": 30,
"id": "",
"inCollapseNode": undefined,
"isCollapsed": undefined,
"isGrandTotals": undefined,
"isLeaf": undefined,
"isPivotMode": true,
"isSubTotals": undefined,
"isTotalMeasure": undefined,
"isTotals": undefined,
"key": "province",
"label": "province",
"level": undefined,
"padding": 0,
"query": undefined,
"rowIndex": undefined,
"seriesNumberWidth": undefined,
"value": "province",
"width": 150,
"x": 0,
"y": 0,
},
Object {
"belongsCell": undefined,
"children": Array [],
"colIndex": -1,
"cornerType": "row",
"extra": undefined,
"field": "city",
"height": 30,
"id": "",
"inCollapseNode": undefined,
"isCollapsed": undefined,
"isGrandTotals": undefined,
"isLeaf": undefined,
"isPivotMode": true,
"isSubTotals": undefined,
"isTotalMeasure": undefined,
"isTotals": undefined,
"key": "city",
"label": "city",
"level": undefined,
"padding": 0,
"query": undefined,
"rowIndex": undefined,
"seriesNumberWidth": undefined,
"value": "city",
"width": 150,
"x": 150,
"y": 0,
},
]
`;

exports[`PivotSheet Corner Tests should render row corner when measure hidden 1`] = `
Array [
Object {
"belongsCell": undefined,
"children": Array [],
"colIndex": -1,
"cornerType": "row",
"extra": undefined,
"field": "province",
"height": 30,
"id": "",
"inCollapseNode": undefined,
"isCollapsed": undefined,
"isGrandTotals": undefined,
"isLeaf": undefined,
"isPivotMode": true,
"isSubTotals": undefined,
"isTotalMeasure": undefined,
"isTotals": undefined,
"key": "province",
"label": "province",
"level": undefined,
"padding": 0,
"query": undefined,
"rowIndex": undefined,
"seriesNumberWidth": undefined,
"value": "province",
"width": 100,
"x": 0,
"y": 0,
},
Object {
"belongsCell": undefined,
"children": Array [],
"colIndex": -1,
"cornerType": "row",
"extra": undefined,
"field": "city",
"height": 30,
"id": "",
"inCollapseNode": undefined,
"isCollapsed": undefined,
"isGrandTotals": undefined,
"isLeaf": undefined,
"isPivotMode": true,
"isSubTotals": undefined,
"isTotalMeasure": undefined,
"isTotals": undefined,
"key": "city",
"label": "city",
"level": undefined,
"padding": 0,
"query": undefined,
"rowIndex": undefined,
"seriesNumberWidth": undefined,
"value": "city",
"width": 100,
"x": 100,
"y": 0,
},
]
`;
114 changes: 114 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/corner-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as simpleDataConfig from 'tests/data/simple-data.json';
import { DEFAULT_STYLE, type SpreadSheet } from '../../src';
import { createPivotSheet } from '../util/helpers';

describe('PivotSheet Corner Tests', () => {
let s2: SpreadSheet;

beforeEach(() => {
s2 = createPivotSheet({
width: 300,
height: 300,
});
s2.render();
});

afterEach(() => {
s2.destroy();
});

// https://github.com/antvis/S2/issues/1929
test('should render row corner when columns is empty and measure hidden', () => {
s2.setDataCfg({
...simpleDataConfig,
fields: {
...simpleDataConfig.fields,
columns: [],
},
});
s2.setOptions({
style: {
colCfg: {
hideMeasureColumn: true,
},
},
});
s2.render();

const cornerNodes = s2.facet.getCornerNodes();
const { colsHierarchy } = s2.facet.layoutResult;

expect(colsHierarchy.width).toEqual(0);
expect(colsHierarchy.height).toEqual(DEFAULT_STYLE.colCfg.height);
expect(colsHierarchy.sampleNodeForLastLevel.y).toEqual(0);
expect(colsHierarchy.sampleNodeForLastLevel.height).toEqual(
DEFAULT_STYLE.colCfg.height,
);
expect(cornerNodes).toHaveLength(2);
expect(s2.facet.getCornerNodes()).toMatchSnapshot();
});

test('should render row corner when measure hidden', () => {
s2.setOptions({
style: {
colCfg: {
hideMeasureColumn: true,
},
},
});
s2.render();

const cornerNodes = s2.facet.getCornerNodes();
const { colsHierarchy } = s2.facet.layoutResult;

expect(colsHierarchy.width).toEqual(100);
expect(colsHierarchy.height).toEqual(DEFAULT_STYLE.colCfg.height);
expect(colsHierarchy.sampleNodeForLastLevel.y).toEqual(0);
expect(colsHierarchy.sampleNodeForLastLevel.height).toEqual(
DEFAULT_STYLE.colCfg.height,
);
expect(cornerNodes).toHaveLength(2);
expect(s2.facet.getCornerNodes()).toMatchSnapshot();
});

test('should render row corner when columns and values is empty', () => {
s2.setDataCfg({
...simpleDataConfig,
fields: {
...simpleDataConfig.fields,
columns: [],
values: [],
},
});
s2.render();

const cornerNodes = s2.facet.getCornerNodes();
const { colsHierarchy } = s2.facet.layoutResult;

expect(colsHierarchy.width).toEqual(0);
expect(colsHierarchy.height).toEqual(0);
expect(colsHierarchy.sampleNodeForLastLevel).toBeNull();
expect(cornerNodes).toHaveLength(2);
expect(cornerNodes).toMatchSnapshot();
});

test('should not render row corner when fields is empty', () => {
s2.setDataCfg({
...simpleDataConfig,
fields: {
rows: [],
columns: [],
values: [],
},
});
s2.render();

const cornerNodes = s2.facet.getCornerNodes();
const { colsHierarchy } = s2.facet.layoutResult;

expect(colsHierarchy.width).toEqual(0);
expect(colsHierarchy.height).toEqual(0);
expect(colsHierarchy.sampleNodeForLastLevel).toBeNull();
expect(cornerNodes).toHaveLength(0);
});
});
20 changes: 18 additions & 2 deletions packages/s2-core/__tests__/unit/facet/bbox/cornerBBox-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { BaseFacet } from '@/facet/base-facet';
import { CornerBBox } from '@/facet/bbox/cornerBBox';

describe('cornerBBox test', () => {
let mockFacet;
let mockFacet: BaseFacet;

beforeEach(() => {
mockFacet = {
layoutResult: {
Expand All @@ -13,6 +14,7 @@ describe('cornerBBox test', () => {
colsHierarchy: {
height: 100,
width: 100,
sampleNodeForLastLevel: true,
},
},
getSeriesNumberWidth() {
Expand All @@ -24,9 +26,14 @@ describe('cornerBBox test', () => {
},
options: {
width: 400,
style: {
colCfg: {
hideMeasureColumn: false,
},
},
},
},
} as BaseFacet;
} as unknown as BaseFacet;
});

test('should return original width when scroll contains row header', () => {
Expand Down Expand Up @@ -68,4 +75,13 @@ describe('cornerBBox test', () => {
expect(bbox.width).toEqual(200);
expect(bbox.originalWidth).toEqual(280);
});

test('should use default column height when columns is empty', () => {
mockFacet.spreadsheet.options.style.colCfg.height = 20;
mockFacet.layoutResult.colsHierarchy.sampleNodeForLastLevel = null;

const bbox = new CornerBBox(mockFacet, true);

expect(bbox.height).toEqual(20);
});
});
4 changes: 4 additions & 0 deletions packages/s2-core/src/facet/base-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,4 +1342,8 @@ export abstract class BaseFacet {
detail.hideColumnNodes.some((node) => node.id === columnNode.id),
);
}

public getCornerNodes(): Node[] {
return this.cornerHeader?.getNodes() || [];
}
}
18 changes: 16 additions & 2 deletions packages/s2-core/src/facet/bbox/cornerBBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ export class CornerBBox extends BaseBBox {
this.maxY = height;
}

private getCornerBBoxHeight() {
private getCornerBBoxOriginalHeight() {
const { colsHierarchy } = this.layoutResult;
this.originalHeight = Math.floor(colsHierarchy.height);
const { colCfg } = this.spreadsheet.options.style;

/**
* 角头分为两部分, 一部分对应行头, 一部分对应列头, 角头的叶子节点对应行头节点, 以下极端情况让没有列头的时候也能 [行头对应的角头] 正常显示
* 1. 只配置了 rows, 而 columns, values 都为空, 此时列头为空
* 2. 配置了 rows, values, 此时存在一级列头 (即 EXTRA_FIELD 数值节点), 但是隐藏了数值 (hideMeasureColumn), 此时列头为空
*/
if (!colsHierarchy.sampleNodeForLastLevel) {
return colCfg?.height;
}
return Math.floor(colsHierarchy.height);
}

private getCornerBBoxHeight() {
this.originalHeight = this.getCornerBBoxOriginalHeight();

return this.originalHeight;
}
Expand Down
Loading

1 comment on commit c073578

@vercel
Copy link

@vercel vercel bot commented on c073578 Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

antvis-s2 – ./

antvis-s2.vercel.app
antvis-s2-git-master-antv-s2.vercel.app
antvis-s2-antv-s2.vercel.app

Please sign in to comment.