Skip to content

Commit c6cdec7

Browse files
CopilotAlexzjt
andauthored
feat(style): 支持紧凑模式下设置额外宽度和最小宽度 (#3309)
* Initial plan * feat(style): 支持紧凑模式下设置额外宽度和最小宽度 (#3305) Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> * docs: 增加相应的文档介绍 * test: 修复单测 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> Co-authored-by: zjt <alex_zjt@163.com>
1 parent e7a02bb commit c6cdec7

11 files changed

Lines changed: 329 additions & 8 deletions

File tree

‎packages/s2-core/__tests__/unit/facet/layout/col-node-width-spec.ts‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,60 @@ describe('Col width Test', () => {
105105

106106
expect(Math.round(s2.facet.getColLeafNodes()[0].width)).toBe(64);
107107
});
108+
109+
test('get correct width in layoutWidthType compact mode with compactExtraWidth', async () => {
110+
s2.setOptions({
111+
style: {
112+
layoutWidthType: LayoutWidthType.Compact,
113+
compactExtraWidth: 20,
114+
},
115+
});
116+
await s2.render();
117+
118+
// 基础宽度 80 + 额外宽度 20 = 100
119+
expect(Math.round(s2.facet.getColLeafNodes()[0].width)).toBe(100);
120+
});
121+
122+
test('get correct width in layoutWidthType compact mode with compactMinWidth', async () => {
123+
s2.setOptions({
124+
style: {
125+
layoutWidthType: LayoutWidthType.Compact,
126+
compactMinWidth: 150,
127+
},
128+
});
129+
await s2.render();
130+
131+
// 基础宽度 80 < 最小宽度 150,所以应用最小宽度
132+
expect(Math.round(s2.facet.getColLeafNodes()[0].width)).toBe(150);
133+
});
134+
135+
test('get correct width in layoutWidthType compact mode with both compactExtraWidth and compactMinWidth', async () => {
136+
s2.setOptions({
137+
style: {
138+
layoutWidthType: LayoutWidthType.Compact,
139+
compactExtraWidth: 10,
140+
compactMinWidth: 100,
141+
},
142+
});
143+
await s2.render();
144+
145+
// 基础宽度 80 + 额外宽度 10 = 90 < 最小宽度 100,所以应用最小宽度
146+
expect(Math.round(s2.facet.getColLeafNodes()[0].width)).toBe(100);
147+
});
148+
149+
test('get correct width in layoutWidthType compact mode with compactExtraWidth exceeding compactMinWidth', async () => {
150+
s2.setOptions({
151+
style: {
152+
layoutWidthType: LayoutWidthType.Compact,
153+
compactExtraWidth: 30,
154+
compactMinWidth: 100,
155+
},
156+
});
157+
await s2.render();
158+
159+
// 基础宽度 80 + 额外宽度 30 = 110 > 最小宽度 100,所以使用计算宽度
160+
expect(Math.round(s2.facet.getColLeafNodes()[0].width)).toBe(110);
161+
});
108162
});
109163

110164
describe('Table Mode', () => {
@@ -142,5 +196,39 @@ describe('Col width Test', () => {
142196
// cost 列,表身数据比列头更长(格式化)
143197
expect(Math.round(colLeafNodes[1].width)).toBe(168);
144198
});
199+
200+
test('get correct width in layoutWidthType compact mode with compactExtraWidth', async () => {
201+
s2.setOptions({
202+
style: {
203+
layoutWidthType: LayoutWidthType.Compact,
204+
compactExtraWidth: 20,
205+
},
206+
});
207+
await s2.render();
208+
209+
const colLeafNodes = s2.facet.getColLeafNodes();
210+
211+
// price 列: 基础宽度 47 + 额外宽度 20 = 67
212+
expect(Math.round(colLeafNodes[0].width)).toBe(67);
213+
// cost 列: 基础宽度 168 + 额外宽度 20 = 188
214+
expect(Math.round(colLeafNodes[1].width)).toBe(188);
215+
});
216+
217+
test('get correct width in layoutWidthType compact mode with compactMinWidth', async () => {
218+
s2.setOptions({
219+
style: {
220+
layoutWidthType: LayoutWidthType.Compact,
221+
compactMinWidth: 100,
222+
},
223+
});
224+
await s2.render();
225+
226+
const colLeafNodes = s2.facet.getColLeafNodes();
227+
228+
// price 列: 基础宽度 47 < 最小宽度 100,所以应用最小宽度
229+
expect(Math.round(colLeafNodes[0].width)).toBe(100);
230+
// cost 列: 基础宽度 168 > 最小宽度 100,保持原来宽度
231+
expect(Math.round(colLeafNodes[1].width)).toBe(168);
232+
});
145233
});
146234
});

‎packages/s2-core/__tests__/unit/facet/layout/row-node-width-spec.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,77 @@ describe('Row width Test in grid mode', () => {
4949

5050
expect(rowNodes[0].width).toBe(20);
5151
});
52+
53+
test('get the correct width of row nodes in compact mode with compactExtraWidth', async () => {
54+
// 1. 先获取不配置额外宽度时的基础宽度
55+
s2.setOptions({
56+
style: {
57+
layoutWidthType: LayoutWidthType.Compact,
58+
},
59+
});
60+
await s2.render();
61+
const rowNodesOriginal = s2.facet.getRowNodes();
62+
const baseWidth = Math.round(rowNodesOriginal[0].width);
63+
64+
// 2. 配置额外宽度
65+
s2.setOptions({
66+
style: {
67+
layoutWidthType: LayoutWidthType.Compact,
68+
compactExtraWidth: 20,
69+
},
70+
// 需要强制重新 render 以触发布局更新
71+
});
72+
// 确保 options 更新生效
73+
await s2.render();
74+
75+
const rowNodes = s2.facet.getRowNodes();
76+
77+
// 基础宽度 + 额外宽度
78+
expect(Math.round(rowNodes[0].width)).toBe(baseWidth + 20);
79+
});
80+
81+
test('get the correct width of row nodes in compact mode with compactMinWidth', async () => {
82+
s2.setOptions({
83+
style: {
84+
layoutWidthType: LayoutWidthType.Compact,
85+
compactMinWidth: 100,
86+
},
87+
});
88+
await s2.render();
89+
90+
const rowNodes = s2.facet.getRowNodes();
91+
92+
// 基础宽度 < 最小宽度 100,所以应用最小宽度
93+
expect(Math.round(rowNodes[0].width)).toBe(100);
94+
});
95+
96+
test('get the correct width of row nodes in compact mode with both compactExtraWidth and compactMinWidth', async () => {
97+
// 1. 获取基础宽度
98+
s2.setOptions({
99+
style: {
100+
layoutWidthType: LayoutWidthType.Compact,
101+
},
102+
});
103+
await s2.render();
104+
const rowNodesOriginal = s2.facet.getRowNodes();
105+
const baseWidth = Math.round(rowNodesOriginal[0].width);
106+
107+
// 2. 设置额外宽度和最小宽度
108+
s2.setOptions({
109+
style: {
110+
layoutWidthType: LayoutWidthType.Compact,
111+
compactExtraWidth: 10,
112+
compactMinWidth: 80,
113+
},
114+
});
115+
await s2.render();
116+
117+
const rowNodes = s2.facet.getRowNodes();
118+
119+
// 基础宽度 + 额外宽度 < 最小宽度 80,所以应用最小宽度
120+
// 47 + 10 = 57 < 80
121+
expect(Math.round(rowNodes[0].width)).toBe(80);
122+
// 验证确实小于 (防御性检查)
123+
expect(baseWidth + 10).toBeLessThan(80);
124+
});
52125
});

‎packages/s2-core/src/common/interface/style.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ export interface S2Style {
130130
*/
131131
layoutWidthType?: `${LayoutWidthType}`;
132132

133+
/**
134+
* 紧凑模式下的额外宽度,用于在紧凑布局计算的宽度基础上增加额外的宽度
135+
* @description 仅在 layoutWidthType 为 'compact' 时生效
136+
*/
137+
compactExtraWidth?: number;
138+
139+
/**
140+
* 紧凑模式下的最小单元格宽度,用于保证单元格不会过窄
141+
* @description 仅在 layoutWidthType 为 'compact' 时生效
142+
*/
143+
compactMinWidth?: number;
144+
133145
/**
134146
* 数值单元格配置
135147
*/

‎packages/s2-core/src/extends/pivot-chart/facet/pivot-chart-facet.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,21 @@ export class PivotChartFacet extends PivotFacet {
127127
}
128128

129129
protected override getCompactGridColNodeWidth(colNode: Node) {
130-
const { dataCell } = this.spreadsheet.options.style!;
130+
const {
131+
dataCell,
132+
compactExtraWidth = 0,
133+
compactMinWidth,
134+
} = this.spreadsheet.options.style!;
131135
const dataCellWidth = getCellWidth(
132136
dataCell!,
133137
this.getColLeafNodeRelatedCount(colNode),
134138
);
135139

136-
return round(dataCellWidth);
140+
const calculatedWidth = round(dataCellWidth + compactExtraWidth);
141+
142+
return isNumber(compactMinWidth)
143+
? Math.max(calculatedWidth, compactMinWidth)
144+
: calculatedWidth;
137145
}
138146

139147
protected override getAdaptGridColWidth(

‎packages/s2-core/src/facet/pivot-facet.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,16 @@ export class PivotFacet extends FrozenFacet {
897897
rowNodeWidth > fieldNameNodeWidth ? maxLabel : fieldName,
898898
);
899899

900-
return round(Math.max(rowNodeWidth, fieldNameNodeWidth));
900+
const { compactExtraWidth = 0, compactMinWidth } =
901+
this.spreadsheet.options.style || {};
902+
903+
const calculatedWidth = round(
904+
Math.max(rowNodeWidth, fieldNameNodeWidth) + compactExtraWidth,
905+
);
906+
907+
return isNumber(compactMinWidth)
908+
? Math.max(calculatedWidth, compactMinWidth)
909+
: calculatedWidth;
901910
}
902911

903912
protected getCompactGridColNodeWidth(colNode: Node, rowLeafNodes: Node[]) {
@@ -1001,13 +1010,21 @@ export class PivotFacet extends FrozenFacet {
10011010
fontSize: Math.max(dataCellTextStyle.fontSize, colCellTextStyle.fontSize),
10021011
});
10031012

1004-
return round(
1013+
const { compactExtraWidth = 0, compactMinWidth } =
1014+
this.spreadsheet.options.style || {};
1015+
1016+
const calculatedWidth = round(
10051017
maxTextWidth +
10061018
colCellStyle!.padding!.left! +
10071019
colCellStyle!.padding!.right! +
10081020
colCellStyle!.verticalBorderWidth! * 2 +
1009-
appendedWidth,
1021+
appendedWidth +
1022+
compactExtraWidth,
10101023
);
1024+
1025+
return isNumber(compactMinWidth)
1026+
? Math.max(calculatedWidth, compactMinWidth)
1027+
: calculatedWidth;
10111028
}
10121029

10131030
public getViewCellHeights() {

‎packages/s2-core/src/facet/table-facet.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,12 +536,12 @@ export class TableFacet extends FrozenFacet {
536536
const { theme, dataSet } = this.spreadsheet;
537537
const { bolderText: colCellTextStyle } = theme.colCell!;
538538
const { text: dataCellTextStyle, cell: cellStyle } = theme.dataCell!;
539-
const data = dataSet.getDisplayDataSet();
539+
const displayData = dataSet.getDisplayDataSet();
540540
const formatter = dataSet.getFieldFormatter(colNode.field);
541541

542542
// 采样前 50,找出表身最长的数据
543543
const maxLabel = maxBy(
544-
data
544+
displayData
545545
?.slice(0, LAYOUT_SAMPLE_COUNT)
546546
.map(
547547
(data) =>
@@ -566,7 +566,16 @@ export class TableFacet extends FrozenFacet {
566566
this.measureTextWidth(colNode.value, colCellTextStyle) +
567567
getOccupiedWidthForTableCol(this.spreadsheet, colNode, theme.colCell!);
568568

569-
return round(Math.max(colHeaderNodeWidth, maxLabelWidth));
569+
const { compactExtraWidth = 0, compactMinWidth } =
570+
this.spreadsheet.options.style || {};
571+
572+
const calculatedWidth = round(
573+
Math.max(colHeaderNodeWidth, maxLabelWidth) + compactExtraWidth,
574+
);
575+
576+
return isNumber(compactMinWidth)
577+
? Math.max(calculatedWidth, compactMinWidth)
578+
: calculatedWidth;
570579
}
571580

572581
private getColLeafNodesWidth(

‎s2-site/common/style.en.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ object is **required** , *default: null* Function description: style setting
1010
| parameter | type | required | Defaults | Functional description |
1111
| --------------- | ---------------------------------------- | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
1212
| layoutWidthType | `adaptive` \| `colAdaptive` \| `compact` | | | Cell Width Layout Type<br>`adaptive` : Rows and columns are equal in width, equally dividing the entire `Canvas` canvas width<br>`colAdaptive` : Equal width of columns, compact layout of line headers, the remaining width of the column equally divided canvas width minus the width of line headers<br>`compact` : compact row and column layout, when the indicator dimension is small, it cannot cover the entire canvas |
13+
| compactExtraWidth | `number` | | 0 | Extra width for compact mode, used to add extra width based on the calculated width in compact layout |
14+
| compactMinWidth | `number` | | 0 | Minimum cell width for compact mode, used to ensure cells are not too narrow |
1315
| dataCell | [DataCell](#dataCell) | | | Value cell configuration |
1416
| colCell | [ColCell](#colCell) | | | Column header cell configuration |
1517
| rowCell | [RowCell](#rowCell) | | | Row header cell configuration |

‎s2-site/common/style.zh.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ order: 3
1010
| 参数 | 类型 | 必选 | 默认值 | 功能描述 |
1111
| --- | --- | --- | --- | --- |
1212
| layoutWidthType | `adaptive \| colAdaptive \| compact` | | | 单元格宽度布局类型<br/> `adaptive` : 行列等宽,均分整个 `Canvas` 画布宽度 <br> `colAdaptive`:列等宽,行头紧凑布局,列等分画布宽度减去行头宽度的剩余宽度 <br/> `compact`:行列紧凑布局,指标维度少的时候无法布满整个画布,列头宽度为实际内容宽度(取当前列最大值,采样每一列前 50 条数据)|
13+
| compactExtraWidth | `number` | | 0 | 紧凑模式下的额外宽度,用于在紧凑布局计算的宽度基础上增加额外的宽度 |
14+
| compactMinWidth | `number` | | 0 | 紧凑模式下的最小单元格宽度,用于保证单元格不会过窄 |
1315
| dataCell | [DataCell](#datacell) | | | 数值单元格配置 |
1416
| rowCell | [RowCell](#rowcell) | | | 行头单元格配置 |
1517
| colCell | [ColCell](#colcell) | | | 列头单元格配置 |

‎s2-site/docs/manual/advanced/custom/cell-size.zh.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ const s2Options = {
2929

3030
<img src="https://gw.alipayobjects.com/zos/antfincdn/%24peMHWxZX/d20c9c80-d8d0-4fae-8d88-d31fe83c8072.png" alt="preview" width="600"/>
3131

32+
## 调整紧凑布局宽高
33+
34+
`layoutWidthType: 'compact'` (紧凑模式) 下,表格宽度是根据内容自适应的。如果内容过短,可能会导致单元格因为太窄而产生的视觉效果不佳,或者太贴合内容导致没有余量。
35+
36+
此时可以通过 `compactExtraWidth` (额外宽度) 和 `compactMinWidth` (最小宽度) 来进行调整。
37+
38+
```ts
39+
const s2Options = {
40+
style: {
41+
layoutWidthType: 'compact',
42+
// 给紧凑模式下计算出的宽度额外增加 12px
43+
compactExtraWidth: 12,
44+
// 保证紧凑模式下单元格宽度至少为 60px
45+
compactMinWidth: 60,
46+
},
47+
}
48+
```
49+
50+
<Playground path='layout/basic/demo/compact-width.tsx' rid='basic-compact' height='400'></Playground>
51+
3252
## 调整数值单元格宽高
3353

3454
:::warning{title="注意"}

0 commit comments

Comments
 (0)