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