@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.
- package/LICENSE +21 -0
- package/README.md +90 -2
- package/esm/charts/area-chart/index.d.ts +19 -0
- package/esm/charts/area-chart/index.d.ts.map +1 -0
- package/esm/charts/area-chart/index.js +31 -0
- package/esm/charts/bar-chart/index.d.ts +16 -0
- package/esm/charts/bar-chart/index.d.ts.map +1 -0
- package/esm/charts/bar-chart/index.js +28 -0
- package/esm/charts/candlestick-chart/index.d.ts +47 -0
- package/esm/charts/candlestick-chart/index.d.ts.map +1 -0
- package/esm/charts/candlestick-chart/index.js +347 -0
- package/esm/charts/combo-chart/index.d.ts +17 -0
- package/esm/charts/combo-chart/index.d.ts.map +1 -0
- package/esm/charts/combo-chart/index.js +26 -0
- package/esm/charts/funnel-chart/index.d.ts +35 -0
- package/esm/charts/funnel-chart/index.d.ts.map +1 -0
- package/esm/charts/funnel-chart/index.js +200 -0
- package/esm/charts/gauge-chart/index.d.ts +41 -0
- package/esm/charts/gauge-chart/index.d.ts.map +1 -0
- package/esm/charts/gauge-chart/index.js +217 -0
- package/esm/charts/graph-chart/index.d.ts +35 -0
- package/esm/charts/graph-chart/index.d.ts.map +1 -0
- package/esm/charts/graph-chart/index.js +224 -0
- package/esm/charts/heatmap-chart/index.d.ts +47 -0
- package/esm/charts/heatmap-chart/index.d.ts.map +1 -0
- package/esm/charts/heatmap-chart/index.js +281 -0
- package/esm/charts/line-chart/index.d.ts +23 -0
- package/esm/charts/line-chart/index.d.ts.map +1 -0
- package/esm/charts/line-chart/index.js +33 -0
- package/esm/charts/pie-chart/index.d.ts +38 -0
- package/esm/charts/pie-chart/index.d.ts.map +1 -0
- package/esm/charts/pie-chart/index.js +272 -0
- package/esm/charts/radar-chart/index.d.ts +44 -0
- package/esm/charts/radar-chart/index.d.ts.map +1 -0
- package/esm/charts/radar-chart/index.js +263 -0
- package/esm/charts/ring-progress/index.d.ts +38 -0
- package/esm/charts/ring-progress/index.d.ts.map +1 -0
- package/esm/charts/ring-progress/index.js +137 -0
- package/esm/charts/sankey-chart/index.d.ts +34 -0
- package/esm/charts/sankey-chart/index.d.ts.map +1 -0
- package/esm/charts/sankey-chart/index.js +248 -0
- package/esm/charts/scatter-chart/index.d.ts +34 -0
- package/esm/charts/scatter-chart/index.d.ts.map +1 -0
- package/esm/charts/scatter-chart/index.js +305 -0
- package/esm/charts/sparkline/index.d.ts +31 -0
- package/esm/charts/sparkline/index.d.ts.map +1 -0
- package/esm/charts/sparkline/index.js +152 -0
- package/esm/charts/tree-chart/index.d.ts +34 -0
- package/esm/charts/tree-chart/index.d.ts.map +1 -0
- package/esm/charts/tree-chart/index.js +200 -0
- package/esm/charts/treemap-chart/index.d.ts +35 -0
- package/esm/charts/treemap-chart/index.d.ts.map +1 -0
- package/esm/charts/treemap-chart/index.js +239 -0
- package/esm/charts/waterfall-chart/index.d.ts +41 -0
- package/esm/charts/waterfall-chart/index.d.ts.map +1 -0
- package/esm/charts/waterfall-chart/index.js +281 -0
- package/esm/components/Legend.d.ts +24 -0
- package/esm/components/Legend.d.ts.map +1 -0
- package/esm/components/Legend.js +50 -0
- package/esm/components/Tooltip.d.ts +24 -0
- package/esm/components/Tooltip.d.ts.map +1 -0
- package/esm/components/Tooltip.js +85 -0
- package/esm/components/chart/index.d.ts +71 -0
- package/esm/components/chart/index.d.ts.map +1 -0
- package/esm/components/chart/index.js +142 -0
- package/esm/components/chart.less +223 -0
- package/esm/core/animation.d.ts +49 -0
- package/esm/core/animation.d.ts.map +1 -0
- package/esm/core/animation.js +93 -0
- package/esm/core/force.d.ts +43 -0
- package/esm/core/force.d.ts.map +1 -0
- package/esm/core/force.js +134 -0
- package/esm/core/format.d.ts +34 -0
- package/esm/core/format.d.ts.map +1 -0
- package/esm/core/format.js +81 -0
- package/esm/core/hierarchy.d.ts +77 -0
- package/esm/core/hierarchy.d.ts.map +1 -0
- package/esm/core/hierarchy.js +233 -0
- package/esm/core/layout.d.ts +67 -0
- package/esm/core/layout.d.ts.map +1 -0
- package/esm/core/layout.js +121 -0
- package/esm/core/sampling.d.ts +32 -0
- package/esm/core/sampling.d.ts.map +1 -0
- package/esm/core/sampling.js +124 -0
- package/esm/core/sankey.d.ts +67 -0
- package/esm/core/sankey.d.ts.map +1 -0
- package/esm/core/sankey.js +136 -0
- package/esm/core/scale.d.ts +98 -0
- package/esm/core/scale.d.ts.map +1 -0
- package/esm/core/scale.js +207 -0
- package/esm/core/shape.d.ts +60 -0
- package/esm/core/shape.d.ts.map +1 -0
- package/esm/core/shape.js +246 -0
- package/esm/global.d.ts +6 -0
- package/esm/hooks/useChartTheme.d.ts +11 -0
- package/esm/hooks/useChartTheme.d.ts.map +1 -0
- package/esm/hooks/useChartTheme.js +35 -0
- package/esm/hooks/useChartTransition.d.ts +20 -0
- package/esm/hooks/useChartTransition.d.ts.map +1 -0
- package/esm/hooks/useChartTransition.js +120 -0
- package/esm/hooks/useElementSize.d.ts +17 -0
- package/esm/hooks/useElementSize.d.ts.map +1 -0
- package/esm/hooks/useElementSize.js +47 -0
- package/esm/index.d.ts +62 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +48 -0
- package/esm/internal/CartesianChart.d.ts +16 -0
- package/esm/internal/CartesianChart.d.ts.map +1 -0
- package/esm/internal/CartesianChart.js +811 -0
- package/esm/internal/model.d.ts +67 -0
- package/esm/internal/model.d.ts.map +1 -0
- package/esm/internal/model.js +146 -0
- package/esm/scene/canvas.d.ts +18 -0
- package/esm/scene/canvas.d.ts.map +1 -0
- package/esm/scene/canvas.js +138 -0
- package/esm/scene/svg.d.ts +12 -0
- package/esm/scene/svg.d.ts.map +1 -0
- package/esm/scene/svg.js +116 -0
- package/esm/scene/types.d.ts +89 -0
- package/esm/scene/types.d.ts.map +1 -0
- package/esm/scene/types.js +21 -0
- package/esm/style.css +196 -0
- package/esm/theme/index.d.ts +127 -0
- package/esm/theme/index.d.ts.map +1 -0
- package/esm/theme/index.js +300 -0
- package/esm/types.d.ts +168 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +2 -0
- package/package.json +64 -3
|
@@ -0,0 +1,305 @@
|
|
|
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 { createLinearScale, extentOf, linearTicks } from "../../core/scale";
|
|
6
|
+
import { computeLayout, suggestTickCount } from "../../core/layout";
|
|
7
|
+
import { formatNumber } from "../../core/format";
|
|
8
|
+
import { symbolPath } from "../../core/shape";
|
|
9
|
+
import { colorAt } from "../../theme";
|
|
10
|
+
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
11
|
+
import { useElementSize } from "../../hooks/useElementSize";
|
|
12
|
+
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
13
|
+
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
14
|
+
import { Legend } from "../../components/Legend";
|
|
15
|
+
import { Tooltip } from "../../components/Tooltip";
|
|
16
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
17
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
18
|
+
/**
|
|
19
|
+
* 散点图。
|
|
20
|
+
*
|
|
21
|
+
* 与折线/柱状不同,散点的 x 是**连续的数值轴**而非分类轴:
|
|
22
|
+
* 分类轴会把 [1, 2, 100] 摆成等间距,严重扭曲相关性判断。
|
|
23
|
+
* 因此这里不复用 CartesianChart 的 band 坐标,改为线性 x 轴。
|
|
24
|
+
*/
|
|
25
|
+
export function ScatterChart({
|
|
26
|
+
data = [],
|
|
27
|
+
xKey = 'x',
|
|
28
|
+
series = [],
|
|
29
|
+
width = '100%',
|
|
30
|
+
height = 320,
|
|
31
|
+
theme: themeInput,
|
|
32
|
+
palette,
|
|
33
|
+
legend = true,
|
|
34
|
+
tooltip = true,
|
|
35
|
+
symbol = 'circle',
|
|
36
|
+
symbolSize = 8,
|
|
37
|
+
animation = true,
|
|
38
|
+
className,
|
|
39
|
+
style,
|
|
40
|
+
ariaLabel,
|
|
41
|
+
emptyText = '暂无数据'
|
|
42
|
+
}) {
|
|
43
|
+
const theme = useChartTheme(themeInput, palette);
|
|
44
|
+
const {
|
|
45
|
+
ref,
|
|
46
|
+
size
|
|
47
|
+
} = useElementSize();
|
|
48
|
+
const [hiddenKeys, setHiddenKeys] = useState([]);
|
|
49
|
+
const [activeKey, setActiveKey] = useState(null);
|
|
50
|
+
const xValues = useMemo(() => data.map(row => {
|
|
51
|
+
const raw = row[xKey];
|
|
52
|
+
const num = typeof raw === 'number' ? raw : Number(raw);
|
|
53
|
+
return Number.isFinite(num) ? num : null;
|
|
54
|
+
}), [data, xKey]);
|
|
55
|
+
const seriesList = useMemo(() => series.map((config, index) => ({
|
|
56
|
+
config,
|
|
57
|
+
key: config.key,
|
|
58
|
+
name: config.name ?? config.key,
|
|
59
|
+
color: config.color ?? colorAt(theme, index),
|
|
60
|
+
values: data.map((row, rowIndex) => {
|
|
61
|
+
const raw = config.data ? config.data[rowIndex] : row[config.key];
|
|
62
|
+
const num = typeof raw === 'number' ? raw : Number(raw);
|
|
63
|
+
return Number.isFinite(num) ? num : null;
|
|
64
|
+
}),
|
|
65
|
+
visible: !config.hidden && !hiddenKeys.includes(config.key)
|
|
66
|
+
})), [series, data, theme, hiddenKeys]);
|
|
67
|
+
const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
|
|
68
|
+
const animConfig = useMemo(() => resolveAnimation(animation),
|
|
69
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
70
|
+
[animKey]);
|
|
71
|
+
|
|
72
|
+
// 每个系列一行数值,更新时在旧值之间插值
|
|
73
|
+
const valueMatrix = useMemo(() => seriesList.map(item => item.values), [seriesList]);
|
|
74
|
+
const transition = useChartTransition(valueMatrix, animConfig);
|
|
75
|
+
const progress = transition.progress;
|
|
76
|
+
const points = useMemo(() => {
|
|
77
|
+
const list = [];
|
|
78
|
+
let seriesIndex = -1;
|
|
79
|
+
for (const item of seriesList) {
|
|
80
|
+
seriesIndex += 1;
|
|
81
|
+
if (!item.visible) continue;
|
|
82
|
+
const row = transition.values[seriesIndex] ?? item.values;
|
|
83
|
+
row.forEach((value, index) => {
|
|
84
|
+
const x = xValues[index];
|
|
85
|
+
if (value === null || x === null || x === undefined) return;
|
|
86
|
+
list.push({
|
|
87
|
+
seriesKey: item.key,
|
|
88
|
+
seriesName: item.name,
|
|
89
|
+
color: item.color,
|
|
90
|
+
x,
|
|
91
|
+
y: value,
|
|
92
|
+
xIndex: index
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return list;
|
|
97
|
+
}, [seriesList, xValues, transition.values]);
|
|
98
|
+
const xDomain = useMemo(() => {
|
|
99
|
+
const [min, max] = extentOf(xValues);
|
|
100
|
+
if (min === max) return [min - 1, max + 1];
|
|
101
|
+
// 两端留 4% 余量,避免点被裁在轴线上
|
|
102
|
+
const pad = (max - min) * 0.04;
|
|
103
|
+
return [min - pad, max + pad];
|
|
104
|
+
}, [xValues]);
|
|
105
|
+
const yDomain = useMemo(() => {
|
|
106
|
+
const [min, max] = extentOf(points.map(point => point.y));
|
|
107
|
+
if (min === max) return [min - 1, max + 1];
|
|
108
|
+
const pad = (max - min) * 0.08;
|
|
109
|
+
return [min - pad, max + pad];
|
|
110
|
+
}, [points]);
|
|
111
|
+
const estimatedPlotHeight = Math.max(60, height - 48);
|
|
112
|
+
const tickCount = suggestTickCount(estimatedPlotHeight);
|
|
113
|
+
const yTicks = useMemo(() => linearTicks(yDomain[0], yDomain[1], tickCount), [yDomain, tickCount]);
|
|
114
|
+
const xTicks = useMemo(() => linearTicks(xDomain[0], xDomain[1], Math.round(tickCount * 1.3)), [xDomain, tickCount]);
|
|
115
|
+
const yLabels = useMemo(() => yTicks.map(tick => formatNumber(tick)), [yTicks]);
|
|
116
|
+
const xLabels = useMemo(() => xTicks.map(tick => formatNumber(tick)), [xTicks]);
|
|
117
|
+
const layout = useMemo(() => computeLayout({
|
|
118
|
+
width: Math.max(size.width, 0),
|
|
119
|
+
height: Math.max(size.height, 0),
|
|
120
|
+
yLabels,
|
|
121
|
+
xLabels,
|
|
122
|
+
theme
|
|
123
|
+
}), [size.width, size.height, yLabels, xLabels, theme]);
|
|
124
|
+
const {
|
|
125
|
+
plot
|
|
126
|
+
} = layout;
|
|
127
|
+
const xScale = useMemo(() => createLinearScale({
|
|
128
|
+
domain: xDomain,
|
|
129
|
+
range: [plot.x, plot.x + plot.width]
|
|
130
|
+
}), [xDomain, plot.x, plot.width]);
|
|
131
|
+
const yScale = useMemo(() => createLinearScale({
|
|
132
|
+
domain: yDomain,
|
|
133
|
+
range: [plot.y + plot.height, plot.y]
|
|
134
|
+
}), [yDomain, plot.y, plot.height]);
|
|
135
|
+
const animBaseY = plot.y + plot.height;
|
|
136
|
+
const scene = useMemo(() => {
|
|
137
|
+
if (plot.width <= 0 || plot.height <= 0) return [];
|
|
138
|
+
const nodes = [];
|
|
139
|
+
for (const tick of yTicks) {
|
|
140
|
+
nodes.push({
|
|
141
|
+
kind: 'line',
|
|
142
|
+
key: `gy-${tick}`,
|
|
143
|
+
x1: plot.x,
|
|
144
|
+
y1: yScale.scale(tick),
|
|
145
|
+
x2: plot.x + plot.width,
|
|
146
|
+
y2: yScale.scale(tick),
|
|
147
|
+
stroke: theme.grid.line,
|
|
148
|
+
strokeWidth: theme.grid.lineWidth,
|
|
149
|
+
strokeDasharray: theme.grid.dash.join(' ') || undefined
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
yTicks.forEach((tick, index) => {
|
|
153
|
+
nodes.push({
|
|
154
|
+
kind: 'text',
|
|
155
|
+
key: `yl-${index}`,
|
|
156
|
+
x: plot.x - theme.axis.tickSize - 4,
|
|
157
|
+
y: yScale.scale(tick),
|
|
158
|
+
text: yLabels[index],
|
|
159
|
+
fill: theme.axis.label,
|
|
160
|
+
fontSize: theme.font.size,
|
|
161
|
+
anchor: 'end',
|
|
162
|
+
baseline: 'middle',
|
|
163
|
+
fontFamily: theme.font.family
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
xTicks.forEach((tick, index) => {
|
|
167
|
+
const x = xScale.scale(tick);
|
|
168
|
+
if (x < plot.x - 1 || x > plot.x + plot.width + 1) return;
|
|
169
|
+
nodes.push({
|
|
170
|
+
kind: 'text',
|
|
171
|
+
key: `xl-${index}`,
|
|
172
|
+
x,
|
|
173
|
+
y: plot.y + plot.height + theme.axis.tickSize + theme.font.size,
|
|
174
|
+
text: xLabels[index],
|
|
175
|
+
fill: theme.axis.label,
|
|
176
|
+
fontSize: theme.font.size,
|
|
177
|
+
anchor: 'middle',
|
|
178
|
+
fontFamily: theme.font.family
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
nodes.push({
|
|
182
|
+
kind: 'line',
|
|
183
|
+
key: 'x-axis',
|
|
184
|
+
x1: plot.x,
|
|
185
|
+
y1: plot.y + plot.height,
|
|
186
|
+
x2: plot.x + plot.width,
|
|
187
|
+
y2: plot.y + plot.height,
|
|
188
|
+
stroke: theme.axis.line,
|
|
189
|
+
strokeWidth: theme.axis.lineWidth
|
|
190
|
+
});
|
|
191
|
+
nodes.push({
|
|
192
|
+
kind: 'line',
|
|
193
|
+
key: 'y-axis',
|
|
194
|
+
x1: plot.x,
|
|
195
|
+
y1: plot.y,
|
|
196
|
+
x2: plot.x,
|
|
197
|
+
y2: plot.y + plot.height,
|
|
198
|
+
stroke: theme.axis.line,
|
|
199
|
+
strokeWidth: theme.axis.lineWidth
|
|
200
|
+
});
|
|
201
|
+
points.forEach((point, pointIndex) => {
|
|
202
|
+
const cx = xScale.scale(point.x);
|
|
203
|
+
const targetY = yScale.scale(point.y);
|
|
204
|
+
// 逐点错峰 + 透明度淡入,避免整片点同时「砸」在基线上
|
|
205
|
+
const local = staggerProgress(progress, pointIndex, points.length, animConfig.stagger);
|
|
206
|
+
const cy = animBaseY + (targetY - animBaseY) * local;
|
|
207
|
+
const isActive = activeKey === `${point.seriesKey}-${point.xIndex}`;
|
|
208
|
+
const radius = isActive ? symbolSize * 0.75 : symbolSize * 0.55;
|
|
209
|
+
nodes.push({
|
|
210
|
+
kind: 'path',
|
|
211
|
+
key: `pt-${point.seriesKey}-${point.xIndex}`,
|
|
212
|
+
d: symbolPath(symbol, cx, cy, radius * 2),
|
|
213
|
+
fill: point.color,
|
|
214
|
+
opacity: (activeKey === null || isActive ? 0.85 : 0.35) * local,
|
|
215
|
+
pointerEvents: 'none'
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
return nodes;
|
|
219
|
+
}, [plot, yTicks, xTicks, yLabels, xLabels, yScale, xScale, points, theme, progress, animBaseY, activeKey, symbol, symbolSize, animConfig.stagger]);
|
|
220
|
+
|
|
221
|
+
/** 指针 → 最近的点(限定 28px 内,避免远处空白也触发) */
|
|
222
|
+
const findNearest = useCallback((clientX, clientY) => {
|
|
223
|
+
const element = ref.current;
|
|
224
|
+
if (!element || !points.length) return null;
|
|
225
|
+
const rect = element.getBoundingClientRect();
|
|
226
|
+
const localX = clientX - rect.left;
|
|
227
|
+
const localY = clientY - rect.top;
|
|
228
|
+
let best = null;
|
|
229
|
+
let minDistance = 28;
|
|
230
|
+
for (const point of points) {
|
|
231
|
+
const px = xScale.scale(point.x);
|
|
232
|
+
const py = yScale.scale(point.y);
|
|
233
|
+
const distance = Math.hypot(px - localX, py - localY);
|
|
234
|
+
if (distance < minDistance) {
|
|
235
|
+
minDistance = distance;
|
|
236
|
+
best = point;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return best;
|
|
240
|
+
}, [ref, points, xScale, yScale]);
|
|
241
|
+
const activePoint = useMemo(() => {
|
|
242
|
+
if (!activeKey) return null;
|
|
243
|
+
return points.find(point => `${point.seriesKey}-${point.xIndex}` === activeKey) ?? null;
|
|
244
|
+
}, [activeKey, points]);
|
|
245
|
+
const tooltipPayload = activePoint ? {
|
|
246
|
+
label: `${xKey}: ${formatNumber(activePoint.x)}`,
|
|
247
|
+
row: data[activePoint.xIndex] ?? null,
|
|
248
|
+
series: [{
|
|
249
|
+
key: activePoint.seriesKey,
|
|
250
|
+
name: activePoint.seriesName,
|
|
251
|
+
color: activePoint.color,
|
|
252
|
+
value: activePoint.y,
|
|
253
|
+
formatted: formatNumber(activePoint.y)
|
|
254
|
+
}]
|
|
255
|
+
} : null;
|
|
256
|
+
const legendItems = seriesList.map(item => ({
|
|
257
|
+
key: item.key,
|
|
258
|
+
name: item.name,
|
|
259
|
+
color: item.color,
|
|
260
|
+
visible: item.visible
|
|
261
|
+
}));
|
|
262
|
+
const toggle = useCallback(key => {
|
|
263
|
+
setHiddenKeys(prev => prev.includes(key) ? prev.filter(item => item !== key) : [...prev, key]);
|
|
264
|
+
}, []);
|
|
265
|
+
const autoAriaLabel = ariaLabel ?? `散点图:${seriesList.map(item => item.name).join('、')}`;
|
|
266
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
267
|
+
className: classNames(prefixCls('chart'), className),
|
|
268
|
+
style: {
|
|
269
|
+
width,
|
|
270
|
+
height,
|
|
271
|
+
...style
|
|
272
|
+
},
|
|
273
|
+
"data-aura-chart": "scatter",
|
|
274
|
+
children: [/*#__PURE__*/_jsx(Legend, {
|
|
275
|
+
items: legendItems,
|
|
276
|
+
theme: theme,
|
|
277
|
+
config: legend,
|
|
278
|
+
onToggle: toggle
|
|
279
|
+
}), /*#__PURE__*/_jsxs("div", {
|
|
280
|
+
ref: ref,
|
|
281
|
+
className: prefixCls('chart__plot'),
|
|
282
|
+
onPointerMove: event => {
|
|
283
|
+
const nearest = findNearest(event.clientX, event.clientY);
|
|
284
|
+
setActiveKey(nearest ? `${nearest.seriesKey}-${nearest.xIndex}` : null);
|
|
285
|
+
},
|
|
286
|
+
onPointerLeave: () => setActiveKey(null),
|
|
287
|
+
children: [points.length === 0 ? /*#__PURE__*/_jsx("div", {
|
|
288
|
+
className: prefixCls('chart__empty'),
|
|
289
|
+
children: emptyText
|
|
290
|
+
}) : /*#__PURE__*/_jsx(SvgScene, {
|
|
291
|
+
nodes: scene,
|
|
292
|
+
width: size.width,
|
|
293
|
+
height: size.height,
|
|
294
|
+
ariaLabel: autoAriaLabel
|
|
295
|
+
}), /*#__PURE__*/_jsx(Tooltip, {
|
|
296
|
+
payload: tooltipPayload,
|
|
297
|
+
x: activePoint ? xScale.scale(activePoint.x) : 0,
|
|
298
|
+
y: activePoint ? yScale.scale(activePoint.y) : 0,
|
|
299
|
+
containerWidth: size.width,
|
|
300
|
+
theme: theme,
|
|
301
|
+
config: tooltip
|
|
302
|
+
})]
|
|
303
|
+
})]
|
|
304
|
+
});
|
|
305
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type ChartThemeInput } from '../../theme/index.js';
|
|
3
|
+
export interface SparklineProps {
|
|
4
|
+
data: number[];
|
|
5
|
+
type?: 'line' | 'area' | 'bar';
|
|
6
|
+
width?: number;
|
|
7
|
+
height?: number;
|
|
8
|
+
/** 线条 / 柱子颜色,默认主题强调色 */
|
|
9
|
+
color?: string;
|
|
10
|
+
/** 曲线是否平滑 */
|
|
11
|
+
smooth?: boolean;
|
|
12
|
+
/** 是否高亮最后一个数据点 */
|
|
13
|
+
showLast?: boolean;
|
|
14
|
+
strokeWidth?: number;
|
|
15
|
+
theme?: ChartThemeInput;
|
|
16
|
+
palette?: string[];
|
|
17
|
+
animation?: boolean;
|
|
18
|
+
className?: string;
|
|
19
|
+
style?: React.CSSProperties;
|
|
20
|
+
ariaLabel?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 迷你图(Sparkline)。
|
|
24
|
+
*
|
|
25
|
+
* 没有坐标轴、没有图例、没有 tooltip —— 它的唯一职责是让读者在扫视表格时
|
|
26
|
+
* 立刻看出「这条数据是在涨还是在跌」。任何额外装饰都会削弱这个目的。
|
|
27
|
+
*
|
|
28
|
+
* 因此它也刻意不提供数值精度:需要读数请用折线图。
|
|
29
|
+
*/
|
|
30
|
+
export declare function Sparkline({ data, type, width, height, color, smooth, showLast, strokeWidth, theme: themeInput, palette, animation, className, style, ariaLabel, }: SparklineProps): React.JSX.Element | null;
|
|
31
|
+
//# 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,cAAc;IAC7B,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,wBAAwB;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,aAAa;IACb,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,kBAAkB;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,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;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,EACxB,IAAS,EACT,IAAa,EACb,KAAW,EACX,MAAW,EACX,KAAK,EACL,MAAc,EACd,QAAgB,EAChB,WAAiB,EACjB,KAAK,EAAE,UAAU,EACjB,OAAO,EACP,SAAgB,EAChB,SAAS,EACT,KAAK,EACL,SAAS,GACV,EAAE,cAAc,4BAwJhB"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React, { useMemo } from 'react';
|
|
3
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
4
|
+
import { SvgScene } from "../../scene/svg";
|
|
5
|
+
import { areaPath, linePath } from "../../core/shape";
|
|
6
|
+
import { extentOf } from "../../core/scale";
|
|
7
|
+
import { withAlpha } from "../../theme";
|
|
8
|
+
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
9
|
+
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
10
|
+
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
11
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
|
+
/**
|
|
13
|
+
* 迷你图(Sparkline)。
|
|
14
|
+
*
|
|
15
|
+
* 没有坐标轴、没有图例、没有 tooltip —— 它的唯一职责是让读者在扫视表格时
|
|
16
|
+
* 立刻看出「这条数据是在涨还是在跌」。任何额外装饰都会削弱这个目的。
|
|
17
|
+
*
|
|
18
|
+
* 因此它也刻意不提供数值精度:需要读数请用折线图。
|
|
19
|
+
*/
|
|
20
|
+
export function Sparkline({
|
|
21
|
+
data = [],
|
|
22
|
+
type = 'line',
|
|
23
|
+
width = 120,
|
|
24
|
+
height = 32,
|
|
25
|
+
color,
|
|
26
|
+
smooth = false,
|
|
27
|
+
showLast = false,
|
|
28
|
+
strokeWidth = 1.6,
|
|
29
|
+
theme: themeInput,
|
|
30
|
+
palette,
|
|
31
|
+
animation = true,
|
|
32
|
+
className,
|
|
33
|
+
style,
|
|
34
|
+
ariaLabel
|
|
35
|
+
}) {
|
|
36
|
+
const theme = useChartTheme(themeInput, palette);
|
|
37
|
+
const accent = color ?? theme.emphasis;
|
|
38
|
+
const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
|
|
39
|
+
const animConfig = useMemo(() => resolveAnimation(animation),
|
|
40
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
41
|
+
[animKey]);
|
|
42
|
+
const transition = useChartTransition([data], animConfig);
|
|
43
|
+
const progress = transition.progress;
|
|
44
|
+
const values = transition.values[0] ?? data;
|
|
45
|
+
const points = useMemo(() => {
|
|
46
|
+
if (!values.length) return [];
|
|
47
|
+
const [min, max] = extentOf(values);
|
|
48
|
+
const span = max - min || 1;
|
|
49
|
+
const pad = strokeWidth;
|
|
50
|
+
const innerWidth = Math.max(1, width - pad);
|
|
51
|
+
const innerHeight = Math.max(1, height - pad * 2);
|
|
52
|
+
return values.map((raw, index) => {
|
|
53
|
+
const value = raw ?? 0;
|
|
54
|
+
return {
|
|
55
|
+
x: values.length === 1 ? width / 2 : pad / 2 + index / (values.length - 1) * innerWidth,
|
|
56
|
+
// y 轴翻转:数值越大越靠上
|
|
57
|
+
y: pad + innerHeight - (value - min) / span * innerHeight,
|
|
58
|
+
value
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}, [values, width, height, strokeWidth]);
|
|
62
|
+
const scene = useMemo(() => {
|
|
63
|
+
if (!points.length) return [];
|
|
64
|
+
const nodes = [];
|
|
65
|
+
const curve = smooth ? 'smooth' : 'linear';
|
|
66
|
+
if (type === 'bar') {
|
|
67
|
+
const slot = width / Math.max(points.length, 1);
|
|
68
|
+
const barWidth = Math.max(1, slot * 0.6);
|
|
69
|
+
const bottom = height - strokeWidth / 2;
|
|
70
|
+
points.forEach((point, index) => {
|
|
71
|
+
const local = staggerProgress(progress, index, points.length, animConfig.stagger);
|
|
72
|
+
const barHeight = Math.max(1, (bottom - point.y) * local);
|
|
73
|
+
nodes.push({
|
|
74
|
+
kind: 'rect',
|
|
75
|
+
key: `bar-${index}`,
|
|
76
|
+
x: point.x - barWidth / 2,
|
|
77
|
+
y: bottom - barHeight,
|
|
78
|
+
width: barWidth,
|
|
79
|
+
height: barHeight,
|
|
80
|
+
radius: 1,
|
|
81
|
+
fill: accent,
|
|
82
|
+
opacity: 0.85,
|
|
83
|
+
pointerEvents: 'none'
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
return nodes;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 动画:进场时按 progress 从底部抬升
|
|
90
|
+
// 逐点错峰:线条像被画出,而不是整体从底部抬起
|
|
91
|
+
const animated = points.map((point, index) => {
|
|
92
|
+
const local = staggerProgress(progress, index, points.length, animConfig.stagger);
|
|
93
|
+
return {
|
|
94
|
+
x: point.x,
|
|
95
|
+
y: height + (point.y - height) * local
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
if (type === 'area') {
|
|
99
|
+
nodes.push({
|
|
100
|
+
kind: 'path',
|
|
101
|
+
key: 'area',
|
|
102
|
+
d: areaPath(animated, height, curve),
|
|
103
|
+
fill: withAlpha(accent, 0.16),
|
|
104
|
+
pointerEvents: 'none'
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
nodes.push({
|
|
108
|
+
kind: 'path',
|
|
109
|
+
key: 'line',
|
|
110
|
+
d: linePath(animated, curve),
|
|
111
|
+
fill: 'none',
|
|
112
|
+
stroke: accent,
|
|
113
|
+
strokeWidth,
|
|
114
|
+
linecap: 'round',
|
|
115
|
+
linejoin: 'round',
|
|
116
|
+
pointerEvents: 'none'
|
|
117
|
+
});
|
|
118
|
+
if (showLast) {
|
|
119
|
+
const last = animated[animated.length - 1];
|
|
120
|
+
nodes.push({
|
|
121
|
+
kind: 'circle',
|
|
122
|
+
key: 'last',
|
|
123
|
+
cx: last.x,
|
|
124
|
+
cy: last.y,
|
|
125
|
+
r: strokeWidth + 1.2,
|
|
126
|
+
fill: accent,
|
|
127
|
+
strokeWidth: 0,
|
|
128
|
+
pointerEvents: 'none'
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return nodes;
|
|
132
|
+
}, [points, type, width, height, accent, strokeWidth, progress, showLast, smooth, animConfig.stagger]);
|
|
133
|
+
if (!data.length) return null;
|
|
134
|
+
return /*#__PURE__*/_jsx("div", {
|
|
135
|
+
className: classNames(prefixCls('chart'), className),
|
|
136
|
+
style: {
|
|
137
|
+
width,
|
|
138
|
+
height,
|
|
139
|
+
...style
|
|
140
|
+
},
|
|
141
|
+
"data-aura-chart": "sparkline",
|
|
142
|
+
children: /*#__PURE__*/_jsx("div", {
|
|
143
|
+
className: prefixCls('chart__plot'),
|
|
144
|
+
children: /*#__PURE__*/_jsx(SvgScene, {
|
|
145
|
+
nodes: scene,
|
|
146
|
+
width: width,
|
|
147
|
+
height: height,
|
|
148
|
+
ariaLabel: ariaLabel ?? `迷你趋势图,共 ${data.length} 个数据点`
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
});
|
|
152
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type HierarchyNode } from '../../core/hierarchy.js';
|
|
3
|
+
import { type ChartThemeInput } from '../../theme/index.js';
|
|
4
|
+
import type { ChartAnimationInput, TooltipConfig } from '../../types.js';
|
|
5
|
+
export interface TreeChartProps {
|
|
6
|
+
data?: HierarchyNode;
|
|
7
|
+
/** 节点圆点半径 */
|
|
8
|
+
nodeSize?: number;
|
|
9
|
+
/** 是否显示节点数值 */
|
|
10
|
+
showValue?: boolean;
|
|
11
|
+
/** 叶子到右边界预留的标签宽度 */
|
|
12
|
+
labelSpace?: number;
|
|
13
|
+
width?: number | string;
|
|
14
|
+
height?: number;
|
|
15
|
+
theme?: ChartThemeInput;
|
|
16
|
+
palette?: string[];
|
|
17
|
+
tooltip?: boolean | TooltipConfig;
|
|
18
|
+
animation?: ChartAnimationInput;
|
|
19
|
+
className?: string;
|
|
20
|
+
style?: React.CSSProperties;
|
|
21
|
+
ariaLabel?: string;
|
|
22
|
+
emptyText?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 树图(横向展开)。
|
|
26
|
+
*
|
|
27
|
+
* 与矩形树图的分工:矩形树图用面积编码数值、回答「谁占得多」;
|
|
28
|
+
* 树图只表达层级结构与从属关系,节点大小恒定。
|
|
29
|
+
*
|
|
30
|
+
* 纵向位置按叶子顺序均分,父节点取子节点均值 —— 这样连线不交叉,
|
|
31
|
+
* 同一父节点下的兄弟会自然聚成一簇。
|
|
32
|
+
*/
|
|
33
|
+
export declare function TreeChart({ data, nodeSize, showValue, labelSpace, width, height, theme: themeInput, palette, tooltip, animation, className, style, ariaLabel, emptyText, }: TreeChartProps): React.JSX.Element;
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AAKjD,OAAO,EAKL,KAAK,aAAa,EAEnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAa,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAM9D,OAAO,KAAK,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,6BAA6B,CAAC;AAErC,MAAM,WAAW,cAAc;IAC7B,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,aAAa;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,eAAe;IACf,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,oBAAoB;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,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,OAAO,CAAC,EAAE,OAAO,GAAG,aAAa,CAAC;IAClC,SAAS,CAAC,EAAE,mBAAmB,CAAC;IAChC,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;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,EACxB,IAAI,EACJ,QAAY,EACZ,SAAiB,EACjB,UAAe,EACf,KAAc,EACd,MAAY,EACZ,KAAK,EAAE,UAAU,EACjB,OAAO,EACP,OAAc,EACd,SAAgB,EAChB,SAAS,EACT,KAAK,EACL,SAAS,EACT,SAAkB,GACnB,EAAE,cAAc,qBA4MhB"}
|