@canvas-components/react-pivot-table 0.1.9 → 0.2.1

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/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # @canvas-components/react-pivot-table
2
+
3
+ `@canvas-components/react-pivot-table` 是 `@canvas-components/pivot-table` 的 React 适配层,在保留原生 Canvas 数据透视表性能的同时,提供标题栏、字段配置面板、图表联动和 React ref 接口。
4
+
5
+ ## 示例效果
6
+
7
+ ![React Pivot Table 示例](./assets/react-pivot-table-demo.gif)
8
+
9
+ ## 功能作用
10
+
11
+ - 提供 `ReactPivotTable` 组件
12
+ - 支持行、列、指标的多层聚合与虚拟滚动
13
+ - 支持字段面板配置和透视图表联动
14
+ - 暴露选择、展开、筛选、排序、导出和状态快照方法
15
+ - 聚合、布局和 Canvas 渲染复用纯原生 `pivot-table` 内核
16
+
17
+ ## 安装方法
18
+
19
+ ```bash
20
+ pnpm add @canvas-components/react-pivot-table @canvas-components/pivot-table react@^16.8 react-dom@^16.8
21
+ ```
22
+
23
+ 最低依赖版本:
24
+
25
+ - `react >= 16.8.0`
26
+ - `react-dom >= 16.8.0`
27
+ - `Node.js >= 18.12.0`
28
+ - `pnpm >= 9.0.0`
29
+
30
+ ## 使用方法
31
+
32
+ ```tsx
33
+ import { ReactPivotTable } from "@canvas-components/react-pivot-table";
34
+
35
+ interface SalesRecord {
36
+ region: string;
37
+ city: string;
38
+ year: number;
39
+ sales: number;
40
+ }
41
+
42
+ export function SalesPivotTable(props: { data: SalesRecord[] }) {
43
+ return (
44
+ <div style={{ width: "100%", height: 560 }}>
45
+ <ReactPivotTable<SalesRecord>
46
+ title="销售数据透视表"
47
+ dataSource={props.data}
48
+ rows={[
49
+ { key: "region", title: "区域", dataIndex: "region" },
50
+ { key: "city", title: "城市", dataIndex: "city" },
51
+ ]}
52
+ columns={[{ key: "year", title: "年份", dataIndex: "year" }]}
53
+ indicators={[
54
+ {
55
+ key: "sales",
56
+ title: "销售额",
57
+ dataIndex: "sales",
58
+ aggregator: "sum",
59
+ },
60
+ ]}
61
+ totals={{
62
+ showRowSubtotals: true,
63
+ showColumnGrandTotal: true,
64
+ showRowGrandTotal: true,
65
+ }}
66
+ />
67
+ </div>
68
+ );
69
+ }
70
+ ```
71
+
72
+ ## 属性说明
73
+
74
+ ### `ReactPivotTableProps`
75
+
76
+ `ReactPivotTableProps` 继承自 `PivotTableOptions`,并增加 React 宿主相关属性。
77
+
78
+ | 属性 | 类型 | 说明 |
79
+ | --------------- | ------------------------------------------------ | ---------------------------- |
80
+ | `className` | `string` | 根节点类名 |
81
+ | `style` | `CSSProperties` | 根节点样式 |
82
+ | `title` | `ReactNode` | 标题栏内容 |
83
+ | `customization` | `boolean \| ReactPivotTableCustomizationOptions` | 是否启用字段配置面板及其配置 |
84
+
85
+ 其余 `dataSource`、`rows`、`columns`、`indicators`、`totals`、`filters`、`sorts` 和虚拟滚动等属性与 `@canvas-components/pivot-table` 保持一致。
86
+
87
+ ### `ReactPivotTableCustomizationOptions`
88
+
89
+ | 属性 | 类型 | 说明 |
90
+ | ---------------- | ------------------ | ------------------------ |
91
+ | `fields` | `PivotField[]` | 可参与透视配置的全部字段 |
92
+ | `open` | `boolean` | 受控字段面板开关 |
93
+ | `defaultOpen` | `boolean` | 非受控字段面板初始状态 |
94
+ | `layout` | `PivotFieldLayout` | 受控字段区域布局 |
95
+ | `defaultLayout` | `PivotFieldLayout` | 非受控字段区域初始布局 |
96
+ | `panelWidth` | `number` | 字段面板宽度 |
97
+ | `onOpenChange` | `(open) => void` | 面板开关变化回调 |
98
+ | `onLayoutChange` | `(layout) => void` | 字段区域布局变化回调 |
99
+ | `onFieldChange` | `(field) => void` | 字段配置变化回调 |
100
+
101
+ ## 实例方法
102
+
103
+ 通过 `ReactPivotTableRef` 可以访问以下能力:
104
+
105
+ - 获取透视模型、可见行列节点和单元格明细记录
106
+ - 读取或设置选择状态,并滚动到指定单元格
107
+ - 展开、收起行列节点或全部节点
108
+ - 设置维度筛选、维度排序和指标排序
109
+ - 导出 CSV、XLSX
110
+ - 获取和恢复版本化状态快照
111
+ - 获取调试快照
112
+
113
+ ## 推荐场景
114
+
115
+ - React 数据分析后台
116
+ - 大数据量多维聚合报表
117
+ - 需要字段拖放配置或图表联动的数据透视场景
118
+ - 需要原生 Canvas 性能和 React 组件用法的场景
package/dist/index.cjs CHANGED
@@ -954,13 +954,21 @@ function buildPivotChartOption(params) {
954
954
  show: true,
955
955
  left: 12,
956
956
  right: 14,
957
- top: 14,
957
+ top: 40,
958
958
  bottom: 18,
959
959
  containLabel: true,
960
960
  backgroundColor: "#ffffff",
961
961
  borderColor: "#e6e9ed",
962
962
  borderWidth: 1
963
963
  },
964
+ legend: {
965
+ show: true,
966
+ orient: "horizontal",
967
+ left: "center",
968
+ top: "top",
969
+ itemGap: 12,
970
+ textStyle: { color: "#646a73", fontSize: 11 }
971
+ },
964
972
  xAxis: horizontal ? valueAxis : categoryAxis,
965
973
  yAxis: horizontal ? categoryAxis : valueAxis,
966
974
  tooltip: {
@@ -1594,7 +1602,12 @@ function toSharedValue(value) {
1594
1602
  secondConditionValue: stringifyConditionValue(value.secondCondition?.value),
1595
1603
  secondConditionSecondValue: stringifyConditionValue(
1596
1604
  value.secondCondition?.secondValue
1597
- )
1605
+ ),
1606
+ conditions: value.conditions?.map((condition) => ({
1607
+ operator: toSharedOperator(condition.operator),
1608
+ value: stringifyConditionValue(condition.value),
1609
+ secondValue: stringifyConditionValue(condition.secondValue)
1610
+ }))
1598
1611
  };
1599
1612
  }
1600
1613
  function toPivotFilter(value) {
@@ -1610,6 +1623,13 @@ function toPivotFilter(value) {
1610
1623
  value.secondConditionValue,
1611
1624
  value.secondConditionSecondValue
1612
1625
  ),
1626
+ conditions: value.conditions?.map(
1627
+ (condition) => toPivotCondition(
1628
+ condition.operator,
1629
+ condition.value,
1630
+ condition.secondValue
1631
+ )
1632
+ ),
1613
1633
  conditionRelation: value.conditionRelation
1614
1634
  };
1615
1635
  }
@@ -1693,7 +1713,10 @@ var defaultFilterTexts = {
1693
1713
  relationAndLabel: "\u4E14",
1694
1714
  relationOrLabel: "\u6216",
1695
1715
  resetAction: "\u91CD\u7F6E",
1696
- applyAction: "\u786E\u5B9A"
1716
+ applyAction: "\u786E\u5B9A",
1717
+ conditionLabel: "\u6761\u4EF6",
1718
+ addConditionAction: "\u65B0\u589E\u6761\u4EF6",
1719
+ deleteConditionAction: "\u5220\u9664\u6761\u4EF6"
1697
1720
  };
1698
1721
  function inferFieldAggregator(records, dataIndex) {
1699
1722
  const value = records.map((record) => pivotTable.getValueByDataIndex(record, dataIndex)).find((item) => item != null);