@aura-react-comp/charts 0.2.0 → 0.4.0

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.
Files changed (98) hide show
  1. package/esm/charts/area-chart/index.d.ts +2 -2
  2. package/esm/charts/area-chart/index.js +28 -23
  3. package/esm/charts/bar-chart/index.d.ts +1 -1
  4. package/esm/charts/bar-chart/index.js +18 -21
  5. package/esm/charts/candlestick-chart/index.d.ts +2 -2
  6. package/esm/charts/candlestick-chart/index.js +243 -180
  7. package/esm/charts/combo-chart/index.d.ts +1 -1
  8. package/esm/charts/combo-chart/index.js +9 -20
  9. package/esm/charts/funnel-chart/index.d.ts +2 -2
  10. package/esm/charts/funnel-chart/index.js +134 -108
  11. package/esm/charts/gauge-chart/index.d.ts +1 -1
  12. package/esm/charts/gauge-chart/index.js +103 -105
  13. package/esm/charts/graph-chart/index.d.ts +3 -3
  14. package/esm/charts/graph-chart/index.js +155 -114
  15. package/esm/charts/heatmap-chart/index.d.ts +2 -2
  16. package/esm/charts/heatmap-chart/index.js +180 -135
  17. package/esm/charts/line-chart/index.d.ts +2 -2
  18. package/esm/charts/line-chart/index.js +29 -24
  19. package/esm/charts/pie-chart/index.d.ts +2 -2
  20. package/esm/charts/pie-chart/index.d.ts.map +1 -1
  21. package/esm/charts/pie-chart/index.js +199 -139
  22. package/esm/charts/radar-chart/index.d.ts +2 -2
  23. package/esm/charts/radar-chart/index.d.ts.map +1 -1
  24. package/esm/charts/radar-chart/index.js +155 -128
  25. package/esm/charts/ring-progress/index.d.ts +1 -1
  26. package/esm/charts/ring-progress/index.js +82 -82
  27. package/esm/charts/sankey-chart/index.d.ts +3 -3
  28. package/esm/charts/sankey-chart/index.js +172 -141
  29. package/esm/charts/scatter-chart/index.d.ts +3 -3
  30. package/esm/charts/scatter-chart/index.d.ts.map +1 -1
  31. package/esm/charts/scatter-chart/index.js +226 -159
  32. package/esm/charts/sparkline/index.d.ts +1 -1
  33. package/esm/charts/sparkline/index.js +82 -66
  34. package/esm/charts/tree-chart/index.d.ts +3 -3
  35. package/esm/charts/tree-chart/index.js +129 -102
  36. package/esm/charts/treemap-chart/index.d.ts +3 -3
  37. package/esm/charts/treemap-chart/index.js +163 -141
  38. package/esm/charts/waterfall-chart/index.d.ts +2 -2
  39. package/esm/charts/waterfall-chart/index.js +166 -121
  40. package/esm/components/Legend.d.ts +5 -4
  41. package/esm/components/Legend.d.ts.map +1 -1
  42. package/esm/components/Legend.js +80 -48
  43. package/esm/components/Tooltip.d.ts +2 -2
  44. package/esm/components/Tooltip.js +63 -71
  45. package/esm/components/chart/index.d.ts +5 -2
  46. package/esm/components/chart/index.d.ts.map +1 -1
  47. package/esm/components/chart/index.js +81 -102
  48. package/esm/components/chart.less +13 -0
  49. package/esm/core/animation.js +32 -43
  50. package/esm/core/export.d.ts +34 -0
  51. package/esm/core/export.d.ts.map +1 -0
  52. package/esm/core/export.js +390 -0
  53. package/esm/core/force.js +23 -45
  54. package/esm/core/format.js +49 -52
  55. package/esm/core/hierarchy.d.ts +1 -1
  56. package/esm/core/hierarchy.js +61 -92
  57. package/esm/core/layout.d.ts +1 -1
  58. package/esm/core/layout.js +33 -50
  59. package/esm/core/sampling.js +31 -53
  60. package/esm/core/sankey.js +36 -55
  61. package/esm/core/scale.js +73 -86
  62. package/esm/core/shape.js +102 -133
  63. package/esm/core/sync.d.ts +41 -0
  64. package/esm/core/sync.d.ts.map +1 -0
  65. package/esm/core/sync.js +120 -0
  66. package/esm/hooks/useChartExport.d.ts +19 -0
  67. package/esm/hooks/useChartExport.d.ts.map +1 -0
  68. package/esm/hooks/useChartExport.js +35 -0
  69. package/esm/hooks/useChartTheme.d.ts +6 -5
  70. package/esm/hooks/useChartTheme.d.ts.map +1 -1
  71. package/esm/hooks/useChartTheme.js +18 -18
  72. package/esm/hooks/useChartTransition.d.ts +1 -1
  73. package/esm/hooks/useChartTransition.js +59 -73
  74. package/esm/hooks/useElementSize.js +20 -28
  75. package/esm/hooks/usePlotPointer.d.ts +23 -0
  76. package/esm/hooks/usePlotPointer.d.ts.map +1 -0
  77. package/esm/hooks/usePlotPointer.js +94 -0
  78. package/esm/index.d.ts +56 -50
  79. package/esm/index.d.ts.map +1 -1
  80. package/esm/index.js +137 -47
  81. package/esm/internal/CartesianChart.d.ts +1 -1
  82. package/esm/internal/CartesianChart.d.ts.map +1 -1
  83. package/esm/internal/CartesianChart.js +667 -485
  84. package/esm/internal/model.d.ts +2 -2
  85. package/esm/internal/model.js +45 -60
  86. package/esm/scene/canvas.d.ts +1 -1
  87. package/esm/scene/canvas.js +101 -107
  88. package/esm/scene/svg.d.ts +1 -1
  89. package/esm/scene/svg.js +107 -99
  90. package/esm/scene/types.js +9 -15
  91. package/esm/style.css +10 -0
  92. package/esm/theme/index.d.ts +2 -2
  93. package/esm/theme/index.d.ts.map +1 -1
  94. package/esm/theme/index.js +145 -170
  95. package/esm/types.d.ts +7 -3
  96. package/esm/types.d.ts.map +1 -1
  97. package/esm/types.js +1 -1
  98. package/package.json +2 -2
@@ -1,31 +1,29 @@
1
- import React, { useMemo, useState } from 'react';
2
- import { classNames, prefixCls } from '@aura-react-comp/shared';
1
+ 'use client';
2
+ // src/charts/treemap-chart/index.tsx
3
+ import { useMemo, useState } from "react";
4
+ import { classNames, prefixCls } from "@aura-react-comp/shared";
3
5
  import { SvgScene } from "../../scene/svg";
4
6
  import { formatNumber, formatPercent } from "../../core/format";
5
7
  import { roundedRectPath } from "../../core/shape";
6
- import { buildTreemap, countLeaves, sumValues } from "../../core/hierarchy";
8
+ import {
9
+ buildTreemap,
10
+ countLeaves,
11
+ sumValues
12
+ } from "../../core/hierarchy";
7
13
  import { mixColor, withAlpha } from "../../theme";
8
14
  import { useChartTheme } from "../../hooks/useChartTheme";
9
15
  import { useElementSize } from "../../hooks/useElementSize";
10
16
  import { useChartTransition } from "../../hooks/useChartTransition";
11
17
  import { resolveAnimation, staggerProgress } from "../../core/animation";
12
18
  import { Tooltip } from "../../components/Tooltip";
13
- import { jsx as _jsx } from "react/jsx-runtime";
14
- import { jsxs as _jsxs } from "react/jsx-runtime";
15
- /**
16
- * 矩形树图。
17
- *
18
- * 用面积编码数值,能在有限空间里同时表达「占比」与「层级」。
19
- * 布局用 squarified 算法:让每块尽量接近正方形,
20
- * 既放得下文字,也便于人眼比较面积。
21
- */
22
- export function TreemapChart({
19
+ import { jsx, jsxs } from "react/jsx-runtime";
20
+ function TreemapChart({
23
21
  data,
24
22
  maxDepth = 1,
25
- label = 'auto',
23
+ label = "auto",
26
24
  showValue = true,
27
25
  gap = 2,
28
- width = '100%',
26
+ width = "100%",
29
27
  height = 320,
30
28
  theme: themeInput,
31
29
  palette,
@@ -34,141 +32,151 @@ export function TreemapChart({
34
32
  className,
35
33
  style,
36
34
  ariaLabel,
37
- emptyText = '暂无数据'
35
+ emptyText = "暂无数据"
38
36
  }) {
39
37
  const theme = useChartTheme(themeInput, palette);
40
- const {
41
- ref,
42
- size
43
- } = useElementSize();
38
+ const { ref, size } = useElementSize();
44
39
  const [activeKey, setActiveKey] = useState(null);
45
- const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
46
- const animConfig = useMemo(() => resolveAnimation(animation),
47
- // eslint-disable-next-line react-hooks/exhaustive-deps
48
- [animKey]);
40
+ const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
41
+ const animConfig = useMemo(
42
+ () => resolveAnimation(animation),
43
+ // eslint-disable-next-line react-hooks/exhaustive-deps
44
+ [animKey]
45
+ );
49
46
  const hierarchy = useMemo(() => {
50
- if (!data) return {
51
- cells: [],
52
- total: 0,
53
- leaves: 0
54
- };
47
+ if (!data)
48
+ return { cells: [], total: 0, leaves: 0 };
55
49
  const total = sumValues(data);
56
- if (total <= 0) return {
57
- cells: [],
58
- total: 0,
59
- leaves: countLeaves(data)
60
- };
61
- const cells = buildTreemap(data, {
62
- x: 0,
63
- y: 0,
64
- width: Math.max(0, size.width),
65
- height: Math.max(0, size.height)
66
- }, {
67
- maxDepth,
68
- headerHeight: 18,
69
- theme,
70
- colorOf: (depth, index, parentColor) => {
71
- if (depth === 0) return theme.palette[index % theme.palette.length];
72
- // 子层用父色向白混合,保持「同一父族颜色相近」的直觉
73
- return mixColor(parentColor ?? theme.palette[0], '#ffffff', 0.46);
50
+ if (total <= 0)
51
+ return { cells: [], total: 0, leaves: countLeaves(data) };
52
+ const cells = buildTreemap(
53
+ data,
54
+ {
55
+ x: 0,
56
+ y: 0,
57
+ width: Math.max(0, size.width),
58
+ height: Math.max(0, size.height)
59
+ },
60
+ {
61
+ maxDepth,
62
+ headerHeight: 18,
63
+ theme,
64
+ colorOf: (depth, index, parentColor) => {
65
+ if (depth === 0)
66
+ return theme.palette[index % theme.palette.length];
67
+ return mixColor(parentColor ?? theme.palette[0], "#ffffff", 0.46);
68
+ }
74
69
  }
75
- });
76
- return {
77
- cells,
78
- total,
79
- leaves: countLeaves(data)
80
- };
70
+ );
71
+ return { cells, total, leaves: countLeaves(data) };
81
72
  }, [data, maxDepth, size.width, size.height, theme]);
82
-
83
- // 用各格面积驱动动画:数据变化时格子平滑伸缩
84
- const matrix = useMemo(() => [hierarchy.cells.map(cell => Math.round(cell.width * cell.height))], [hierarchy.cells]);
73
+ const matrix = useMemo(
74
+ () => [hierarchy.cells.map((cell) => Math.round(cell.width * cell.height))],
75
+ [hierarchy.cells]
76
+ );
85
77
  const transition = useChartTransition(matrix, animConfig);
86
78
  const progress = transition.progress;
87
79
  const scene = useMemo(() => {
88
- if (!hierarchy.cells.length || size.width <= 0) return [];
80
+ if (!hierarchy.cells.length || size.width <= 0)
81
+ return [];
89
82
  const nodes = [];
90
83
  hierarchy.cells.forEach((cell, index) => {
91
- const local = staggerProgress(progress, index, hierarchy.cells.length, animConfig.stagger);
84
+ var _a;
85
+ const local = staggerProgress(
86
+ progress,
87
+ index,
88
+ hierarchy.cells.length,
89
+ animConfig.stagger
90
+ );
92
91
  const key = `${cell.depth}-${cell.node.name}-${index}`;
93
92
  const isActive = activeKey === key;
94
93
  nodes.push({
95
- kind: 'path',
94
+ kind: "path",
96
95
  key,
97
- d: roundedRectPath(cell.x + gap / 2, cell.y + gap / 2, Math.max(0, cell.width - gap), Math.max(0, cell.height - gap), 3),
96
+ d: roundedRectPath(
97
+ cell.x + gap / 2,
98
+ cell.y + gap / 2,
99
+ Math.max(0, cell.width - gap),
100
+ Math.max(0, cell.height - gap),
101
+ 3
102
+ ),
98
103
  fill: cell.color,
99
104
  // 子层淡一些,形成「父块包着子块」的视觉层次
100
105
  opacity: (cell.depth === 0 ? 1 : 0.92) * local,
101
- stroke: isActive ? theme.text.primary : undefined,
106
+ stroke: isActive ? theme.text.primary : void 0,
102
107
  strokeWidth: isActive ? 2 : 0,
103
- pointerEvents: 'none'
108
+ pointerEvents: "none"
104
109
  });
105
110
  const boxWidth = cell.width - gap;
106
111
  const boxHeight = cell.height - gap;
107
- const showLabel = label === 'always' || label === 'auto' && boxWidth >= 56 && boxHeight >= 22 || cell.depth === 0 && boxWidth >= 56 && boxHeight >= 22;
108
- if (!showLabel || label === false) return;
109
- const hasChildren = Boolean(cell.node.children?.length);
112
+ const showLabel = label === "always" || label === "auto" && boxWidth >= 56 && boxHeight >= 22 || cell.depth === 0 && boxWidth >= 56 && boxHeight >= 22;
113
+ if (!showLabel || label === false)
114
+ return;
115
+ const hasChildren = Boolean((_a = cell.node.children) == null ? void 0 : _a.length);
110
116
  const value = hasChildren ? sumValues(cell.node) : cell.node.value ?? 0;
111
117
  const showNumber = showValue && boxHeight >= 40 && boxWidth >= 56;
112
- // 有子块的格子把名称放在顶部标题条里;叶子居中
113
118
  const labelY = hasChildren ? cell.y + 11 : cell.y + (showNumber ? 16 : boxHeight / 2 + gap);
114
119
  nodes.push({
115
- kind: 'text',
120
+ kind: "text",
116
121
  key: `${key}-label`,
117
122
  x: cell.x + 8,
118
123
  y: labelY,
119
124
  text: cell.node.name,
120
- fill: '#fff',
125
+ fill: "#fff",
121
126
  fontSize: cell.depth === 0 ? theme.font.size : theme.font.sizeSmall,
122
- fontWeight: cell.depth === 0 ? theme.font.weightMedium : undefined,
123
- anchor: 'start',
124
- baseline: 'middle',
127
+ fontWeight: cell.depth === 0 ? theme.font.weightMedium : void 0,
128
+ anchor: "start",
129
+ baseline: "middle",
125
130
  opacity: local,
126
131
  fontFamily: theme.font.family
127
132
  });
128
-
129
- // 父块的汇总数值放到标题条右端,既不占额外高度又保留信息
130
133
  if (hasChildren && showValue && boxWidth >= 130 && boxHeight >= 30) {
131
134
  nodes.push({
132
- kind: 'text',
135
+ kind: "text",
133
136
  key: `${key}-total`,
134
137
  x: cell.x + boxWidth - 8,
135
138
  y: labelY,
136
- text: `${formatNumber(value, {
137
- compact: true
138
- })} · ${formatPercent(value, hierarchy.total, 1)}`,
139
- fill: withAlpha('#ffffff', 0.85),
139
+ text: `${formatNumber(value, { compact: true })} · ${formatPercent(value, hierarchy.total, 1)}`,
140
+ fill: withAlpha("#ffffff", 0.85),
140
141
  fontSize: theme.font.sizeSmall,
141
- anchor: 'end',
142
- baseline: 'middle',
142
+ anchor: "end",
143
+ baseline: "middle",
143
144
  opacity: local,
144
145
  fontFamily: theme.font.family
145
146
  });
146
147
  }
147
148
  if (showNumber && !hasChildren) {
148
149
  nodes.push({
149
- kind: 'text',
150
+ kind: "text",
150
151
  key: `${key}-value`,
151
152
  x: cell.x + 8,
152
153
  y: cell.y + 32,
153
- text: `${formatNumber(value, {
154
- compact: true
155
- })} · ${formatPercent(value, hierarchy.total, 1)}`,
156
- fill: withAlpha('#ffffff', 0.82),
154
+ text: `${formatNumber(value, { compact: true })} · ${formatPercent(value, hierarchy.total, 1)}`,
155
+ fill: withAlpha("#ffffff", 0.82),
157
156
  fontSize: theme.font.sizeSmall,
158
- anchor: 'start',
159
- baseline: 'middle',
157
+ anchor: "start",
158
+ baseline: "middle",
160
159
  opacity: local,
161
160
  fontFamily: theme.font.family
162
161
  });
163
162
  }
164
163
  });
165
164
  return nodes;
166
- }, [hierarchy, size.width, gap, theme, progress, animConfig.stagger, activeKey, label, showValue]);
167
-
168
- /** 命中检测:从后往前找第一个包含点的格子(深层画在上层) */
165
+ }, [
166
+ hierarchy,
167
+ size.width,
168
+ gap,
169
+ theme,
170
+ progress,
171
+ animConfig.stagger,
172
+ activeKey,
173
+ label,
174
+ showValue
175
+ ]);
169
176
  const cellFromPointer = (clientX, clientY) => {
170
177
  const element = ref.current;
171
- if (!element) return null;
178
+ if (!element)
179
+ return null;
172
180
  const rect = element.getBoundingClientRect();
173
181
  const x = clientX - rect.left;
174
182
  const y = clientY - rect.top;
@@ -181,58 +189,72 @@ export function TreemapChart({
181
189
  return null;
182
190
  };
183
191
  const activeCell = useMemo(() => {
184
- if (!activeKey) return null;
185
- return hierarchy.cells.find((cell, index) => `${cell.depth}-${cell.node.name}-${index}` === activeKey) ?? null;
192
+ if (!activeKey)
193
+ return null;
194
+ return hierarchy.cells.find(
195
+ (cell, index) => `${cell.depth}-${cell.node.name}-${index}` === activeKey
196
+ ) ?? null;
186
197
  }, [activeKey, hierarchy.cells]);
187
198
  const tooltipPayload = activeCell ? {
188
199
  label: activeCell.node.name,
189
200
  row: null,
190
- series: [{
191
- key: activeCell.node.name,
192
- name: '数值',
193
- color: activeCell.color,
194
- value: activeCell.node.value ?? sumValues(activeCell.node),
195
- formatted: `${formatNumber(activeCell.node.value ?? sumValues(activeCell.node), {
196
- compact: true
197
- })}(${formatPercent(activeCell.node.value ?? sumValues(activeCell.node), hierarchy.total, 1)})`
198
- }]
201
+ series: [
202
+ {
203
+ key: activeCell.node.name,
204
+ name: "数值",
205
+ color: activeCell.color,
206
+ value: activeCell.node.value ?? sumValues(activeCell.node),
207
+ formatted: `${formatNumber(activeCell.node.value ?? sumValues(activeCell.node), { compact: true })}(${formatPercent(activeCell.node.value ?? sumValues(activeCell.node), hierarchy.total, 1)})`
208
+ }
209
+ ]
199
210
  } : null;
200
- const autoAriaLabel = ariaLabel ?? `矩形树图:共 ${hierarchy.leaves} 项,总计 ${formatNumber(hierarchy.total, {
201
- compact: true
202
- })}`;
203
- return /*#__PURE__*/_jsx("div", {
204
- className: classNames(prefixCls('chart'), className),
205
- style: {
206
- width,
207
- height,
208
- ...style
209
- },
210
- "data-aura-chart": "treemap",
211
- children: /*#__PURE__*/_jsxs("div", {
212
- ref: ref,
213
- className: prefixCls('chart__plot'),
214
- onPointerMove: event => {
215
- const cell = cellFromPointer(event.clientX, event.clientY);
216
- setActiveKey(cell ? `${cell.depth}-${cell.node.name}-${hierarchy.cells.indexOf(cell)}` : null);
217
- },
218
- onPointerLeave: () => setActiveKey(null),
219
- children: [!hierarchy.cells.length || size.width === 0 ? /*#__PURE__*/_jsx("div", {
220
- className: prefixCls('chart__empty'),
221
- children: emptyText
222
- }) : /*#__PURE__*/_jsx(SvgScene, {
223
- nodes: scene,
224
- width: size.width,
225
- height: size.height,
226
- ariaLabel: autoAriaLabel
227
- }), /*#__PURE__*/_jsx(Tooltip, {
228
- payload: tooltipPayload,
229
- x: activeCell ? activeCell.x + activeCell.width / 2 : 0,
230
- y: activeCell ? activeCell.y + 8 : 0,
231
- containerWidth: size.width,
232
- containerHeight: size.height,
233
- theme: theme,
234
- config: tooltip
235
- })]
236
- })
237
- });
238
- }
211
+ const autoAriaLabel = ariaLabel ?? `矩形树图:共 ${hierarchy.leaves} 项,总计 ${formatNumber(hierarchy.total, { compact: true })}`;
212
+ return /* @__PURE__ */ jsx(
213
+ "div",
214
+ {
215
+ className: classNames(prefixCls("chart"), className),
216
+ style: { width, height, ...style },
217
+ "data-aura-chart": "treemap",
218
+ children: /* @__PURE__ */ jsxs(
219
+ "div",
220
+ {
221
+ ref,
222
+ className: prefixCls("chart__plot"),
223
+ onPointerMove: (event) => {
224
+ const cell = cellFromPointer(event.clientX, event.clientY);
225
+ setActiveKey(
226
+ cell ? `${cell.depth}-${cell.node.name}-${hierarchy.cells.indexOf(cell)}` : null
227
+ );
228
+ },
229
+ onPointerLeave: () => setActiveKey(null),
230
+ children: [
231
+ !hierarchy.cells.length || size.width === 0 ? /* @__PURE__ */ jsx("div", { className: prefixCls("chart__empty"), children: emptyText }) : /* @__PURE__ */ jsx(
232
+ SvgScene,
233
+ {
234
+ nodes: scene,
235
+ width: size.width,
236
+ height: size.height,
237
+ ariaLabel: autoAriaLabel
238
+ }
239
+ ),
240
+ /* @__PURE__ */ jsx(
241
+ Tooltip,
242
+ {
243
+ payload: tooltipPayload,
244
+ x: activeCell ? activeCell.x + activeCell.width / 2 : 0,
245
+ y: activeCell ? activeCell.y + 8 : 0,
246
+ containerWidth: size.width,
247
+ containerHeight: size.height,
248
+ theme,
249
+ config: tooltip
250
+ }
251
+ )
252
+ ]
253
+ }
254
+ )
255
+ }
256
+ );
257
+ }
258
+ export {
259
+ TreemapChart
260
+ };
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
- import { type ChartThemeInput } from '../../theme';
3
- import type { TooltipConfig } from '../../types';
2
+ import { type ChartThemeInput } from '../../theme/index.js';
3
+ import type { TooltipConfig } from '../../types.js';
4
4
  export interface WaterfallItem {
5
5
  name: string;
6
6
  /** 增量值:正为增加,负为减少 */