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