@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,137 @@
|
|
|
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 { arcLinePath } from "../../core/shape";
|
|
6
|
+
import { formatPercent } from "../../core/format";
|
|
7
|
+
import { withAlpha } from "../../theme";
|
|
8
|
+
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
9
|
+
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
10
|
+
import { resolveAnimation } from "../../core/animation";
|
|
11
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
|
+
const DEG = Math.PI / 180;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 环形进度。
|
|
17
|
+
*
|
|
18
|
+
* 与 GaugeChart 的分工:仪表盘强调「在量程中的位置」(带刻度与端点),
|
|
19
|
+
* 环形进度强调「完成度」(无刻度、更紧凑),常用于表格单元格与卡片角落。
|
|
20
|
+
*/
|
|
21
|
+
export function RingProgress({
|
|
22
|
+
value,
|
|
23
|
+
max = 100,
|
|
24
|
+
size = 160,
|
|
25
|
+
thickness = 12,
|
|
26
|
+
color,
|
|
27
|
+
trackColor,
|
|
28
|
+
startAngle = -90,
|
|
29
|
+
round = true,
|
|
30
|
+
showPercent = true,
|
|
31
|
+
children,
|
|
32
|
+
theme: themeInput,
|
|
33
|
+
palette,
|
|
34
|
+
animation = true,
|
|
35
|
+
className,
|
|
36
|
+
style,
|
|
37
|
+
ariaLabel
|
|
38
|
+
}) {
|
|
39
|
+
const theme = useChartTheme(themeInput, palette);
|
|
40
|
+
const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
|
|
41
|
+
const animConfig = useMemo(() => resolveAnimation(animation),
|
|
42
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
43
|
+
[animKey]);
|
|
44
|
+
const transition = useChartTransition([[value]], animConfig);
|
|
45
|
+
const progress = transition.progress;
|
|
46
|
+
const animatedValue = transition.values[0]?.[0] ?? value;
|
|
47
|
+
const ratio = useMemo(() => {
|
|
48
|
+
if (max === 0) return 0;
|
|
49
|
+
return Math.min(1, Math.max(0, animatedValue / max));
|
|
50
|
+
}, [animatedValue, max]);
|
|
51
|
+
const accent = color ?? theme.emphasis;
|
|
52
|
+
const track = trackColor ?? withAlpha(theme.axis.line, 0.6);
|
|
53
|
+
const geometry = useMemo(() => {
|
|
54
|
+
const center = size / 2;
|
|
55
|
+
const radius = Math.max(0, center - thickness / 2 - 1);
|
|
56
|
+
return {
|
|
57
|
+
cx: center,
|
|
58
|
+
cy: center,
|
|
59
|
+
radius,
|
|
60
|
+
arc: radius * 2 * Math.PI
|
|
61
|
+
};
|
|
62
|
+
}, [size, thickness]);
|
|
63
|
+
const start = startAngle * DEG;
|
|
64
|
+
const end = start + Math.PI * 2 * ratio * progress;
|
|
65
|
+
const scene = useMemo(() => {
|
|
66
|
+
const {
|
|
67
|
+
cx,
|
|
68
|
+
cy,
|
|
69
|
+
radius
|
|
70
|
+
} = geometry;
|
|
71
|
+
if (radius <= 0) return [];
|
|
72
|
+
const nodes = [];
|
|
73
|
+
nodes.push({
|
|
74
|
+
kind: 'path',
|
|
75
|
+
key: 'track',
|
|
76
|
+
d: arcLinePath(cx, cy, radius, 0, Math.PI * 2),
|
|
77
|
+
fill: 'none',
|
|
78
|
+
stroke: track,
|
|
79
|
+
strokeWidth: thickness,
|
|
80
|
+
pointerEvents: 'none'
|
|
81
|
+
});
|
|
82
|
+
if (end > start + 0.0001) {
|
|
83
|
+
nodes.push({
|
|
84
|
+
kind: 'path',
|
|
85
|
+
key: 'value',
|
|
86
|
+
d: arcLinePath(cx, cy, radius, start, end),
|
|
87
|
+
fill: 'none',
|
|
88
|
+
stroke: accent,
|
|
89
|
+
strokeWidth: thickness,
|
|
90
|
+
linecap: round ? 'round' : 'butt',
|
|
91
|
+
pointerEvents: 'none'
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return nodes;
|
|
95
|
+
}, [geometry, start, end, track, accent, thickness, round]);
|
|
96
|
+
const autoAriaLabel = ariaLabel ?? `进度:${Math.round(ratio * 100)}%`;
|
|
97
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
98
|
+
className: classNames(prefixCls('chart'), className),
|
|
99
|
+
style: {
|
|
100
|
+
width: size,
|
|
101
|
+
height: size,
|
|
102
|
+
position: 'relative',
|
|
103
|
+
...style
|
|
104
|
+
},
|
|
105
|
+
"data-aura-chart": "ring-progress",
|
|
106
|
+
role: "img",
|
|
107
|
+
"aria-label": autoAriaLabel,
|
|
108
|
+
children: [/*#__PURE__*/_jsx("div", {
|
|
109
|
+
className: prefixCls('chart__plot'),
|
|
110
|
+
children: /*#__PURE__*/_jsx(SvgScene, {
|
|
111
|
+
nodes: scene,
|
|
112
|
+
width: size,
|
|
113
|
+
height: size,
|
|
114
|
+
ariaLabel: autoAriaLabel
|
|
115
|
+
})
|
|
116
|
+
}), /*#__PURE__*/_jsx("div", {
|
|
117
|
+
style: {
|
|
118
|
+
position: 'absolute',
|
|
119
|
+
inset: 0,
|
|
120
|
+
display: 'flex',
|
|
121
|
+
flexDirection: 'column',
|
|
122
|
+
alignItems: 'center',
|
|
123
|
+
justifyContent: 'center',
|
|
124
|
+
pointerEvents: 'none'
|
|
125
|
+
},
|
|
126
|
+
children: children ?? (showPercent ? /*#__PURE__*/_jsx("span", {
|
|
127
|
+
style: {
|
|
128
|
+
fontSize: Math.max(14, size * 0.16),
|
|
129
|
+
fontWeight: theme.font.weightMedium,
|
|
130
|
+
color: theme.text.primary,
|
|
131
|
+
fontVariantNumeric: 'tabular-nums'
|
|
132
|
+
},
|
|
133
|
+
children: formatPercent(value, max || 1, 0)
|
|
134
|
+
}) : null)
|
|
135
|
+
})]
|
|
136
|
+
});
|
|
137
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type ChartThemeInput } from '../../theme/index.js';
|
|
3
|
+
import { type SankeyLinkInput, type SankeyNode } from '../../core/sankey.js';
|
|
4
|
+
import type { ChartAnimationInput, TooltipConfig } from '../../types.js';
|
|
5
|
+
export interface SankeyChartProps {
|
|
6
|
+
/** 节点列表;只传 links 时会自动从 links 推导 */
|
|
7
|
+
nodes?: SankeyNode[];
|
|
8
|
+
links?: SankeyLinkInput[];
|
|
9
|
+
/** 节点矩形宽度 */
|
|
10
|
+
nodeWidth?: number;
|
|
11
|
+
/** 同层节点竖向间隙 */
|
|
12
|
+
nodeGap?: number;
|
|
13
|
+
/** 是否在节点旁显示流量数值 */
|
|
14
|
+
showValue?: boolean;
|
|
15
|
+
width?: number | string;
|
|
16
|
+
height?: number;
|
|
17
|
+
theme?: ChartThemeInput;
|
|
18
|
+
palette?: string[];
|
|
19
|
+
tooltip?: boolean | TooltipConfig;
|
|
20
|
+
animation?: ChartAnimationInput;
|
|
21
|
+
className?: string;
|
|
22
|
+
style?: React.CSSProperties;
|
|
23
|
+
ariaLabel?: string;
|
|
24
|
+
emptyText?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 桑基图。
|
|
28
|
+
*
|
|
29
|
+
* 用带宽编码流量,用节点位置编码阶段,一眼能看出「东西从哪来、到哪去、路上漏了多少」。
|
|
30
|
+
*
|
|
31
|
+
* 与漏斗图的区别:漏斗只能表达单链路层层递减,桑基能表达分流与合流。
|
|
32
|
+
*/
|
|
33
|
+
export declare function SankeyChart({ nodes: nodesProp, links, nodeWidth, nodeGap, showValue, width, height, theme: themeInput, palette, tooltip, animation, className, style, ariaLabel, emptyText, }: SankeyChartProps): React.JSX.Element;
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1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EACL,SAAS,EACT,SAAkB,GACnB,EAAE,gBAAgB,qBA8RlB"}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React, { useMemo, useState } from 'react';
|
|
3
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
4
|
+
import { SvgScene } from "../../scene/svg";
|
|
5
|
+
import { formatNumber, formatPercent, estimateTextWidth } from "../../core/format";
|
|
6
|
+
import { withAlpha } from "../../theme";
|
|
7
|
+
import { layoutSankey } from "../../core/sankey";
|
|
8
|
+
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
9
|
+
import { useElementSize } from "../../hooks/useElementSize";
|
|
10
|
+
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
11
|
+
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
12
|
+
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 SankeyChart({
|
|
23
|
+
nodes: nodesProp,
|
|
24
|
+
links = [],
|
|
25
|
+
nodeWidth = 12,
|
|
26
|
+
nodeGap = 10,
|
|
27
|
+
showValue = true,
|
|
28
|
+
width = '100%',
|
|
29
|
+
height = 360,
|
|
30
|
+
theme: themeInput,
|
|
31
|
+
palette,
|
|
32
|
+
tooltip = true,
|
|
33
|
+
animation = true,
|
|
34
|
+
className,
|
|
35
|
+
style,
|
|
36
|
+
ariaLabel,
|
|
37
|
+
emptyText = '暂无数据'
|
|
38
|
+
}) {
|
|
39
|
+
const theme = useChartTheme(themeInput, palette);
|
|
40
|
+
const {
|
|
41
|
+
ref,
|
|
42
|
+
size
|
|
43
|
+
} = useElementSize();
|
|
44
|
+
const [activeLink, setActiveLink] = useState(null);
|
|
45
|
+
const [activeNode, setActiveNode] = 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]);
|
|
50
|
+
|
|
51
|
+
/** 只给 links 时,按出现顺序补出节点 */
|
|
52
|
+
const nodes = useMemo(() => {
|
|
53
|
+
if (nodesProp?.length) return nodesProp;
|
|
54
|
+
const seen = new Set();
|
|
55
|
+
const derived = [];
|
|
56
|
+
for (const link of links) {
|
|
57
|
+
if (!seen.has(link.source)) {
|
|
58
|
+
seen.add(link.source);
|
|
59
|
+
derived.push({
|
|
60
|
+
name: link.source
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (!seen.has(link.target)) {
|
|
64
|
+
seen.add(link.target);
|
|
65
|
+
derived.push({
|
|
66
|
+
name: link.target
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return derived;
|
|
71
|
+
}, [nodesProp, links]);
|
|
72
|
+
const total = useMemo(() => links.reduce((sum, link) => sum + (link.value || 0), 0) || 1, [links]);
|
|
73
|
+
|
|
74
|
+
// 左右各留出标签空间
|
|
75
|
+
const labelSpace = 92;
|
|
76
|
+
const layout = useMemo(() => layoutSankey(nodes, links, {
|
|
77
|
+
x: labelSpace,
|
|
78
|
+
y: 12,
|
|
79
|
+
width: Math.max(0, size.width - labelSpace * 2),
|
|
80
|
+
height: Math.max(0, size.height - 24)
|
|
81
|
+
}, {
|
|
82
|
+
nodeWidth,
|
|
83
|
+
nodeGap,
|
|
84
|
+
colorOf: (layer, index) => theme.palette[(layer * 2 + index) % theme.palette.length]
|
|
85
|
+
}), [nodes, links, size.width, size.height, nodeWidth, nodeGap, theme.palette]);
|
|
86
|
+
const valueMatrix = useMemo(() => [layout.links.map(link => Math.round(link.value))], [layout.links]);
|
|
87
|
+
const progress = useChartTransition(valueMatrix, animConfig).progress;
|
|
88
|
+
const scene = useMemo(() => {
|
|
89
|
+
if (!layout.nodes.length || size.width <= 0) return [];
|
|
90
|
+
const nodes = [];
|
|
91
|
+
|
|
92
|
+
// 链接先画,节点压在上面
|
|
93
|
+
layout.links.forEach((link, index) => {
|
|
94
|
+
const local = staggerProgress(progress, link.source.layer, layout.layers, animConfig.stagger);
|
|
95
|
+
const isActive = activeLink?.source.name === link.source.name && activeLink?.target.name === link.target.name && activeLink.value === link.value;
|
|
96
|
+
nodes.push({
|
|
97
|
+
kind: 'path',
|
|
98
|
+
key: `link-${index}`,
|
|
99
|
+
d: link.d,
|
|
100
|
+
fill: withAlpha(link.color, isActive ? 0.55 : 0.26),
|
|
101
|
+
opacity: local,
|
|
102
|
+
pointerEvents: 'none'
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
layout.nodes.forEach((node, index) => {
|
|
106
|
+
const local = staggerProgress(progress, node.layer, layout.layers, animConfig.stagger);
|
|
107
|
+
const isActive = activeNode === node.name;
|
|
108
|
+
nodes.push({
|
|
109
|
+
kind: 'rect',
|
|
110
|
+
key: `node-${node.name}`,
|
|
111
|
+
x: node.x,
|
|
112
|
+
y: node.y,
|
|
113
|
+
width: node.width,
|
|
114
|
+
height: Math.max(2, node.height * local),
|
|
115
|
+
radius: 2,
|
|
116
|
+
fill: node.color,
|
|
117
|
+
stroke: isActive ? theme.text.primary : undefined,
|
|
118
|
+
strokeWidth: isActive ? 2 : 0,
|
|
119
|
+
pointerEvents: 'none'
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// 首尾两列标签放节点外侧,中间列放内侧;内侧会被色带压住,
|
|
123
|
+
// 所以文字统一带白色光晕
|
|
124
|
+
const isLastLayer = node.layer === layout.layers - 1;
|
|
125
|
+
const onLeft = node.layer === 0 || isLastLayer;
|
|
126
|
+
const labelX = onLeft ? node.x - 8 : node.x + node.width + 8;
|
|
127
|
+
const maxLabelWidth = onLeft ? labelX - 4 : size.width - labelX - 4;
|
|
128
|
+
if (maxLabelWidth < 24) return;
|
|
129
|
+
const label = showValue && node.layer === layout.layers - 1 ? `${node.name} ${formatNumber(node.value, {
|
|
130
|
+
compact: true
|
|
131
|
+
})}` : node.name;
|
|
132
|
+
const text = estimateTextWidth(label, theme.font.size) > maxLabelWidth ? `${label.slice(0, Math.max(2, Math.floor(maxLabelWidth / theme.font.size)))}…` : label;
|
|
133
|
+
nodes.push({
|
|
134
|
+
kind: 'text',
|
|
135
|
+
key: `node-label-${node.name}-${index}`,
|
|
136
|
+
x: labelX,
|
|
137
|
+
y: node.y + node.height / 2,
|
|
138
|
+
text,
|
|
139
|
+
fill: isActive ? theme.text.primary : theme.text.secondary,
|
|
140
|
+
fontSize: theme.font.size,
|
|
141
|
+
anchor: onLeft ? 'end' : 'start',
|
|
142
|
+
baseline: 'middle',
|
|
143
|
+
opacity: local,
|
|
144
|
+
fontFamily: theme.font.family,
|
|
145
|
+
// 光晕:文字压在色带上也读得清
|
|
146
|
+
stroke: theme.tooltip.background,
|
|
147
|
+
strokeWidth: 3
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
return nodes;
|
|
151
|
+
}, [layout, size.width, theme, progress, animConfig.stagger, activeLink, activeNode, showValue]);
|
|
152
|
+
const linkFromPointer = (clientX, clientY) => {
|
|
153
|
+
const element = ref.current;
|
|
154
|
+
if (!element) return null;
|
|
155
|
+
const rect = element.getBoundingClientRect();
|
|
156
|
+
const x = clientX - rect.left;
|
|
157
|
+
const y = clientY - rect.top;
|
|
158
|
+
|
|
159
|
+
// 节点优先
|
|
160
|
+
for (const node of layout.nodes) {
|
|
161
|
+
if (x >= node.x - 2 && x <= node.x + node.width + 2 && y >= node.y && y <= node.y + node.height) {
|
|
162
|
+
setActiveNode(node.name);
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
setActiveNode(null);
|
|
167
|
+
|
|
168
|
+
// 链接:按 x 在两端之间做线性插值,得到该处的带状区间
|
|
169
|
+
for (const link of layout.links) {
|
|
170
|
+
const x0 = link.source.x + link.source.width;
|
|
171
|
+
const x1 = link.target.x;
|
|
172
|
+
if (x < x0 || x > x1 || x1 <= x0) continue;
|
|
173
|
+
const ratio = (x - x0) / (x1 - x0);
|
|
174
|
+
const top = link.sourceY + (link.targetY - link.sourceY) * ratio;
|
|
175
|
+
const thickness = link.sourceThickness + (link.targetThickness - link.sourceThickness) * ratio;
|
|
176
|
+
if (y >= top && y <= top + thickness) return link;
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
};
|
|
180
|
+
const tooltipPayload = activeLink ? {
|
|
181
|
+
label: `${activeLink.source.name} → ${activeLink.target.name}`,
|
|
182
|
+
row: null,
|
|
183
|
+
series: [{
|
|
184
|
+
key: 'value',
|
|
185
|
+
name: '流量',
|
|
186
|
+
color: activeLink.color,
|
|
187
|
+
value: activeLink.value,
|
|
188
|
+
formatted: `${formatNumber(activeLink.value, {
|
|
189
|
+
compact: true
|
|
190
|
+
})}(${formatPercent(activeLink.value, total, 1)})`
|
|
191
|
+
}]
|
|
192
|
+
} : activeNode ? (() => {
|
|
193
|
+
const node = layout.nodes.find(item => item.name === activeNode);
|
|
194
|
+
if (!node) return null;
|
|
195
|
+
return {
|
|
196
|
+
label: node.name,
|
|
197
|
+
row: null,
|
|
198
|
+
series: [{
|
|
199
|
+
key: 'value',
|
|
200
|
+
name: '流量',
|
|
201
|
+
color: node.color,
|
|
202
|
+
value: node.value,
|
|
203
|
+
formatted: formatNumber(node.value, {
|
|
204
|
+
compact: true
|
|
205
|
+
})
|
|
206
|
+
}]
|
|
207
|
+
};
|
|
208
|
+
})() : null;
|
|
209
|
+
const autoAriaLabel = ariaLabel ?? `桑基图:${layout.nodes.length} 个节点,${layout.links.length} 条流向`;
|
|
210
|
+
return /*#__PURE__*/_jsx("div", {
|
|
211
|
+
className: classNames(prefixCls('chart'), className),
|
|
212
|
+
style: {
|
|
213
|
+
width,
|
|
214
|
+
height,
|
|
215
|
+
...style
|
|
216
|
+
},
|
|
217
|
+
"data-aura-chart": "sankey",
|
|
218
|
+
children: /*#__PURE__*/_jsxs("div", {
|
|
219
|
+
ref: ref,
|
|
220
|
+
className: prefixCls('chart__plot'),
|
|
221
|
+
onPointerMove: event => {
|
|
222
|
+
const link = linkFromPointer(event.clientX, event.clientY);
|
|
223
|
+
setActiveLink(link);
|
|
224
|
+
},
|
|
225
|
+
onPointerLeave: () => {
|
|
226
|
+
setActiveLink(null);
|
|
227
|
+
setActiveNode(null);
|
|
228
|
+
},
|
|
229
|
+
children: [!layout.nodes.length || size.width === 0 ? /*#__PURE__*/_jsx("div", {
|
|
230
|
+
className: prefixCls('chart__empty'),
|
|
231
|
+
children: emptyText
|
|
232
|
+
}) : /*#__PURE__*/_jsx(SvgScene, {
|
|
233
|
+
nodes: scene,
|
|
234
|
+
width: size.width,
|
|
235
|
+
height: size.height,
|
|
236
|
+
ariaLabel: autoAriaLabel
|
|
237
|
+
}), /*#__PURE__*/_jsx(Tooltip, {
|
|
238
|
+
payload: tooltipPayload,
|
|
239
|
+
x: activeLink ? (activeLink.source.x + activeLink.target.x) / 2 : size.width / 2,
|
|
240
|
+
y: 12,
|
|
241
|
+
containerWidth: size.width,
|
|
242
|
+
containerHeight: size.height,
|
|
243
|
+
theme: theme,
|
|
244
|
+
config: tooltip
|
|
245
|
+
})]
|
|
246
|
+
})
|
|
247
|
+
});
|
|
248
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { ChartDataRow, SeriesConfig } from '../../types.js';
|
|
3
|
+
import { type ChartThemeInput } from '../../theme/index.js';
|
|
4
|
+
import type { LegendConfig, TooltipConfig } from '../../types.js';
|
|
5
|
+
export interface ScatterChartProps {
|
|
6
|
+
data?: ChartDataRow[];
|
|
7
|
+
/** 数值型 x 字段 */
|
|
8
|
+
xKey?: string;
|
|
9
|
+
series?: SeriesConfig[];
|
|
10
|
+
width?: number | string;
|
|
11
|
+
height?: number;
|
|
12
|
+
theme?: ChartThemeInput;
|
|
13
|
+
palette?: string[];
|
|
14
|
+
legend?: boolean | LegendConfig;
|
|
15
|
+
tooltip?: boolean | TooltipConfig;
|
|
16
|
+
/** 点形状 */
|
|
17
|
+
symbol?: 'circle' | 'square' | 'diamond' | 'triangle' | 'star' | 'cross';
|
|
18
|
+
/** 点大小 */
|
|
19
|
+
symbolSize?: number;
|
|
20
|
+
animation?: boolean;
|
|
21
|
+
className?: string;
|
|
22
|
+
style?: React.CSSProperties;
|
|
23
|
+
ariaLabel?: string;
|
|
24
|
+
emptyText?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 散点图。
|
|
28
|
+
*
|
|
29
|
+
* 与折线/柱状不同,散点的 x 是**连续的数值轴**而非分类轴:
|
|
30
|
+
* 分类轴会把 [1, 2, 100] 摆成等间距,严重扭曲相关性判断。
|
|
31
|
+
* 因此这里不复用 CartesianChart 的 band 坐标,改为线性 x 轴。
|
|
32
|
+
*/
|
|
33
|
+
export declare function ScatterChart({ data, xKey, series, width, height, theme: themeInput, palette, legend, tooltip, symbol, symbolSize, animation, className, style, ariaLabel, emptyText, }: ScatterChartProps): 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,KAAyC,MAAM,OAAO,CAAC;AAE9D,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAkB,MAAM,aAAa,CAAC;AAO9E,OAAO,EAAW,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAO5D,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,6BAA6B,CA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