@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,27 @@
1
1
  'use client';
2
- import React, { useCallback, useMemo, useState } from 'react';
3
- import { classNames, prefixCls } from '@aura-react-comp/shared';
2
+ // src/charts/graph-chart/index.tsx
3
+ import { useCallback, useMemo, useState } from "react";
4
+ import { classNames, prefixCls } from "@aura-react-comp/shared";
4
5
  import { SvgScene } from "../../scene/svg";
5
6
  import { formatNumber, estimateTextWidth } from "../../core/format";
6
7
  import { withAlpha } from "../../theme";
7
- import { layoutForce } from "../../core/force";
8
+ import {
9
+ layoutForce
10
+ } from "../../core/force";
8
11
  import { useChartTheme } from "../../hooks/useChartTheme";
9
12
  import { useElementSize } from "../../hooks/useElementSize";
10
13
  import { useChartTransition } from "../../hooks/useChartTransition";
11
14
  import { resolveAnimation, staggerProgress } from "../../core/animation";
12
15
  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
- * 连得紧的聚成一簇,孤立的被推到边缘。这是一种**探索**型图表——
20
- * 它的价值是让你看到结构,而不是读出精确数值。
21
- */
22
- export function GraphChart({
16
+ import { jsx, jsxs } from "react/jsx-runtime";
17
+ function GraphChart({
23
18
  nodes = [],
24
19
  links = [],
25
20
  iterations = 220,
26
21
  nodeSize = 7,
27
22
  showLabel = true,
28
23
  scaleByDegree = true,
29
- width = '100%',
24
+ width = "100%",
30
25
  height = 380,
31
26
  theme: themeInput,
32
27
  palette,
@@ -35,73 +30,85 @@ export function GraphChart({
35
30
  className,
36
31
  style,
37
32
  ariaLabel,
38
- emptyText = '暂无数据'
33
+ emptyText = "暂无数据"
39
34
  }) {
40
35
  const theme = useChartTheme(themeInput, palette);
41
- const {
42
- ref,
43
- size
44
- } = useElementSize();
36
+ const { ref, size } = useElementSize();
45
37
  const [activeId, setActiveId] = 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]);
38
+ const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
39
+ const animConfig = useMemo(
40
+ () => resolveAnimation(animation),
41
+ // eslint-disable-next-line react-hooks/exhaustive-deps
42
+ [animKey]
43
+ );
50
44
  const groups = useMemo(() => {
51
45
  const list = [];
52
- nodes.forEach(node => {
53
- const group = node.group ?? '__default__';
54
- if (!list.includes(group)) list.push(group);
46
+ nodes.forEach((node) => {
47
+ const group = node.group ?? "__default__";
48
+ if (!list.includes(group))
49
+ list.push(group);
55
50
  });
56
51
  return list;
57
52
  }, [nodes]);
58
53
  const positions = useMemo(() => {
59
- if (!nodes.length || size.width <= 0) return [];
60
- return layoutForce(nodes, links, {
61
- x: 0,
62
- y: 0,
63
- width: size.width,
64
- height: size.height
65
- }, {
66
- iterations,
67
- radius: nodeSize + 4
68
- });
54
+ if (!nodes.length || size.width <= 0)
55
+ return [];
56
+ return layoutForce(
57
+ nodes,
58
+ links,
59
+ { x: 0, y: 0, width: size.width, height: size.height },
60
+ { iterations, radius: nodeSize + 4 }
61
+ );
69
62
  }, [nodes, links, size.width, size.height, iterations, nodeSize]);
70
- const positionMap = useMemo(() => new Map(positions.map(item => [item.id, item])), [positions]);
71
- const maxDegree = Math.max(1, ...positions.map(item => item.degree));
72
- const radiusOf = useCallback(position => {
73
- if (!scaleByDegree) return nodeSize;
74
- // 连接越多半径越大,但限制在 2.2 倍以内,避免中心节点吞掉整张图
75
- return nodeSize * (1 + Math.min(1.2, position.degree / maxDegree) * 1.2);
76
- }, [scaleByDegree, nodeSize, maxDegree]);
77
- const colorOf = useCallback(node => {
78
- const groupIndex = groups.indexOf(node.group ?? '__default__');
79
- return theme.palette[Math.max(0, groupIndex) % theme.palette.length];
80
- }, [groups, theme.palette]);
81
- const degreeMatrix = useMemo(() => [positions.map(item => item.degree)], [positions]);
63
+ const positionMap = useMemo(
64
+ () => new Map(positions.map((item) => [item.id, item])),
65
+ [positions]
66
+ );
67
+ const maxDegree = Math.max(1, ...positions.map((item) => item.degree));
68
+ const radiusOf = useCallback(
69
+ (position) => {
70
+ if (!scaleByDegree)
71
+ return nodeSize;
72
+ return nodeSize * (1 + Math.min(1.2, position.degree / maxDegree) * 1.2);
73
+ },
74
+ [scaleByDegree, nodeSize, maxDegree]
75
+ );
76
+ const colorOf = useCallback(
77
+ (node) => {
78
+ const groupIndex = groups.indexOf(node.group ?? "__default__");
79
+ return theme.palette[Math.max(0, groupIndex) % theme.palette.length];
80
+ },
81
+ [groups, theme.palette]
82
+ );
83
+ const degreeMatrix = useMemo(
84
+ () => [positions.map((item) => item.degree)],
85
+ [positions]
86
+ );
82
87
  const progress = useChartTransition(degreeMatrix, animConfig).progress;
83
88
  const neighbors = useMemo(() => {
84
- const map = new Map();
89
+ const map = /* @__PURE__ */ new Map();
85
90
  for (const link of links) {
86
- if (!map.has(link.source)) map.set(link.source, new Set());
87
- if (!map.has(link.target)) map.set(link.target, new Set());
91
+ if (!map.has(link.source))
92
+ map.set(link.source, /* @__PURE__ */ new Set());
93
+ if (!map.has(link.target))
94
+ map.set(link.target, /* @__PURE__ */ new Set());
88
95
  map.get(link.source).add(link.target);
89
96
  map.get(link.target).add(link.source);
90
97
  }
91
98
  return map;
92
99
  }, [links]);
93
100
  const scene = useMemo(() => {
94
- if (!positions.length || size.width <= 0) return [];
101
+ if (!positions.length || size.width <= 0)
102
+ return [];
95
103
  const nodes_ = [];
96
-
97
- // 边
98
104
  links.forEach((link, index) => {
99
105
  const from = positionMap.get(link.source);
100
106
  const to = positionMap.get(link.target);
101
- if (!from || !to) return;
107
+ if (!from || !to)
108
+ return;
102
109
  const related = activeId === null || activeId === link.source || activeId === link.target;
103
110
  nodes_.push({
104
- kind: 'line',
111
+ kind: "line",
105
112
  key: `edge-${index}`,
106
113
  x1: from.x,
107
114
  y1: from.y,
@@ -110,56 +117,76 @@ export function GraphChart({
110
117
  stroke: related && activeId !== null ? theme.emphasis : withAlpha(theme.axis.line, 0.9),
111
118
  strokeWidth: related && activeId !== null ? 1.6 : 1,
112
119
  opacity: (related ? 1 : 0.25) * progress,
113
- pointerEvents: 'none'
120
+ pointerEvents: "none"
114
121
  });
115
122
  });
116
-
117
- // 节点
118
123
  positions.forEach((position, index) => {
119
- const node = nodes.find(item => item.id === position.id);
120
- if (!node) return;
124
+ var _a;
125
+ const node = nodes.find((item) => item.id === position.id);
126
+ if (!node)
127
+ return;
121
128
  const baseRadius = radiusOf(position);
122
- const local = staggerProgress(progress, index, positions.length, animConfig.stagger);
129
+ const local = staggerProgress(
130
+ progress,
131
+ index,
132
+ positions.length,
133
+ animConfig.stagger
134
+ );
123
135
  const radius = baseRadius * local;
124
136
  const isActive = activeId === position.id;
125
- const isNeighbor = activeId ? neighbors.get(activeId)?.has(position.id) ?? false : false;
137
+ const isNeighbor = activeId ? ((_a = neighbors.get(activeId)) == null ? void 0 : _a.has(position.id)) ?? false : false;
126
138
  const dim = activeId !== null && !isActive && !isNeighbor;
127
139
  nodes_.push({
128
- kind: 'circle',
140
+ kind: "circle",
129
141
  key: `node-${position.id}`,
130
142
  cx: position.x,
131
143
  cy: position.y,
132
144
  r: Math.max(0.5, radius),
133
145
  fill: colorOf(node),
134
146
  opacity: dim ? 0.28 : 1,
135
- stroke: isActive ? theme.text.primary : undefined,
147
+ stroke: isActive ? theme.text.primary : void 0,
136
148
  strokeWidth: isActive ? 2 : 0,
137
- pointerEvents: 'none'
149
+ pointerEvents: "none"
138
150
  });
139
- if (!showLabel || radius < 4) return;
151
+ if (!showLabel || radius < 4)
152
+ return;
140
153
  const label = node.name ?? node.id;
141
- // 标签最多占 20% 画布宽度,超了就截断
142
154
  const maxWidth = size.width * 0.2;
143
155
  const text = estimateTextWidth(label, theme.font.sizeSmall) > maxWidth ? `${label.slice(0, Math.max(2, Math.floor(maxWidth / theme.font.sizeSmall)))}…` : label;
144
156
  nodes_.push({
145
- kind: 'text',
157
+ kind: "text",
146
158
  key: `label-${position.id}`,
147
159
  x: position.x,
148
160
  y: position.y + radius + theme.font.sizeSmall + 2,
149
161
  text,
150
162
  fill: theme.text.secondary,
151
163
  fontSize: theme.font.sizeSmall,
152
- anchor: 'middle',
153
- baseline: 'middle',
164
+ anchor: "middle",
165
+ baseline: "middle",
154
166
  opacity: dim ? 0.3 : local,
155
167
  fontFamily: theme.font.family
156
168
  });
157
169
  });
158
170
  return nodes_;
159
- }, [positions, links, positionMap, nodes, theme, activeId, neighbors, progress, animConfig.stagger, showLabel, size.width, radiusOf, colorOf]);
171
+ }, [
172
+ positions,
173
+ links,
174
+ positionMap,
175
+ nodes,
176
+ theme,
177
+ activeId,
178
+ neighbors,
179
+ progress,
180
+ animConfig.stagger,
181
+ showLabel,
182
+ size.width,
183
+ radiusOf,
184
+ colorOf
185
+ ]);
160
186
  const nodeFromPointer = (clientX, clientY) => {
161
187
  const element = ref.current;
162
- if (!element) return null;
188
+ if (!element)
189
+ return null;
163
190
  const rect = element.getBoundingClientRect();
164
191
  const x = clientX - rect.left;
165
192
  const y = clientY - rect.top;
@@ -175,50 +202,63 @@ export function GraphChart({
175
202
  }
176
203
  return nearest;
177
204
  };
178
- const activeNode = activeId ? nodes.find(node => node.id === activeId) : null;
205
+ const activeNode = activeId ? nodes.find((node) => node.id === activeId) : null;
179
206
  const activePosition = activeId ? positionMap.get(activeId) : null;
180
207
  const tooltipPayload = activeNode ? {
181
208
  label: activeNode.name ?? activeNode.id,
182
209
  row: null,
183
- series: [{
184
- key: 'degree',
185
- name: '连接数',
186
- color: colorOf(activeNode),
187
- value: activePosition?.degree ?? 0,
188
- formatted: formatNumber(activePosition?.degree ?? 0)
189
- }]
210
+ series: [
211
+ {
212
+ key: "degree",
213
+ name: "连接数",
214
+ color: colorOf(activeNode),
215
+ value: (activePosition == null ? void 0 : activePosition.degree) ?? 0,
216
+ formatted: formatNumber((activePosition == null ? void 0 : activePosition.degree) ?? 0)
217
+ }
218
+ ]
190
219
  } : null;
191
220
  const autoAriaLabel = ariaLabel ?? `关系图:${nodes.length} 个节点,${links.length} 条关系`;
192
- return /*#__PURE__*/_jsx("div", {
193
- className: classNames(prefixCls('chart'), className),
194
- style: {
195
- width,
196
- height,
197
- ...style
198
- },
199
- "data-aura-chart": "graph",
200
- children: /*#__PURE__*/_jsxs("div", {
201
- ref: ref,
202
- className: prefixCls('chart__plot'),
203
- onPointerMove: event => setActiveId(nodeFromPointer(event.clientX, event.clientY)),
204
- onPointerLeave: () => setActiveId(null),
205
- children: [!positions.length || size.width === 0 ? /*#__PURE__*/_jsx("div", {
206
- className: prefixCls('chart__empty'),
207
- children: emptyText
208
- }) : /*#__PURE__*/_jsx(SvgScene, {
209
- nodes: scene,
210
- width: size.width,
211
- height: size.height,
212
- ariaLabel: autoAriaLabel
213
- }), /*#__PURE__*/_jsx(Tooltip, {
214
- payload: tooltipPayload,
215
- x: activePosition ? activePosition.x : 0,
216
- y: activePosition ? activePosition.y - 20 : 0,
217
- containerWidth: size.width,
218
- containerHeight: size.height,
219
- theme: theme,
220
- config: tooltip
221
- })]
222
- })
223
- });
224
- }
221
+ return /* @__PURE__ */ jsx(
222
+ "div",
223
+ {
224
+ className: classNames(prefixCls("chart"), className),
225
+ style: { width, height, ...style },
226
+ "data-aura-chart": "graph",
227
+ children: /* @__PURE__ */ jsxs(
228
+ "div",
229
+ {
230
+ ref,
231
+ className: prefixCls("chart__plot"),
232
+ onPointerMove: (event) => setActiveId(nodeFromPointer(event.clientX, event.clientY)),
233
+ onPointerLeave: () => setActiveId(null),
234
+ children: [
235
+ !positions.length || size.width === 0 ? /* @__PURE__ */ jsx("div", { className: prefixCls("chart__empty"), children: emptyText }) : /* @__PURE__ */ jsx(
236
+ SvgScene,
237
+ {
238
+ nodes: scene,
239
+ width: size.width,
240
+ height: size.height,
241
+ ariaLabel: autoAriaLabel
242
+ }
243
+ ),
244
+ /* @__PURE__ */ jsx(
245
+ Tooltip,
246
+ {
247
+ payload: tooltipPayload,
248
+ x: activePosition ? activePosition.x : 0,
249
+ y: activePosition ? activePosition.y - 20 : 0,
250
+ containerWidth: size.width,
251
+ containerHeight: size.height,
252
+ theme,
253
+ config: tooltip
254
+ }
255
+ )
256
+ ]
257
+ }
258
+ )
259
+ }
260
+ );
261
+ }
262
+ export {
263
+ GraphChart
264
+ };