@canvas-components/pivot-table 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 canvas-components contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # @canvas-components/pivot-table
2
+
3
+ 独立于 `list-table` 的纯原生数据透视表内核。包内不依赖 React,React 接入由 `@canvas-components/react-pivot-table` 提供。
4
+
5
+ ## 能力
6
+
7
+ - 双轴多级维度树与稳定节点 ID
8
+ - `sum`、`avg`、`count`、`distinctCount`、`min`、`max` 和自定义聚合器
9
+ - 稀疏聚合、小计、总计和多指标行列布局
10
+ - 四区 Canvas、虚拟滚动、展开收起、拖拽矩形选区和下钻
11
+ - 支持单元格、矩形选区与指标整列复制
12
+ - 维度成员排序、聚合前过滤和共享表头过滤面板
13
+ - CSV/XLSX 导出与版本化状态快照
14
+
15
+ ## React 示例
16
+
17
+ ```tsx
18
+ import { ReactPivotTable } from "@canvas-components/react-pivot-table";
19
+
20
+ <ReactPivotTable
21
+ dataSource={records}
22
+ rows={[{ dataIndex: "region", allowFilter: true }]}
23
+ columns={[{ key: "year", dataIndex: "year" }]}
24
+ indicators={[{ key: "sales", dataIndex: "sales", aggregator: "sum" }]}
25
+ />;
26
+ ```
27
+
28
+ `rows` 与 `columns` 的字段命名、`render` 回调及排序过滤配置与
29
+ `ListTableColumn` 保持一致,可以从 ListTable 列配置中直接选取维度列。
30
+ PivotTable 会在未提供 `key` 时使用 `dataIndex` 生成稳定维度标识;
31
+ `defaultExpandDepth` 是透视维度额外支持的展开配置。
32
+
33
+ 过滤发生在聚合前。`filters`、`sorts`、`selection` 和 `expandedRowKeys`/`expandedColumnKeys` 可受控;不传时由实例维护。
34
+
35
+ ## 原生实例 API
36
+
37
+ ```ts
38
+ import { PivotTable } from "@canvas-components/pivot-table";
39
+
40
+ const table = new PivotTable(container, options);
41
+ table.mount();
42
+
43
+ table.getPivotModel();
44
+ table.getVisibleRowNodes();
45
+ table.getVisibleColumnNodes();
46
+ table.selectCell(address);
47
+ table.selectNode("row", nodeId);
48
+ table.scrollToCell(address);
49
+ table.setDimensionFilter("row", "region", filter);
50
+ table.setDimensionSort("row", "region", "asc");
51
+ table.setIndicatorSort({
52
+ axis: "row",
53
+ dimensionKey: "region",
54
+ indicatorKey: "sales",
55
+ direction: "desc",
56
+ });
57
+ table.getCellRecords(cellContext);
58
+ table.exportCsv({ mode: "visible" });
59
+ table.exportXlsx({ mode: "all", sheetName: "销售透视" });
60
+ table.getState();
61
+ table.restoreState(snapshot);
62
+ table.getDebugSnapshot();
63
+
64
+ table.destroy();
65
+ ```
66
+
67
+ `ReactPivotTable` 的 ref 透传选择、筛选、排序、导出、状态快照和调试方法,不在 React 层复制聚合或布局逻辑。
68
+
69
+ ## 自定义聚合器
70
+
71
+ ```ts
72
+ const weightedAverage = {
73
+ create: () => ({ weightedValue: 0, weight: 0 }),
74
+ add: (state, value, context) => {
75
+ const weight = Number(context.record.weight ?? 0);
76
+ state.weightedValue += Number(value ?? 0) * weight;
77
+ state.weight += weight;
78
+ },
79
+ finalize: (state) =>
80
+ state.weight === 0 ? null : state.weightedValue / state.weight,
81
+ };
82
+
83
+ const indicators = [
84
+ {
85
+ key: "weightedPrice",
86
+ dataIndex: "price",
87
+ aggregator: weightedAverage,
88
+ },
89
+ ];
90
+ ```
91
+
92
+ 聚合器状态按维度组合隔离。`add` 不得复用其他单元格状态;需要大数据分块时可实现 `merge`。