logo

S2

  • Manual
  • API
  • Examples
  • Playground
  • ChangeLog
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quick Start
  • Basic Tutorial
    • Base Concept
    • Sheet Type
      • Pivot Mode
        Updated
      • Table Mode
    • Conditions
      Updated
    • Totals
    • Sort
      • Custom Sort
        Updated
      • Group Sort
        Updated
    • Theme
      Updated
    • Tooltip
      Updated
    • Internationalization
    • 数据格式化
      New
    • Multi Line
      New
    • Pagination
      New
  • Advanced Tutorial
    • Advanced Tutorial
      • Link
      • Render Chart In Cell
      • Draw Chart With @antv/g2
    • Custom
      • Customize Hook
        Updated
      • Customize Tree
        New
      • Customize Icon
      • Customize Cell Alignment
      • Customize Cell Size
        Updated
      • Customize Order
      • Custom Collapse/Expand Nodes
    • Interaction
      • Basic Interaction
      • Custom Interaction
      • Hide Columns
      • Cell Resize
      • Merge Cell
      • Scroll
      • Copy
        New
      • Highlight and select cell
        New
    • Analyze Component
      • Introduction
        New
      • Advanced Sort
        Updated
      • Switcher
      • Export
        Updated
      • Pagination
      • Drill Down
    • Sheet Component
      • Editable Mode
      • Strategy Sheet
        Updated
    • HD Adapter
    • Get Instance
    • Get Cell Data
      Updated
    • Table adaptive
    • AntV/G Plugins
      New
    • Pivot Chart
      Experimental
    • Vue 3.0
  • Extended Reading
    • Data Process
      • Pivot
      • Table
    • Layout
      • Pivot
      • Table
    • Performance
      Updated
  • Contribution
  • FAQ
  • S2 2.0 Migration Guide

Customize Cell Size

Previous
Customize Cell Alignment
Next
Customize Order

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

S2 can manually drag and drop to dynamically change the width and height of cells. At the same time, there are three built-in layouts: row and column行列等宽,列等宽and行列紧凑布局layout ( see examples )

We can modify the background color, font size and other configurations of the cell through the theme . If you want to customize the width and height of the cell, you can use the style configuration of s2Options to achieve it


const s2Options = {
style: {
// 行头单元格配置
rowCell: {},
// 列头单元格配置
colCell: {},
// 数值单元格配置
dataCell: {},
},
}

preview

Adjust value cell width and height

Priority is less than rowCell.height and colCell.width

const s2Options = {
style: {
dataCell: {
width: 100,
height: 90
},
},
}

The height of row header cell and the width of column header cell are always consistent with the value cell, so it is equivalent to adjusting the height of row header and the width of column header at this time.

preview

Adjust row header cell width and height

The height adjustment of row header cells is applied to leaf nodes (the height of non-leaf nodes is the sum of the heights of all child nodes), and the height is always consistent with the height of the value cell .

const s2Options = {
style: {
rowCell: {
width: 50,
height: 50
},
},
}

It can also be dynamically adjusted according to the current row header node information, returning null means using the default width and height

const s2Options = {
style: {
rowCell: {
width: (rowNode) => {
// 例:叶子节点 300px, 非叶子节点 200px
return rowNode.isLeaf ? 300 : 200;
},
height: (rowNode) => {
// 例:偶数行高度 300pox, 奇数行默认高度
return rowNode.level % 2 === 0 ? 300 : null,
}
},
},
}

preview

If you want to set different widths and heights for a specific row/column, you can use rowCell 's widthByField and heightByField preset heights to achieve it. Two types of configurations are supported:

  • fieldId (eg: root[&] 浙江省[&] 杭州市): the unique ID corresponding to each row head node after the row and column cross, applicable to specific cells whose width and height are accurate (how to get the ID)
  • field (example: city ): corresponds to the field configured in s2DataConfig.fields.rows , applicable to cells accurate to a certain type of dimension value

const s2Options = {
style: {
rowCell: {
widthByField: {
city: 100
},
heightByField: {
'root[&] 浙江省 [&] 杭州市': 60,
'root[&] 浙江省 [&] 宁波市': 100,
},
},
},
}

preview

The schedule is a bit special. Since there are only column headers, if you want to set a different height for a specific row, you can adjust it according to the row number

const s2Options = {
style: {
rowCell: {
// 给第一行和第三行设置不同的高度
heightByField: {
'1': 130,
'3': 60,
},
},
},
}

Adjust line header width in tree mode

No difference from tile mode configuration

const s2Options = {
hierarchyType: 'tree',
style: {
rowCell: {
width: 200,
}
},
}

Adjust column header cell width and height

The width adjustment of column header cells is applied to leaf nodes (the width of non-leaf nodes is the sum of the widths of all child nodes), and the width is always consistent with the width of the value cell

const s2Options = {
style: {
colCell: {
width: 200,
height: 60,
},
},
}

preview

If you want to set a different width and height for each column, you can dynamically adjust the width and height according to the current column header node information, return null to use the default width and height

const s2Options = {
style: {
colCell: {
width: (colNode) => {
// 例:前两列宽度 100px, 其他 50px
return colNode.colIndex <= 2 ? 100 : 50
},
height: (colNode) => {
// 例:前两列高度 100px, 其他 50px
return colNode.colIndex <= 2 ? 100 : 50
},
},
},
}

If you want to set different widths and heights for a specific column, you can use colCell 's widthByField and heightByField preset widths and heights to achieve it. Two types of configurations are supported:

  • fieldId (example: root[&] 家具[&] 沙发[&]number ): the unique ID corresponding to each column head node after the row and column cross, applicable to specific cells whose width and height are accurate (how to get the ID)
  • field (example: city ): corresponds to the field configured in s2DataConfig.fields.columns , applicable to cells accurate to a certain type of dimension value
import { EXTRA_FIELD } from '@antv/s2'
const s2Options = {
style: {
colCell: {
widthByField: {
// 默认 [数值挂列头], EXTRA_FIELD 为内部虚拟数值列
[EXTRA_FIELD]: 60,
'root[&] 家具 [&] 沙发 [&]number': 120,
},
heightByField: {
[EXTRA_FIELD]: 80,
},
},
},
}

preview

hide column header

You can also set the height to 0 to achieve the effect of hiding the column header , see an example

const s2Options = {
style: {
colCell: {
height: 0,
},
},
}

preview

API documentation

style

object is required , default: null Function description: style setting

parametertyperequiredDefaultsFunctional description
layoutWidthTypeadaptive | colAdaptive | compactCell Width Layout Type
adaptive : Rows and columns are equal in width, equally dividing the entire Canvas canvas width
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
compact : compact row and column layout, when the indicator dimension is small, it cannot cover the entire canvas
dataCellDataCellValue cell configuration
colCellColCellColumn header cell configuration
rowCellRowCellRow header cell configuration

DataCell

object is required , default: null Function description: Numerical cell configuration

parameterillustratetypeDefaultsrequired
widthcell widthnumber96-
heightcell heightnumber30-
valuesCfgcell configuration{ originalValueField?: string, widthPercent?: number[], showOriginalValue?: boolean }-

ColCell

object is required , default: null Function description: Column header cell configuration

parameterillustratetypeDefaultsrequired
widthCell width, which can be dynamically set according to the current column header node (leaf nodes are valid)number | (colNode: Node) => number96
heightCell height, which can be dynamically set according to the current column header node (leaf nodes are valid)number | (colNode: Node) => number30
widthByFieldSet the width according to the measurement value (drag or preset width scene), field corresponds to the field or column header id in s2DataConfig.fields.columns , see detailsRecord<string, number>-
heightByFieldSet the height according to the measurement value (drag or preset height scene), the field corresponds to the field or column header id in s2DataConfig.fields.columns , see detailsRecord<string, number>-
hideValueThe default value hangs the column header, which will display the column header and the value at the same time, and hide the value to make it more beautiful. (that is, s2DataConfig.fields.values and it is only valid for a single value, and it is recommended to use hidden column headers for multiple values)booleanfalse

RowCell

object is required , default: null Function description: row header cell configuration

parameterillustratetypeDefaultsrequired
widthThe row cell width can be dynamically set according to the current row header node, and the tree structure is also applicablenumber | (rowNode: Node) => numberTiles: 96 , Trees: 120
heightRow cell height, which can be dynamically set according to the current row header nodenumber | (rowNode: Node) => number30
collapseFieldsCustom folded nodes for row headers in tree mode.
Supports two formats: id ( 'root[&] 行头维度值' ) and dimension field ( 'city' ). The priority is higher than collapseAll and expandDepth , and the priority is the lowest when it is set to null . view demo
Record<string, boolean>
collapseAllWhether to collapse all line headers by default in tree structure mode.booleanfalse
expandDepthIn the tree structure mode, the row header expands the expanded level by default (the level starts from 0), and when it is set to null , the priority is the lowestnumber
showTreeLeafNodeAlignDotIn the tree mode, whether the row header leaf nodes display the hierarchical placeholdersbooleanfalse
withByFieldSet the width of each line according to field . field corresponds to the field or column header id in s2DataConfig.fields.rows , see detailsRecord<string, number>-
heightByFieldSet the height of each row according to field .
1. Pivot table: field corresponds to field or column header id in s2DataConfig.fields.rows .
2. Detailed table: field corresponds to the row number, starting from 1 . check the details
Record<string, number>-