@aura-react-comp/charts 0.0.0-stage → 0.3.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.
Files changed (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -2
  3. package/esm/charts/area-chart/index.d.ts +19 -0
  4. package/esm/charts/area-chart/index.d.ts.map +1 -0
  5. package/esm/charts/area-chart/index.js +31 -0
  6. package/esm/charts/bar-chart/index.d.ts +16 -0
  7. package/esm/charts/bar-chart/index.d.ts.map +1 -0
  8. package/esm/charts/bar-chart/index.js +28 -0
  9. package/esm/charts/candlestick-chart/index.d.ts +47 -0
  10. package/esm/charts/candlestick-chart/index.d.ts.map +1 -0
  11. package/esm/charts/candlestick-chart/index.js +347 -0
  12. package/esm/charts/combo-chart/index.d.ts +17 -0
  13. package/esm/charts/combo-chart/index.d.ts.map +1 -0
  14. package/esm/charts/combo-chart/index.js +26 -0
  15. package/esm/charts/funnel-chart/index.d.ts +35 -0
  16. package/esm/charts/funnel-chart/index.d.ts.map +1 -0
  17. package/esm/charts/funnel-chart/index.js +200 -0
  18. package/esm/charts/gauge-chart/index.d.ts +41 -0
  19. package/esm/charts/gauge-chart/index.d.ts.map +1 -0
  20. package/esm/charts/gauge-chart/index.js +217 -0
  21. package/esm/charts/graph-chart/index.d.ts +35 -0
  22. package/esm/charts/graph-chart/index.d.ts.map +1 -0
  23. package/esm/charts/graph-chart/index.js +224 -0
  24. package/esm/charts/heatmap-chart/index.d.ts +47 -0
  25. package/esm/charts/heatmap-chart/index.d.ts.map +1 -0
  26. package/esm/charts/heatmap-chart/index.js +281 -0
  27. package/esm/charts/line-chart/index.d.ts +23 -0
  28. package/esm/charts/line-chart/index.d.ts.map +1 -0
  29. package/esm/charts/line-chart/index.js +33 -0
  30. package/esm/charts/pie-chart/index.d.ts +38 -0
  31. package/esm/charts/pie-chart/index.d.ts.map +1 -0
  32. package/esm/charts/pie-chart/index.js +272 -0
  33. package/esm/charts/radar-chart/index.d.ts +44 -0
  34. package/esm/charts/radar-chart/index.d.ts.map +1 -0
  35. package/esm/charts/radar-chart/index.js +263 -0
  36. package/esm/charts/ring-progress/index.d.ts +38 -0
  37. package/esm/charts/ring-progress/index.d.ts.map +1 -0
  38. package/esm/charts/ring-progress/index.js +137 -0
  39. package/esm/charts/sankey-chart/index.d.ts +34 -0
  40. package/esm/charts/sankey-chart/index.d.ts.map +1 -0
  41. package/esm/charts/sankey-chart/index.js +248 -0
  42. package/esm/charts/scatter-chart/index.d.ts +34 -0
  43. package/esm/charts/scatter-chart/index.d.ts.map +1 -0
  44. package/esm/charts/scatter-chart/index.js +305 -0
  45. package/esm/charts/sparkline/index.d.ts +31 -0
  46. package/esm/charts/sparkline/index.d.ts.map +1 -0
  47. package/esm/charts/sparkline/index.js +152 -0
  48. package/esm/charts/tree-chart/index.d.ts +34 -0
  49. package/esm/charts/tree-chart/index.d.ts.map +1 -0
  50. package/esm/charts/tree-chart/index.js +200 -0
  51. package/esm/charts/treemap-chart/index.d.ts +35 -0
  52. package/esm/charts/treemap-chart/index.d.ts.map +1 -0
  53. package/esm/charts/treemap-chart/index.js +239 -0
  54. package/esm/charts/waterfall-chart/index.d.ts +41 -0
  55. package/esm/charts/waterfall-chart/index.d.ts.map +1 -0
  56. package/esm/charts/waterfall-chart/index.js +281 -0
  57. package/esm/components/Legend.d.ts +24 -0
  58. package/esm/components/Legend.d.ts.map +1 -0
  59. package/esm/components/Legend.js +50 -0
  60. package/esm/components/Tooltip.d.ts +24 -0
  61. package/esm/components/Tooltip.d.ts.map +1 -0
  62. package/esm/components/Tooltip.js +85 -0
  63. package/esm/components/chart/index.d.ts +71 -0
  64. package/esm/components/chart/index.d.ts.map +1 -0
  65. package/esm/components/chart/index.js +142 -0
  66. package/esm/components/chart.less +223 -0
  67. package/esm/core/animation.d.ts +49 -0
  68. package/esm/core/animation.d.ts.map +1 -0
  69. package/esm/core/animation.js +93 -0
  70. package/esm/core/force.d.ts +43 -0
  71. package/esm/core/force.d.ts.map +1 -0
  72. package/esm/core/force.js +134 -0
  73. package/esm/core/format.d.ts +34 -0
  74. package/esm/core/format.d.ts.map +1 -0
  75. package/esm/core/format.js +81 -0
  76. package/esm/core/hierarchy.d.ts +77 -0
  77. package/esm/core/hierarchy.d.ts.map +1 -0
  78. package/esm/core/hierarchy.js +233 -0
  79. package/esm/core/layout.d.ts +67 -0
  80. package/esm/core/layout.d.ts.map +1 -0
  81. package/esm/core/layout.js +121 -0
  82. package/esm/core/sampling.d.ts +32 -0
  83. package/esm/core/sampling.d.ts.map +1 -0
  84. package/esm/core/sampling.js +124 -0
  85. package/esm/core/sankey.d.ts +67 -0
  86. package/esm/core/sankey.d.ts.map +1 -0
  87. package/esm/core/sankey.js +136 -0
  88. package/esm/core/scale.d.ts +98 -0
  89. package/esm/core/scale.d.ts.map +1 -0
  90. package/esm/core/scale.js +207 -0
  91. package/esm/core/shape.d.ts +60 -0
  92. package/esm/core/shape.d.ts.map +1 -0
  93. package/esm/core/shape.js +246 -0
  94. package/esm/global.d.ts +6 -0
  95. package/esm/hooks/useChartTheme.d.ts +11 -0
  96. package/esm/hooks/useChartTheme.d.ts.map +1 -0
  97. package/esm/hooks/useChartTheme.js +35 -0
  98. package/esm/hooks/useChartTransition.d.ts +20 -0
  99. package/esm/hooks/useChartTransition.d.ts.map +1 -0
  100. package/esm/hooks/useChartTransition.js +120 -0
  101. package/esm/hooks/useElementSize.d.ts +17 -0
  102. package/esm/hooks/useElementSize.d.ts.map +1 -0
  103. package/esm/hooks/useElementSize.js +47 -0
  104. package/esm/index.d.ts +62 -0
  105. package/esm/index.d.ts.map +1 -0
  106. package/esm/index.js +48 -0
  107. package/esm/internal/CartesianChart.d.ts +16 -0
  108. package/esm/internal/CartesianChart.d.ts.map +1 -0
  109. package/esm/internal/CartesianChart.js +811 -0
  110. package/esm/internal/model.d.ts +67 -0
  111. package/esm/internal/model.d.ts.map +1 -0
  112. package/esm/internal/model.js +146 -0
  113. package/esm/scene/canvas.d.ts +18 -0
  114. package/esm/scene/canvas.d.ts.map +1 -0
  115. package/esm/scene/canvas.js +138 -0
  116. package/esm/scene/svg.d.ts +12 -0
  117. package/esm/scene/svg.d.ts.map +1 -0
  118. package/esm/scene/svg.js +116 -0
  119. package/esm/scene/types.d.ts +89 -0
  120. package/esm/scene/types.d.ts.map +1 -0
  121. package/esm/scene/types.js +21 -0
  122. package/esm/style.css +196 -0
  123. package/esm/theme/index.d.ts +127 -0
  124. package/esm/theme/index.d.ts.map +1 -0
  125. package/esm/theme/index.js +300 -0
  126. package/esm/types.d.ts +168 -0
  127. package/esm/types.d.ts.map +1 -0
  128. package/esm/types.js +2 -0
  129. package/package.json +64 -3
@@ -0,0 +1,272 @@
1
+ 'use client';
2
+ import React, { useCallback, useMemo, useState } from 'react';
3
+ import { classNames, prefixCls } from '@aura-react-comp/shared';
4
+ import { SvgScene } from "../../scene/svg";
5
+ import { arcPath } from "../../core/shape";
6
+ import { formatPercent } from "../../core/format";
7
+ import { useChartTheme } from "../../hooks/useChartTheme";
8
+ import { useElementSize } from "../../hooks/useElementSize";
9
+ import { useChartTransition } from "../../hooks/useChartTransition";
10
+ import { resolveAnimation, staggerProgress } from "../../core/animation";
11
+ import { Legend } from "../../components/Legend";
12
+ import { Tooltip } from "../../components/Tooltip";
13
+ import { jsx as _jsx } from "react/jsx-runtime";
14
+ import { Fragment as _Fragment } from "react/jsx-runtime";
15
+ import { jsxs as _jsxs } from "react/jsx-runtime";
16
+ /**
17
+ * 饼图 / 环形图。
18
+ *
19
+ * 交互命中不用 DOM 事件绑定在每个扇形上,而是把鼠标坐标换算成极坐标角度后
20
+ * 反查所属扇区 —— 这样 SVG 与 Canvas 两种渲染方式可以共用同一套交互逻辑。
21
+ */
22
+ export function PieChart({
23
+ data = [],
24
+ width = '100%',
25
+ height = 320,
26
+ theme: themeInput,
27
+ palette,
28
+ legend = true,
29
+ tooltip = true,
30
+ innerRadius = 0.6,
31
+ padAngle = 0.01,
32
+ label = 'outside',
33
+ centerContent,
34
+ animation = true,
35
+ className,
36
+ style,
37
+ ariaLabel,
38
+ emptyText = '暂无数据'
39
+ }) {
40
+ const theme = useChartTheme(themeInput, palette);
41
+ const {
42
+ ref,
43
+ size
44
+ } = useElementSize();
45
+ const [hiddenKeys, setHiddenKeys] = useState([]);
46
+ const [activeIndex, setActiveIndex] = useState(null);
47
+ const visibleData = useMemo(() => data.filter(item => !hiddenKeys.includes(item.name)), [data, hiddenKeys]);
48
+ const total = useMemo(() => visibleData.reduce((sum, item) => sum + (Number.isFinite(item.value) ? item.value : 0), 0), [visibleData]);
49
+ const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
50
+ const animConfig = useMemo(() => resolveAnimation(animation),
51
+ // eslint-disable-next-line react-hooks/exhaustive-deps
52
+ [animKey]);
53
+
54
+ // 用插值后的数值重算角度:数据更新时扇区会平滑重排,而不是整块重画
55
+ const valueMatrix = useMemo(() => [visibleData.map(item => Number.isFinite(item.value) ? item.value : 0)], [visibleData]);
56
+ const transition = useChartTransition(valueMatrix, animConfig);
57
+ const progress = transition.progress;
58
+ const animatedValues = transition.values[0] ?? valueMatrix[0];
59
+ const animatedTotal = animatedValues.reduce((sum, value) => sum + (value ?? 0), 0) || 1;
60
+ const slices = useMemo(() => {
61
+ const sum = animatedTotal || 1;
62
+ let angle = -Math.PI / 2;
63
+ return visibleData.map((item, index) => {
64
+ const ratio = (animatedValues[index] ?? 0) / sum;
65
+ const sweep = ratio * Math.PI * 2;
66
+ const slice = {
67
+ ...item,
68
+ index,
69
+ color: item.color ?? theme.palette[index % theme.palette.length],
70
+ startAngle: angle,
71
+ endAngle: angle + sweep,
72
+ percent: ratio
73
+ };
74
+ angle += sweep;
75
+ return slice;
76
+ });
77
+ }, [visibleData, animatedValues, animatedTotal, theme.palette]);
78
+ const geometry = useMemo(() => {
79
+ const outer = Math.max(0, Math.min(size.width, size.height) / 2 - (label === 'outside' ? 36 : 14));
80
+ const inner = outer * Math.max(0, Math.min(0.92, innerRadius));
81
+ return {
82
+ cx: size.width / 2,
83
+ cy: size.height / 2,
84
+ outer,
85
+ inner
86
+ };
87
+ }, [size.width, size.height, label, innerRadius]);
88
+ const scene = useMemo(() => {
89
+ if (!slices.length || geometry.outer <= 0) return [];
90
+ const nodes = [];
91
+ for (const slice of slices) {
92
+ // 扇区错峰展开:从 12 点方向顺时针依次扫出,比整块同时膨胀自然
93
+ const local = staggerProgress(progress, slice.index, slices.length, animConfig.stagger);
94
+ const end = slice.startAngle + (slice.endAngle - slice.startAngle) * local;
95
+ const isActive = activeIndex === slice.index;
96
+ const gap = slices.length > 1 ? padAngle / 2 : 0;
97
+ const outer = isActive ? geometry.outer + 3 : geometry.outer;
98
+ nodes.push({
99
+ kind: 'path',
100
+ key: `slice-${slice.name}`,
101
+ d: arcPath(geometry.cx, geometry.cy, geometry.inner, outer, slice.startAngle + gap, Math.max(slice.startAngle + gap, end - gap)),
102
+ fill: slice.color,
103
+ opacity: activeIndex === null || isActive ? 1 : 0.55,
104
+ pointerEvents: 'none'
105
+ });
106
+ if (label === 'inside' && slice.percent >= 0.05) {
107
+ const mid = (slice.startAngle + end) / 2;
108
+ const radius = (geometry.inner + outer) / 2;
109
+ nodes.push({
110
+ kind: 'text',
111
+ key: `inside-${slice.name}`,
112
+ x: geometry.cx + Math.cos(mid) * radius,
113
+ y: geometry.cy + Math.sin(mid) * radius,
114
+ text: formatPercent(slice.percent, 1, 0),
115
+ fill: '#fff',
116
+ fontSize: theme.font.size,
117
+ fontWeight: theme.font.weightMedium,
118
+ anchor: 'middle',
119
+ baseline: 'middle',
120
+ opacity: progress
121
+ });
122
+ }
123
+ if (label === 'outside' && progress > 0.6 && slice.percent > 0.02) {
124
+ const mid = (slice.startAngle + end) / 2;
125
+ const anchorX = geometry.cx + Math.cos(mid) * (outer + 8);
126
+ const anchorY = geometry.cy + Math.sin(mid) * (outer + 8);
127
+ const elbowX = geometry.cx + Math.cos(mid) * (outer + 16);
128
+ const elbowY = geometry.cy + Math.sin(mid) * (outer + 16);
129
+ const isRight = Math.cos(mid) >= 0;
130
+ const textX = elbowX + (isRight ? 8 : -8);
131
+ const textY = elbowY + (Math.sin(mid) > 0.4 ? 4 : Math.sin(mid) < -0.4 ? -2 : 0);
132
+ nodes.push({
133
+ kind: 'path',
134
+ key: `leader-${slice.name}`,
135
+ d: `M${anchorX},${anchorY}L${elbowX},${elbowY}L${textX},${textY}`,
136
+ fill: 'none',
137
+ stroke: theme.axis.line,
138
+ strokeWidth: 1,
139
+ pointerEvents: 'none'
140
+ });
141
+ nodes.push({
142
+ kind: 'text',
143
+ key: `outside-${slice.name}`,
144
+ x: textX + (isRight ? 2 : -2),
145
+ y: textY,
146
+ text: `${slice.name} ${formatPercent(slice.percent, 1, 1)}`,
147
+ fill: isActive ? theme.text.primary : theme.text.secondary,
148
+ fontSize: theme.font.size,
149
+ anchor: isRight ? 'start' : 'end',
150
+ baseline: 'middle',
151
+ fontWeight: isActive ? theme.font.weightMedium : theme.font.weightNormal
152
+ });
153
+ }
154
+ }
155
+ if (geometry.inner > 0) {
156
+ const active = activeIndex !== null ? slices[activeIndex] : null;
157
+ nodes.push({
158
+ kind: 'text',
159
+ key: 'center-main',
160
+ x: geometry.cx,
161
+ y: geometry.cy - (active ? 6 : 0),
162
+ text: active ? String(Math.round(active.value)) : String(Math.round(animatedTotal)),
163
+ fill: theme.text.primary,
164
+ fontSize: active ? 20 : 22,
165
+ fontWeight: theme.font.weightMedium,
166
+ anchor: 'middle',
167
+ baseline: 'middle'
168
+ });
169
+ nodes.push({
170
+ kind: 'text',
171
+ key: 'center-sub',
172
+ x: geometry.cx,
173
+ y: geometry.cy + 18,
174
+ text: active ? `${active.name} · ${formatPercent(active.percent, 1, 1)}` : '总计',
175
+ fill: theme.text.secondary,
176
+ fontSize: theme.font.sizeSmall,
177
+ anchor: 'middle',
178
+ baseline: 'middle'
179
+ });
180
+ }
181
+ return nodes;
182
+ }, [slices, geometry, progress, activeIndex, label, padAngle, theme, animatedTotal, animConfig.stagger]);
183
+
184
+ /** 指针 → 扇区索引 */
185
+ const indexFromPointer = useCallback((clientX, clientY) => {
186
+ const element = ref.current;
187
+ if (!element || !slices.length) return null;
188
+ const rect = element.getBoundingClientRect();
189
+ const dx = clientX - rect.left - geometry.cx;
190
+ const dy = clientY - rect.top - geometry.cy;
191
+ const radius = Math.hypot(dx, dy);
192
+ if (radius < geometry.inner || radius > geometry.outer + 6) return null;
193
+ let angle = Math.atan2(dy, dx);
194
+ const start = -Math.PI / 2;
195
+ // 归一化到 [start, start + 2π)
196
+ while (angle < start) angle += Math.PI * 2;
197
+ while (angle >= start + Math.PI * 2) angle -= Math.PI * 2;
198
+ const hit = slices.find(slice => angle >= slice.startAngle && angle < slice.endAngle);
199
+ return hit ? hit.index : null;
200
+ }, [ref, slices, geometry]);
201
+ const active = activeIndex !== null ? slices[activeIndex] : null;
202
+ const tooltipPayload = active ? {
203
+ label: active.name,
204
+ row: null,
205
+ series: [{
206
+ key: active.name,
207
+ name: active.name,
208
+ color: active.color,
209
+ value: active.value,
210
+ formatted: `${active.value}(${formatPercent(active.percent, 1, 1)})`
211
+ }]
212
+ } : null;
213
+ const legendItems = data.map((item, index) => ({
214
+ key: item.name,
215
+ name: item.name,
216
+ color: item.color ?? theme.palette[index % theme.palette.length],
217
+ visible: !hiddenKeys.includes(item.name)
218
+ }));
219
+ const toggle = useCallback(key => {
220
+ setHiddenKeys(prev => prev.includes(key) ? prev.filter(item => item !== key) : [...prev, key]);
221
+ }, []);
222
+ const autoAriaLabel = ariaLabel ?? '饼图';
223
+ return /*#__PURE__*/_jsxs("div", {
224
+ className: classNames(prefixCls('chart'), className),
225
+ style: {
226
+ width,
227
+ height,
228
+ ...style
229
+ },
230
+ "data-aura-chart": "pie",
231
+ children: [/*#__PURE__*/_jsx(Legend, {
232
+ items: legendItems,
233
+ theme: theme,
234
+ config: legend,
235
+ onToggle: toggle
236
+ }), /*#__PURE__*/_jsxs("div", {
237
+ ref: ref,
238
+ className: prefixCls('chart__plot'),
239
+ onPointerMove: event => setActiveIndex(indexFromPointer(event.clientX, event.clientY)),
240
+ onPointerLeave: () => setActiveIndex(null),
241
+ children: [slices.length === 0 || total <= 0 ? /*#__PURE__*/_jsx("div", {
242
+ className: prefixCls('chart__empty'),
243
+ children: emptyText
244
+ }) : /*#__PURE__*/_jsxs(_Fragment, {
245
+ children: [/*#__PURE__*/_jsx(SvgScene, {
246
+ nodes: scene,
247
+ width: size.width,
248
+ height: size.height,
249
+ ariaLabel: autoAriaLabel
250
+ }), centerContent && geometry.inner > 0 ? /*#__PURE__*/_jsx("div", {
251
+ style: {
252
+ position: 'absolute',
253
+ inset: 0,
254
+ display: 'flex',
255
+ alignItems: 'center',
256
+ justifyContent: 'center',
257
+ pointerEvents: 'none'
258
+ },
259
+ children: centerContent
260
+ }) : null]
261
+ }), /*#__PURE__*/_jsx(Tooltip, {
262
+ payload: tooltipPayload,
263
+ x: geometry.cx,
264
+ y: activeIndex !== null ? geometry.cy - geometry.outer * 0.5 : 0,
265
+ containerWidth: size.width,
266
+ containerHeight: size.height,
267
+ theme: theme,
268
+ config: tooltip
269
+ })]
270
+ })]
271
+ });
272
+ }
@@ -0,0 +1,44 @@
1
+ import React from 'react';
2
+ import { type ChartThemeInput } from '../../theme/index.js';
3
+ import type { LegendConfig, TooltipConfig } from '../../types.js';
4
+ export interface RadarIndicator {
5
+ /** 指标名,显示在顶点外侧 */
6
+ name: string;
7
+ /** 该指标的最大值 */
8
+ max: number;
9
+ }
10
+ export interface RadarSeries {
11
+ key: string;
12
+ name?: string;
13
+ /** 与 indicators 一一对应的数值 */
14
+ values: number[];
15
+ color?: string;
16
+ }
17
+ export interface RadarChartProps {
18
+ indicators: RadarIndicator[];
19
+ series: RadarSeries[];
20
+ width?: number | string;
21
+ height?: number;
22
+ theme?: ChartThemeInput;
23
+ palette?: string[];
24
+ legend?: boolean | LegendConfig;
25
+ tooltip?: boolean | TooltipConfig;
26
+ /** 网格层数(同心多边形) */
27
+ levels?: number;
28
+ animation?: boolean;
29
+ className?: string;
30
+ style?: React.CSSProperties;
31
+ ariaLabel?: string;
32
+ emptyText?: string;
33
+ }
34
+ /**
35
+ * 雷达图。
36
+ *
37
+ * 几何本质是极坐标:n 个指标把圆周等分,每个指标一条轴,
38
+ * 数值映射为「沿轴距圆心的距离」。
39
+ *
40
+ * 与饼图相反,雷达图不编码总量,它编码的是**形状**——多指标的综合轮廓。
41
+ * 因此各指标的 max 必须由使用者显式给出:自动取最大值会让「谁更好」失去参照。
42
+ */
43
+ export declare function RadarChart({ indicators, series, width, height, theme: themeInput, palette, legend, tooltip, levels, animation, className, style, ariaLabel, emptyText, }: RadarChartProps): React.JSX.Element;
44
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyC,MAAM,OAAO,CAAC;AAI9D,OAAO,EAAa,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAO9D,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE/D,OAAO,6BAA6B,CAAC;AAErC,MAAM,WAAW,cAAc;IAC7B,kBAAkB;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,cAAc;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2BAA2B;IAC3B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,cAAc,EAAE,CAAC;IAC7B,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,GAAG,YAAY,CAAC;IAChC,OAAO,CAAC,EAAE,OAAO,GAAG,aAAa,CAAC;IAClC,kBAAkB;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAID;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,EACzB,UAAe,EACf,MAAW,EACX,KAAc,EACd,MAAY,EACZ,KAAK,EAAE,UAAU,EACjB,OAAO,EACP,MAAa,EACb,OAAc,EACd,MAAU,EACV,SAAgB,EAChB,SAAS,EACT,KAAK,EACL,SAAS,EACT,SAAkB,GACnB,EAAE,eAAe,qBA6RjB"}
@@ -0,0 +1,263 @@
1
+ 'use client';
2
+ import React, { useCallback, useMemo, useState } from 'react';
3
+ import { classNames, prefixCls } from '@aura-react-comp/shared';
4
+ import { SvgScene } from "../../scene/svg";
5
+ import { withAlpha } from "../../theme";
6
+ import { useChartTheme } from "../../hooks/useChartTheme";
7
+ import { useElementSize } from "../../hooks/useElementSize";
8
+ import { useChartTransition } from "../../hooks/useChartTransition";
9
+ import { resolveAnimation, staggerProgress } from "../../core/animation";
10
+ import { Legend } from "../../components/Legend";
11
+ import { Tooltip } from "../../components/Tooltip";
12
+ import { estimateTextWidth } from "../../core/format";
13
+ import { jsx as _jsx } from "react/jsx-runtime";
14
+ import { jsxs as _jsxs } from "react/jsx-runtime";
15
+ const GRID_LABEL_GAP = 10;
16
+
17
+ /**
18
+ * 雷达图。
19
+ *
20
+ * 几何本质是极坐标:n 个指标把圆周等分,每个指标一条轴,
21
+ * 数值映射为「沿轴距圆心的距离」。
22
+ *
23
+ * 与饼图相反,雷达图不编码总量,它编码的是**形状**——多指标的综合轮廓。
24
+ * 因此各指标的 max 必须由使用者显式给出:自动取最大值会让「谁更好」失去参照。
25
+ */
26
+ export function RadarChart({
27
+ indicators = [],
28
+ series = [],
29
+ width = '100%',
30
+ height = 320,
31
+ theme: themeInput,
32
+ palette,
33
+ legend = true,
34
+ tooltip = true,
35
+ levels = 4,
36
+ animation = true,
37
+ className,
38
+ style,
39
+ ariaLabel,
40
+ emptyText = '暂无数据'
41
+ }) {
42
+ const theme = useChartTheme(themeInput, palette);
43
+ const {
44
+ ref,
45
+ size
46
+ } = useElementSize();
47
+ const [hiddenKeys, setHiddenKeys] = useState([]);
48
+ const [activeKey, setActiveKey] = useState(null);
49
+ const visible = useMemo(() => series.filter(item => !hiddenKeys.includes(item.key)), [series, hiddenKeys]);
50
+ const colors = useMemo(() => series.map((item, index) => item.color ?? theme.palette[index % theme.palette.length]), [series, theme.palette]);
51
+ const geometry = useMemo(() => {
52
+ const count = Math.max(indicators.length, 3);
53
+ // 顶点标签要占位置,半径留出文字宽度的一半
54
+ const maxLabel = indicators.reduce((max, item) => Math.max(max, estimateTextWidth(item.name, theme.font.size)), 0);
55
+ const padding = Math.min(64, maxLabel / 2 + GRID_LABEL_GAP + 6);
56
+ const radius = Math.max(0, Math.min(size.width, size.height) / 2 - padding);
57
+ return {
58
+ cx: size.width / 2,
59
+ cy: size.height / 2,
60
+ radius,
61
+ count
62
+ };
63
+ }, [indicators, size.width, size.height, theme.font.size]);
64
+ const angleAt = useCallback(index => -Math.PI / 2 + index * Math.PI * 2 / geometry.count, [geometry.count]);
65
+ const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
66
+ const animConfig = useMemo(() => resolveAnimation(animation),
67
+ // eslint-disable-next-line react-hooks/exhaustive-deps
68
+ [animKey]);
69
+ const valueMatrix = useMemo(() => series.map(item => item.values), [series]);
70
+ const transition = useChartTransition(valueMatrix, animConfig);
71
+ const progress = transition.progress;
72
+ const scene = useMemo(() => {
73
+ const {
74
+ cx,
75
+ cy,
76
+ radius
77
+ } = geometry;
78
+ if (radius <= 0 || indicators.length < 3) return [];
79
+ const nodes = [];
80
+
81
+ // 同心多边形网格
82
+ for (let level = 1; level <= levels; level += 1) {
83
+ const ratio = level / levels;
84
+ const points = indicators.map((_, index) => {
85
+ const angle = angleAt(index);
86
+ return `${cx + Math.cos(angle) * radius * ratio},${cy + Math.sin(angle) * radius * ratio}`;
87
+ }).join('L');
88
+ nodes.push({
89
+ kind: 'path',
90
+ key: `grid-${level}`,
91
+ d: `M${points}Z`,
92
+ fill: level % 2 === 0 ? withAlpha(theme.grid.line, 0.05) : 'none',
93
+ stroke: theme.grid.line,
94
+ strokeWidth: theme.grid.lineWidth,
95
+ strokeDasharray: level === levels ? undefined : theme.grid.dash.join(' '),
96
+ pointerEvents: 'none'
97
+ });
98
+ }
99
+
100
+ // 轴线
101
+ indicators.forEach((_, index) => {
102
+ const angle = angleAt(index);
103
+ nodes.push({
104
+ kind: 'line',
105
+ key: `axis-${index}`,
106
+ x1: cx,
107
+ y1: cy,
108
+ x2: cx + Math.cos(angle) * radius,
109
+ y2: cy + Math.sin(angle) * radius,
110
+ stroke: theme.axis.line,
111
+ strokeWidth: 1,
112
+ pointerEvents: 'none'
113
+ });
114
+ });
115
+
116
+ // 指标名
117
+ indicators.forEach((indicator, index) => {
118
+ const angle = angleAt(index);
119
+ const x = cx + Math.cos(angle) * (radius + GRID_LABEL_GAP);
120
+ const y = cy + Math.sin(angle) * (radius + GRID_LABEL_GAP);
121
+ const cos = Math.cos(angle);
122
+ nodes.push({
123
+ kind: 'text',
124
+ key: `label-${index}`,
125
+ x,
126
+ y,
127
+ text: indicator.name,
128
+ fill: theme.text.secondary,
129
+ fontSize: theme.font.size,
130
+ anchor: Math.abs(cos) < 0.2 ? 'middle' : cos > 0 ? 'start' : 'end',
131
+ baseline: 'middle',
132
+ fontFamily: theme.font.family
133
+ });
134
+ });
135
+
136
+ // 数据多边形
137
+ visible.forEach(item => {
138
+ const seriesIndex = series.indexOf(item);
139
+ const color = colors[seriesIndex] ?? theme.palette[0];
140
+ const isActive = activeKey === item.key;
141
+ // 整块轮廓从圆心展开(同一系列内顶点同步),比逐点长出更符合「形状」语义
142
+ const local = staggerProgress(progress, seriesIndex, visible.length, animConfig.stagger);
143
+ const row = transition.values[seriesIndex] ?? item.values;
144
+ const points = indicators.map((indicator, index) => {
145
+ const raw = row[index] ?? 0;
146
+ const max = indicator.max || 1;
147
+ // 超出 max 的数值会画到网格外,视觉上必须夹紧而不是溢出画布
148
+ const ratio = Math.min(1.2, Math.max(0, raw / max)) * local;
149
+ const angle = angleAt(index);
150
+ return {
151
+ x: geometry.cx + Math.cos(angle) * geometry.radius * ratio,
152
+ y: geometry.cy + Math.sin(angle) * geometry.radius * ratio
153
+ };
154
+ });
155
+ nodes.push({
156
+ kind: 'path',
157
+ key: `area-${item.key}`,
158
+ d: `M${points.map(p => `${p.x},${p.y}`).join('L')}Z`,
159
+ fill: withAlpha(color, isActive ? 0.28 : 0.16),
160
+ stroke: color,
161
+ strokeWidth: isActive ? theme.series.emphasisWidth : theme.series.lineWidth,
162
+ linejoin: 'round',
163
+ opacity: activeKey === null || isActive ? 1 : 0.5,
164
+ pointerEvents: 'none'
165
+ });
166
+ points.forEach((point, index) => {
167
+ nodes.push({
168
+ kind: 'circle',
169
+ key: `dot-${item.key}-${index}`,
170
+ cx: point.x,
171
+ cy: point.y,
172
+ r: isActive ? 4 : 3,
173
+ fill: theme.tooltip.background,
174
+ stroke: color,
175
+ strokeWidth: 2,
176
+ opacity: activeKey === null || isActive ? 1 : 0.5,
177
+ pointerEvents: 'none'
178
+ });
179
+ });
180
+ });
181
+ return nodes;
182
+ }, [geometry, indicators, levels, angleAt, theme, visible, colors, series, activeKey, progress, transition.values, animConfig.stagger]);
183
+
184
+ /** 指针 → 最近的多边形(按包围盒近似,够用且不引入额外几何计算) */
185
+ const findNearest = useCallback((clientX, clientY) => {
186
+ const element = ref.current;
187
+ if (!element || visible.length === 0) return null;
188
+ const rect = element.getBoundingClientRect();
189
+ const localX = clientX - rect.left;
190
+ const localY = clientY - rect.top;
191
+ const distance = Math.hypot(localX - geometry.cx, localY - geometry.cy);
192
+ if (distance > geometry.radius + 12) return null;
193
+ return visible[0].key;
194
+ }, [ref, visible, geometry]);
195
+ const active = useMemo(() => {
196
+ if (!activeKey) return null;
197
+ const item = series.find(entry => entry.key === activeKey);
198
+ if (!item) return null;
199
+ const color = colors[series.indexOf(item)] ?? theme.palette[0];
200
+ return {
201
+ item,
202
+ color,
203
+ payload: {
204
+ label: item.name ?? item.key,
205
+ row: null,
206
+ series: indicators.map((indicator, index) => ({
207
+ key: indicator.name,
208
+ name: indicator.name,
209
+ color,
210
+ value: item.values[index] ?? null,
211
+ formatted: `${item.values[index] ?? 0} / ${indicator.max}`
212
+ }))
213
+ }
214
+ };
215
+ }, [activeKey, series, colors, theme.palette, indicators]);
216
+ const legendItems = series.map((item, index) => ({
217
+ key: item.key,
218
+ name: item.name ?? item.key,
219
+ color: colors[index],
220
+ visible: !hiddenKeys.includes(item.key)
221
+ }));
222
+ const toggle = useCallback(key => {
223
+ setHiddenKeys(prev => prev.includes(key) ? prev.filter(item => item !== key) : [...prev, key]);
224
+ }, []);
225
+ const autoAriaLabel = ariaLabel ?? `雷达图:${indicators.map(item => item.name).join('、')}`;
226
+ return /*#__PURE__*/_jsxs("div", {
227
+ className: classNames(prefixCls('chart'), className),
228
+ style: {
229
+ width,
230
+ height,
231
+ ...style
232
+ },
233
+ "data-aura-chart": "radar",
234
+ children: [/*#__PURE__*/_jsx(Legend, {
235
+ items: legendItems,
236
+ theme: theme,
237
+ config: legend,
238
+ onToggle: toggle
239
+ }), /*#__PURE__*/_jsxs("div", {
240
+ ref: ref,
241
+ className: prefixCls('chart__plot'),
242
+ onPointerMove: event => setActiveKey(findNearest(event.clientX, event.clientY)),
243
+ onPointerLeave: () => setActiveKey(null),
244
+ children: [indicators.length < 3 || visible.length === 0 ? /*#__PURE__*/_jsx("div", {
245
+ className: prefixCls('chart__empty'),
246
+ children: emptyText
247
+ }) : /*#__PURE__*/_jsx(SvgScene, {
248
+ nodes: scene,
249
+ width: size.width,
250
+ height: size.height,
251
+ ariaLabel: autoAriaLabel
252
+ }), /*#__PURE__*/_jsx(Tooltip, {
253
+ payload: active?.payload ?? null,
254
+ x: geometry.cx,
255
+ y: geometry.cy - geometry.radius * 0.6,
256
+ containerWidth: size.width,
257
+ containerHeight: size.height,
258
+ theme: theme,
259
+ config: tooltip
260
+ })]
261
+ })]
262
+ });
263
+ }
@@ -0,0 +1,38 @@
1
+ import React from 'react';
2
+ import { type ChartThemeInput } from '../../theme/index.js';
3
+ export interface RingProgressProps {
4
+ /** 当前值 */
5
+ value: number;
6
+ /** 满值,默认 100 */
7
+ max?: number;
8
+ /** 正方形边长 */
9
+ size?: number;
10
+ /** 环宽(像素) */
11
+ thickness?: number;
12
+ /** 进度颜色,默认取主题强调色 */
13
+ color?: string;
14
+ /** 轨道颜色 */
15
+ trackColor?: string;
16
+ /** 起点角度(度),默认 -90 即正上方 */
17
+ startAngle?: number;
18
+ /** 圆角端点 */
19
+ round?: boolean;
20
+ /** 是否在中心显示百分比,默认 true */
21
+ showPercent?: boolean;
22
+ /** 中心显示的自定义内容,优先级高于 showPercent */
23
+ children?: React.ReactNode;
24
+ theme?: ChartThemeInput;
25
+ palette?: string[];
26
+ animation?: boolean;
27
+ className?: string;
28
+ style?: React.CSSProperties;
29
+ ariaLabel?: string;
30
+ }
31
+ /**
32
+ * 环形进度。
33
+ *
34
+ * 与 GaugeChart 的分工:仪表盘强调「在量程中的位置」(带刻度与端点),
35
+ * 环形进度强调「完成度」(无刻度、更紧凑),常用于表格单元格与卡片角落。
36
+ */
37
+ export declare function RingProgress({ value, max, size, thickness, color, trackColor, startAngle, round, showPercent, children, theme: themeInput, palette, animation, className, style, ariaLabel, }: RingProgressProps): React.JSX.Element;
38
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkB,MAAM,OAAO,CAAC;AAMvC,OAAO,EAAa,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAI9D,OAAO,6BAA6B,CAAC;AAErC,MAAM,WAAW,iBAAiB;IAChC,UAAU;IACV,KAAK,EAAE,MAAM,CAAC;IACd,gBAAgB;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,YAAY;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,aAAa;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oBAAoB;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW;IACX,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0BAA0B;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW;IACX,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,yBAAyB;IACzB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,mCAAmC;IACnC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAID;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,EAC3B,KAAK,EACL,GAAS,EACT,IAAU,EACV,SAAc,EACd,KAAK,EACL,UAAU,EACV,UAAgB,EAChB,KAAY,EACZ,WAAkB,EAClB,QAAQ,EACR,KAAK,EAAE,UAAU,EACjB,OAAO,EACP,SAAgB,EAChB,SAAS,EACT,KAAK,EACL,SAAS,GACV,EAAE,iBAAiB,qBA6GnB"}