配置
通过以下 API 配置 Pivot 表格和配置面板:
config:定义 Pivot 表格的结构及数据聚合方式render-table:动态更改表格配置tableShape:配置 Pivot 表格的外观columnShape:配置列的外观和行为headerShape:配置表头的外观和行为configPanel:控制配置面板的显示状态setLocale:应用语言环境(参见本地化)data、fields:加载数据和字段元数据predicates:在聚合前对数据进行预处理methods:定义自定义聚合方法limits:限制最终数据集中的行数和列数
有关数据操作的说明,请参见数据操作。
您可以配置以下 Pivot 表格元素:
- 列和行
- 表头和表脚
- 单元格
- 表格尺寸
调整表格尺寸
使用 tableShape 属性更改行、列、表头和表脚的尺寸。
以下代码片段展示了默认尺寸:
const sizes = {
rowHeight: 34,
headerHeight: 30,
footerHeight: 30,
columnWidth: 150
};
以下代码片段覆盖了默认尺寸:
const table = new pivot.Pivot("#root", {
fields,
data,
tableShape: {
sizes: {
rowHeight: 44,
headerHeight: 60,
footerHeight: 30,
columnWidth: 170
}
},
config: {
rows: ["studio", "genre"],
columns: [],
values: [
{
field: "title",
method: "count"
},
{
field: "score",
method: "max"
}
]
}
});
信息
要设置特定列的宽度,请使用 columnShape 属性的 width 参数。
自动调整列宽以适应内容
使用 columnShape 属性的 autoWidth 参数自动计算列宽。所有 autoWidth 子参数均为可选项。完整说明请参见 columnShape 参考文档。
autoWidth 对象接受以下参数:
columns:选择哪些字段启用自动计算宽度的对象auto:根据表头、单元格内容或两者来调整宽度maxRows:用于检测列尺寸所分析的数据行数(默认值:20)firstOnly:若为true(默认值),则每个字段仅分析一次。当多列基于同一字段时(例如,oil对应count和oil对应sum),仅分析第一列,其余列继承其宽度
以下代码片段为四个字段启用 autoWidth 并禁用 firstOnly,使每列独立进行宽度计算:
const table = new pivot.Pivot("#root", {
fields,
data,
config: {
rows: ["studio", "genre"],
columns: [],
values: [
{
field: "title",
method: "count"
},
{
field: "score",
method: "max"
}
]
},
columnShape: {
autoWidth: {
// 为这些字段计算列宽
columns: {
studio: true,
genre: true,
title: true,
score: true
},
// 分析所有字段
firstOnly: false
}
}
});