@aura-react-comp/charts 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/charts/area-chart/index.js +27 -23
- package/esm/charts/bar-chart/index.js +17 -21
- package/esm/charts/candlestick-chart/index.js +242 -180
- package/esm/charts/combo-chart/index.js +8 -20
- package/esm/charts/funnel-chart/index.js +133 -108
- package/esm/charts/gauge-chart/index.js +102 -105
- package/esm/charts/graph-chart/index.js +154 -114
- package/esm/charts/heatmap-chart/index.js +179 -135
- package/esm/charts/line-chart/index.js +28 -24
- package/esm/charts/pie-chart/index.d.ts.map +1 -1
- package/esm/charts/pie-chart/index.js +198 -139
- package/esm/charts/radar-chart/index.d.ts.map +1 -1
- package/esm/charts/radar-chart/index.js +154 -128
- package/esm/charts/ring-progress/index.js +81 -82
- package/esm/charts/sankey-chart/index.js +171 -141
- package/esm/charts/scatter-chart/index.d.ts.map +1 -1
- package/esm/charts/scatter-chart/index.js +225 -159
- package/esm/charts/sparkline/index.js +81 -66
- package/esm/charts/tree-chart/index.js +128 -102
- package/esm/charts/treemap-chart/index.js +162 -141
- package/esm/charts/waterfall-chart/index.js +165 -121
- package/esm/components/Legend.d.ts +3 -2
- package/esm/components/Legend.d.ts.map +1 -1
- package/esm/components/Legend.js +79 -48
- package/esm/components/Tooltip.js +62 -71
- package/esm/components/chart/index.d.ts +4 -1
- package/esm/components/chart/index.d.ts.map +1 -1
- package/esm/components/chart/index.js +80 -102
- package/esm/components/chart.less +13 -0
- package/esm/core/animation.js +31 -43
- package/esm/core/export.d.ts +34 -0
- package/esm/core/export.d.ts.map +1 -0
- package/esm/core/export.js +390 -0
- package/esm/core/force.js +22 -45
- package/esm/core/format.js +48 -52
- package/esm/core/hierarchy.js +60 -92
- package/esm/core/layout.js +32 -50
- package/esm/core/sampling.js +30 -53
- package/esm/core/sankey.js +35 -55
- package/esm/core/scale.js +72 -86
- package/esm/core/shape.js +101 -133
- package/esm/core/sync.d.ts +41 -0
- package/esm/core/sync.d.ts.map +1 -0
- package/esm/core/sync.js +120 -0
- package/esm/hooks/useChartExport.d.ts +19 -0
- package/esm/hooks/useChartExport.d.ts.map +1 -0
- package/esm/hooks/useChartExport.js +35 -0
- package/esm/hooks/useChartTheme.js +17 -19
- package/esm/hooks/useChartTransition.js +58 -73
- package/esm/hooks/useElementSize.js +19 -28
- package/esm/hooks/usePlotPointer.d.ts +23 -0
- package/esm/hooks/usePlotPointer.d.ts.map +1 -0
- package/esm/hooks/usePlotPointer.js +94 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +136 -47
- package/esm/internal/CartesianChart.d.ts.map +1 -1
- package/esm/internal/CartesianChart.js +666 -485
- package/esm/internal/model.js +44 -60
- package/esm/scene/canvas.js +100 -107
- package/esm/scene/svg.js +106 -99
- package/esm/scene/types.js +8 -15
- package/esm/style.css +10 -0
- package/esm/theme/index.js +143 -171
- package/esm/types.d.ts +4 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +0 -1
- package/package.json +1 -1
package/esm/core/hierarchy.js
CHANGED
|
@@ -1,48 +1,30 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (node.children?.length) {
|
|
2
|
+
// src/core/hierarchy.ts
|
|
3
|
+
function sumValues(node) {
|
|
4
|
+
var _a;
|
|
5
|
+
if ((_a = node.children) == null ? void 0 : _a.length) {
|
|
7
6
|
return node.children.reduce((sum, child) => sum + sumValues(child), 0);
|
|
8
7
|
}
|
|
9
8
|
return Math.max(0, node.value ?? 0);
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
function countLeaves(node) {
|
|
11
|
+
var _a;
|
|
12
|
+
if (!((_a = node.children) == null ? void 0 : _a.length))
|
|
13
|
+
return 1;
|
|
15
14
|
return node.children.reduce((sum, child) => sum + countLeaves(child), 0);
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return Math.max(...node.children.map(child => maxDepth(child, depth + 1)));
|
|
16
|
+
function maxDepth(node, depth = 0) {
|
|
17
|
+
var _a;
|
|
18
|
+
if (!((_a = node.children) == null ? void 0 : _a.length))
|
|
19
|
+
return depth;
|
|
20
|
+
return Math.max(...node.children.map((child) => maxDepth(child, depth + 1)));
|
|
22
21
|
}
|
|
23
|
-
|
|
24
|
-
* Squarified Treemap 布局(Bruls, Huizing & van Wijk, 2000)。
|
|
25
|
-
*
|
|
26
|
-
* 为什么不用「交替横竖切」(slice-and-dice):那个算法会产生极端细长的矩形,
|
|
27
|
-
* 面积虽然对得上,但细条既放不下文字、也难以目测比较。
|
|
28
|
-
* squarified 通过逐行控制最差宽高比,让每块尽量接近正方形。
|
|
29
|
-
*/
|
|
30
|
-
export function squarify(values, rect) {
|
|
22
|
+
function squarify(values, rect) {
|
|
31
23
|
const total = values.reduce((sum, value) => sum + value, 0);
|
|
32
24
|
if (total <= 0 || rect.width <= 0 || rect.height <= 0) {
|
|
33
|
-
return values.map(() => ({
|
|
34
|
-
x: rect.x,
|
|
35
|
-
y: rect.y,
|
|
36
|
-
width: 0,
|
|
37
|
-
height: 0
|
|
38
|
-
}));
|
|
25
|
+
return values.map(() => ({ x: rect.x, y: rect.y, width: 0, height: 0 }));
|
|
39
26
|
}
|
|
40
|
-
|
|
41
|
-
// 按面积降序,squarified 的前提是大块先摆
|
|
42
|
-
const order = values.map((value, index) => ({
|
|
43
|
-
value,
|
|
44
|
-
index
|
|
45
|
-
})).sort((a, b) => b.value - a.value);
|
|
27
|
+
const order = values.map((value, index) => ({ value, index })).sort((a, b) => b.value - a.value);
|
|
46
28
|
const result = values.map(() => ({
|
|
47
29
|
x: 0,
|
|
48
30
|
y: 0,
|
|
@@ -51,22 +33,23 @@ export function squarify(values, rect) {
|
|
|
51
33
|
}));
|
|
52
34
|
const area = rect.width * rect.height;
|
|
53
35
|
const scale = area / total;
|
|
54
|
-
let free = {
|
|
55
|
-
...rect
|
|
56
|
-
};
|
|
36
|
+
let free = { ...rect };
|
|
57
37
|
let cursor = 0;
|
|
58
38
|
while (cursor < order.length) {
|
|
59
39
|
const shortSide = Math.min(free.width, free.height);
|
|
60
|
-
if (shortSide <= 0)
|
|
61
|
-
|
|
62
|
-
// 贪心地把当前行填到「宽高比不再变好」为止
|
|
40
|
+
if (shortSide <= 0)
|
|
41
|
+
break;
|
|
63
42
|
const row = [];
|
|
64
43
|
let rowSum = 0;
|
|
65
44
|
let bestRatio = Number.POSITIVE_INFINITY;
|
|
66
45
|
while (cursor < order.length) {
|
|
67
46
|
const candidate = order[cursor];
|
|
68
47
|
const nextSum = rowSum + candidate.value;
|
|
69
|
-
const ratio = worstAspectRatio(
|
|
48
|
+
const ratio = worstAspectRatio(
|
|
49
|
+
[...row.map((item) => item.value), candidate.value],
|
|
50
|
+
nextSum * scale,
|
|
51
|
+
shortSide
|
|
52
|
+
);
|
|
70
53
|
if (row.length === 0 || ratio <= bestRatio) {
|
|
71
54
|
row.push(candidate);
|
|
72
55
|
rowSum = nextSum;
|
|
@@ -76,11 +59,8 @@ export function squarify(values, rect) {
|
|
|
76
59
|
break;
|
|
77
60
|
}
|
|
78
61
|
}
|
|
79
|
-
|
|
80
|
-
// 摆放这一行
|
|
81
62
|
const rowArea = rowSum * scale;
|
|
82
63
|
const horizontal = free.width >= free.height;
|
|
83
|
-
// 沿短边铺:短边是高时竖着排,短边是宽时横着排
|
|
84
64
|
const thickness = rowArea / Math.max(1e-6, horizontal ? free.height : free.width);
|
|
85
65
|
let offset = 0;
|
|
86
66
|
for (const item of row) {
|
|
@@ -120,24 +100,19 @@ export function squarify(values, rect) {
|
|
|
120
100
|
}
|
|
121
101
|
return result;
|
|
122
102
|
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 一行的最差宽高比。
|
|
126
|
-
*
|
|
127
|
-
* 公式来自原论文:给定行内各块面积、该行总面积与所在边的长度,
|
|
128
|
-
* 返回值越接近 1,矩形越接近正方形。
|
|
129
|
-
*/
|
|
130
103
|
function worstAspectRatio(row, rowArea, side) {
|
|
131
|
-
if (rowArea <= 0 || side <= 0)
|
|
104
|
+
if (rowArea <= 0 || side <= 0)
|
|
105
|
+
return Number.POSITIVE_INFINITY;
|
|
132
106
|
const max = Math.max(...row);
|
|
133
107
|
const min = Math.min(...row);
|
|
134
|
-
if (min <= 0)
|
|
135
|
-
|
|
108
|
+
if (min <= 0)
|
|
109
|
+
return Number.POSITIVE_INFINITY;
|
|
110
|
+
return Math.max(
|
|
111
|
+
side * side * max / (rowArea * rowArea),
|
|
112
|
+
rowArea * rowArea / (side * side * min)
|
|
113
|
+
);
|
|
136
114
|
}
|
|
137
|
-
|
|
138
|
-
* 递归铺满:先对根的直接子节点做 squarify,再在每个矩形内部继续切分。
|
|
139
|
-
*/
|
|
140
|
-
export function buildTreemap(root, rect, options) {
|
|
115
|
+
function buildTreemap(root, rect, options) {
|
|
141
116
|
const {
|
|
142
117
|
maxDepth: depthLimit = 1,
|
|
143
118
|
theme,
|
|
@@ -148,14 +123,16 @@ export function buildTreemap(root, rect, options) {
|
|
|
148
123
|
const total = sumValues(root) || 1;
|
|
149
124
|
const walk = (node, area, depth, parentColor) => {
|
|
150
125
|
const children = node.children ?? [];
|
|
151
|
-
|
|
152
|
-
|
|
126
|
+
if (!children.length || depth >= depthLimit)
|
|
127
|
+
return;
|
|
153
128
|
const values = children.map(sumValues);
|
|
154
129
|
const rects = squarify(values, area);
|
|
155
130
|
children.forEach((child, index) => {
|
|
131
|
+
var _a;
|
|
156
132
|
const box = rects[index];
|
|
157
|
-
if (box.width <= 0 || box.height <= 0)
|
|
158
|
-
|
|
133
|
+
if (box.width <= 0 || box.height <= 0)
|
|
134
|
+
return;
|
|
135
|
+
const color = child.color ?? (colorOf == null ? void 0 : colorOf(depth, index, parentColor)) ?? theme.palette[index % theme.palette.length];
|
|
159
136
|
cells.push({
|
|
160
137
|
node: child,
|
|
161
138
|
depth,
|
|
@@ -166,9 +143,7 @@ export function buildTreemap(root, rect, options) {
|
|
|
166
143
|
ratio: Math.min(1, sumValues(child) / total),
|
|
167
144
|
color
|
|
168
145
|
});
|
|
169
|
-
|
|
170
|
-
// 该格还会继续展开时,把子块的区域向下让出标题条
|
|
171
|
-
const willExpand = Boolean(child.children?.length) && depth + 1 < depthLimit;
|
|
146
|
+
const willExpand = Boolean((_a = child.children) == null ? void 0 : _a.length) && depth + 1 < depthLimit;
|
|
172
147
|
const inner = willExpand && headerHeight > 0 && box.height > headerHeight + 24 ? {
|
|
173
148
|
x: box.x,
|
|
174
149
|
y: box.y + headerHeight,
|
|
@@ -181,13 +156,7 @@ export function buildTreemap(root, rect, options) {
|
|
|
181
156
|
walk(root, rect, 0);
|
|
182
157
|
return cells;
|
|
183
158
|
}
|
|
184
|
-
|
|
185
|
-
* 树图布局:横向展开(深度 → x),纵向按叶子顺序均分。
|
|
186
|
-
*
|
|
187
|
-
* 父节点的 y 取子节点 y 的均值,这样连线不会交叉,
|
|
188
|
-
* 也让「同一父节点下的兄弟」在视觉上聚成一簇。
|
|
189
|
-
*/
|
|
190
|
-
export function layoutTree(root, rect) {
|
|
159
|
+
function layoutTree(root, rect) {
|
|
191
160
|
const depth = Math.max(1, maxDepth(root));
|
|
192
161
|
const leaves = Math.max(1, countLeaves(root));
|
|
193
162
|
const rowHeight = rect.height / leaves;
|
|
@@ -195,39 +164,38 @@ export function layoutTree(root, rect) {
|
|
|
195
164
|
const nodes = [];
|
|
196
165
|
let leafCursor = 0;
|
|
197
166
|
const walk = (node, level, parent) => {
|
|
167
|
+
var _a;
|
|
198
168
|
const index = nodes.length;
|
|
199
169
|
const x = rect.x + level * columnWidth;
|
|
200
|
-
nodes.push({
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
x,
|
|
204
|
-
y: 0,
|
|
205
|
-
parent
|
|
206
|
-
});
|
|
207
|
-
if (!node.children?.length) {
|
|
208
|
-
const y = rect.y + leafCursor * rowHeight + rowHeight / 2;
|
|
170
|
+
nodes.push({ node, depth: level, x, y: 0, parent });
|
|
171
|
+
if (!((_a = node.children) == null ? void 0 : _a.length)) {
|
|
172
|
+
const y2 = rect.y + leafCursor * rowHeight + rowHeight / 2;
|
|
209
173
|
leafCursor += 1;
|
|
210
|
-
nodes[index].y =
|
|
211
|
-
return
|
|
174
|
+
nodes[index].y = y2;
|
|
175
|
+
return y2;
|
|
212
176
|
}
|
|
213
|
-
const childYs = node.children.map(child => walk(child, level + 1, index));
|
|
177
|
+
const childYs = node.children.map((child) => walk(child, level + 1, index));
|
|
214
178
|
const y = childYs.reduce((sum, value) => sum + value, 0) / Math.max(1, childYs.length);
|
|
215
179
|
nodes[index].y = y;
|
|
216
180
|
return y;
|
|
217
181
|
};
|
|
218
182
|
walk(root, 0, -1);
|
|
219
|
-
return {
|
|
220
|
-
nodes,
|
|
221
|
-
depth
|
|
222
|
-
};
|
|
183
|
+
return { nodes, depth };
|
|
223
184
|
}
|
|
224
|
-
|
|
225
|
-
/** 树图连线:正交折线(父 → 子),比贝塞尔更清晰地表达层级 */
|
|
226
|
-
export function treeLinkPath(parent, child, nodeWidth) {
|
|
185
|
+
function treeLinkPath(parent, child, nodeWidth) {
|
|
227
186
|
const x1 = parent.x + nodeWidth;
|
|
228
187
|
const y1 = parent.y;
|
|
229
188
|
const x2 = child.x;
|
|
230
189
|
const y2 = child.y;
|
|
231
190
|
const midX = x1 + (x2 - x1) / 2;
|
|
232
191
|
return `M${x1},${y1}H${midX}V${y2}H${x2}`;
|
|
233
|
-
}
|
|
192
|
+
}
|
|
193
|
+
export {
|
|
194
|
+
buildTreemap,
|
|
195
|
+
countLeaves,
|
|
196
|
+
layoutTree,
|
|
197
|
+
maxDepth,
|
|
198
|
+
squarify,
|
|
199
|
+
sumValues,
|
|
200
|
+
treeLinkPath
|
|
201
|
+
};
|
package/esm/core/layout.js
CHANGED
|
@@ -1,46 +1,25 @@
|
|
|
1
1
|
'use client';
|
|
2
|
+
// src/core/layout.ts
|
|
2
3
|
import { estimateTextWidth } from "./format";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* 布局计算:把「容器尺寸」换算成「绘图区」。
|
|
6
|
-
*
|
|
7
|
-
* 难点在于坐标轴文字会占用边缘空间,而可用的边缘空间又取决于容器大小 ——
|
|
8
|
-
* 这里采用业界通行的两遍法:
|
|
9
|
-
* 第一遍用完整容器高度估算刻度数量 → 得到刻度标签;
|
|
10
|
-
* 第二遍按标签实际宽度(估算)反推 margin。
|
|
11
|
-
* 两遍都是纯计算,不触碰 DOM,因此可在 SSR 与测试中直接跑。
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** x 轴标签的排布方式 */
|
|
15
|
-
|
|
16
|
-
const AXIS_GAP = 8;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 计算 x 轴标签布局。
|
|
20
|
-
*
|
|
21
|
-
* 判定顺序:
|
|
22
|
-
* 1. 全部横排放得下 → horizontal
|
|
23
|
-
* 2. 斜排 35° 放得下 → rotate
|
|
24
|
-
* 3. 都不行 → 抽样显示(interval),强制横排,避免出现密集的"斜线森林"
|
|
25
|
-
*/
|
|
26
|
-
export function computeXAxisLayout(labels, availableWidth, theme) {
|
|
4
|
+
var AXIS_GAP = 8;
|
|
5
|
+
function computeXAxisLayout(labels, availableWidth, theme) {
|
|
27
6
|
const fontSize = theme.font.size;
|
|
28
7
|
const count = labels.length;
|
|
29
8
|
if (count === 0) {
|
|
30
9
|
return {
|
|
31
|
-
layout:
|
|
10
|
+
layout: "horizontal",
|
|
32
11
|
rotate: 0,
|
|
33
12
|
fontSize,
|
|
34
13
|
height: 0,
|
|
35
14
|
interval: 0
|
|
36
15
|
};
|
|
37
16
|
}
|
|
38
|
-
const widths = labels.map(label => estimateTextWidth(label, fontSize));
|
|
17
|
+
const widths = labels.map((label) => estimateTextWidth(label, fontSize));
|
|
39
18
|
const maxWidth = Math.max(...widths);
|
|
40
19
|
const slot = availableWidth / count;
|
|
41
20
|
if (maxWidth + 12 <= slot) {
|
|
42
21
|
return {
|
|
43
|
-
layout:
|
|
22
|
+
layout: "horizontal",
|
|
44
23
|
rotate: 0,
|
|
45
24
|
fontSize,
|
|
46
25
|
height: fontSize + AXIS_GAP,
|
|
@@ -53,25 +32,29 @@ export function computeXAxisLayout(labels, availableWidth, theme) {
|
|
|
53
32
|
if (rotatedWidth + 4 <= slot) {
|
|
54
33
|
const height = Math.ceil(maxWidth * Math.sin(rad) + fontSize * Math.cos(rad)) + AXIS_GAP;
|
|
55
34
|
return {
|
|
56
|
-
layout:
|
|
35
|
+
layout: "rotate",
|
|
57
36
|
rotate,
|
|
58
37
|
fontSize,
|
|
59
38
|
height: Math.min(height, 72),
|
|
60
39
|
interval: 0
|
|
61
40
|
};
|
|
62
41
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
42
|
+
const interval = Math.max(
|
|
43
|
+
1,
|
|
44
|
+
Math.ceil(
|
|
45
|
+
count / Math.max(1, Math.floor(availableWidth / (maxWidth + 12)))
|
|
46
|
+
)
|
|
47
|
+
);
|
|
66
48
|
return {
|
|
67
|
-
layout:
|
|
49
|
+
layout: "horizontal",
|
|
68
50
|
rotate: 0,
|
|
69
51
|
fontSize,
|
|
70
52
|
height: fontSize + AXIS_GAP,
|
|
71
53
|
interval
|
|
72
54
|
};
|
|
73
55
|
}
|
|
74
|
-
|
|
56
|
+
function computeLayout(input) {
|
|
57
|
+
var _a, _b, _c, _d;
|
|
75
58
|
const {
|
|
76
59
|
width,
|
|
77
60
|
height,
|
|
@@ -81,14 +64,14 @@ export function computeLayout(input) {
|
|
|
81
64
|
showXAxis = true,
|
|
82
65
|
showYAxis = true
|
|
83
66
|
} = input;
|
|
84
|
-
const maxYLabelWidth = yLabels.length ? Math.max(
|
|
67
|
+
const maxYLabelWidth = yLabels.length ? Math.max(
|
|
68
|
+
...yLabels.map((label) => estimateTextWidth(label, theme.font.size))
|
|
69
|
+
) : 0;
|
|
85
70
|
const left = showYAxis ? Math.ceil(maxYLabelWidth + theme.axis.tickSize + AXIS_GAP) : 12;
|
|
86
71
|
const right = 16;
|
|
87
|
-
|
|
88
|
-
// 第一遍:先按横向标签预留底部空间,得出可用宽度
|
|
89
72
|
const provisionalPlotWidth = Math.max(0, width - left - right);
|
|
90
73
|
const xAxis = showXAxis ? computeXAxisLayout(xLabels, provisionalPlotWidth, theme) : {
|
|
91
|
-
layout:
|
|
74
|
+
layout: "horizontal",
|
|
92
75
|
rotate: 0,
|
|
93
76
|
fontSize: theme.font.size,
|
|
94
77
|
height: 0,
|
|
@@ -97,10 +80,10 @@ export function computeLayout(input) {
|
|
|
97
80
|
const bottom = showXAxis ? xAxis.height + 4 : 12;
|
|
98
81
|
const top = 12;
|
|
99
82
|
const margin = {
|
|
100
|
-
top: input.margin
|
|
101
|
-
right: input.margin
|
|
102
|
-
bottom: input.margin
|
|
103
|
-
left: input.margin
|
|
83
|
+
top: ((_a = input.margin) == null ? void 0 : _a.top) ?? top,
|
|
84
|
+
right: ((_b = input.margin) == null ? void 0 : _b.right) ?? right,
|
|
85
|
+
bottom: ((_c = input.margin) == null ? void 0 : _c.bottom) ?? bottom,
|
|
86
|
+
left: ((_d = input.margin) == null ? void 0 : _d.left) ?? left
|
|
104
87
|
};
|
|
105
88
|
const plot = {
|
|
106
89
|
x: margin.left,
|
|
@@ -108,14 +91,13 @@ export function computeLayout(input) {
|
|
|
108
91
|
width: Math.max(0, width - margin.left - margin.right),
|
|
109
92
|
height: Math.max(0, height - margin.top - margin.bottom)
|
|
110
93
|
};
|
|
111
|
-
return {
|
|
112
|
-
plot,
|
|
113
|
-
margin,
|
|
114
|
-
xAxis
|
|
115
|
-
};
|
|
94
|
+
return { plot, margin, xAxis };
|
|
116
95
|
}
|
|
117
|
-
|
|
118
|
-
/** 数值轴刻度数量的经验值:约每 44px 一个刻度,限制在 3~8 之间 */
|
|
119
|
-
export function suggestTickCount(plotHeight) {
|
|
96
|
+
function suggestTickCount(plotHeight) {
|
|
120
97
|
return Math.min(8, Math.max(3, Math.round(plotHeight / 44)));
|
|
121
|
-
}
|
|
98
|
+
}
|
|
99
|
+
export {
|
|
100
|
+
computeLayout,
|
|
101
|
+
computeXAxisLayout,
|
|
102
|
+
suggestTickCount
|
|
103
|
+
};
|
package/esm/core/sampling.js
CHANGED
|
@@ -1,40 +1,19 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* 为什么需要:数据点超过几百个后,屏幕像素已经不够区分相邻点了,
|
|
6
|
-
* 继续逐点渲染只会拖慢滚动而不会增加信息量。
|
|
7
|
-
* 但「每隔 N 个取一个」会丢掉尖峰——那些恰恰是读者最关心的。
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Largest-Triangle-Three-Buckets(LTTB)。
|
|
12
|
-
*
|
|
13
|
-
* 把数据切成 threshold 个桶,每个桶里选一个点,使得它与前后选中点
|
|
14
|
-
* 构成的三角形面积最大——面积大意味着这个点「离直线最远」,
|
|
15
|
-
* 也就是最能代表该段形状特征的极值点。
|
|
16
|
-
*
|
|
17
|
-
* 复杂度 O(n),且能保住尖峰,是目前综合表现最好的保形降采样之一。
|
|
18
|
-
*
|
|
19
|
-
* @returns 选中点的原始下标(升序),首尾必选
|
|
20
|
-
*/
|
|
21
|
-
export function lttbIndices(points, threshold) {
|
|
2
|
+
// src/core/sampling.ts
|
|
3
|
+
function lttbIndices(points, threshold) {
|
|
22
4
|
const length = points.length;
|
|
23
|
-
if (length <= 2)
|
|
5
|
+
if (length <= 2)
|
|
6
|
+
return points.map((_, index) => index);
|
|
24
7
|
if (threshold >= length) {
|
|
25
|
-
// 不需要降采样,直接全取
|
|
26
8
|
return points.map((_, index) => index);
|
|
27
9
|
}
|
|
28
10
|
if (threshold <= 2) {
|
|
29
|
-
// 只保留首尾,至少能看出整体范围
|
|
30
11
|
return [0, length - 1];
|
|
31
12
|
}
|
|
32
13
|
const indices = [0];
|
|
33
|
-
// 每桶的跨度;最后一个点单独处理,所以是 (n-2)/(threshold-2)
|
|
34
14
|
const bucketSize = (length - 2) / (threshold - 2);
|
|
35
15
|
let previous = 0;
|
|
36
16
|
for (let bucket = 0; bucket < threshold - 2; bucket += 1) {
|
|
37
|
-
// 下一桶的平均点,用来算三角形面积
|
|
38
17
|
const nextStart = Math.floor((bucket + 1) * bucketSize) + 1;
|
|
39
18
|
const nextEnd = Math.min(Math.floor((bucket + 2) * bucketSize) + 1, length);
|
|
40
19
|
let averageX = 0;
|
|
@@ -42,13 +21,13 @@ export function lttbIndices(points, threshold) {
|
|
|
42
21
|
let averageCount = 0;
|
|
43
22
|
for (let index = nextStart; index < nextEnd; index += 1) {
|
|
44
23
|
const point = points[index];
|
|
45
|
-
if (point.y === null || !Number.isFinite(point.y))
|
|
24
|
+
if (point.y === null || !Number.isFinite(point.y))
|
|
25
|
+
continue;
|
|
46
26
|
averageX += point.x;
|
|
47
27
|
averageY += point.y;
|
|
48
28
|
averageCount += 1;
|
|
49
29
|
}
|
|
50
30
|
if (averageCount === 0) {
|
|
51
|
-
// 下一桶全是缺失值,就退化为取桶中点
|
|
52
31
|
const fallback = Math.min(Math.floor(nextStart), length - 1);
|
|
53
32
|
averageX = points[fallback].x;
|
|
54
33
|
averageY = 0;
|
|
@@ -57,12 +36,13 @@ export function lttbIndices(points, threshold) {
|
|
|
57
36
|
averageX /= averageCount;
|
|
58
37
|
averageY /= averageCount;
|
|
59
38
|
const rangeStart = Math.floor(bucket * bucketSize) + 1;
|
|
60
|
-
const rangeEnd = Math.min(
|
|
39
|
+
const rangeEnd = Math.min(
|
|
40
|
+
Math.floor((bucket + 1) * bucketSize) + 1,
|
|
41
|
+
length - 1
|
|
42
|
+
);
|
|
61
43
|
const anchor = points[previous];
|
|
62
44
|
let maxArea = -1;
|
|
63
45
|
let chosen = -1;
|
|
64
|
-
|
|
65
|
-
// 锚点本身缺失时无法算面积,退化为取桶内第一个有效点
|
|
66
46
|
if (anchor.y === null || !Number.isFinite(anchor.y)) {
|
|
67
47
|
for (let index = rangeStart; index < rangeEnd; index += 1) {
|
|
68
48
|
if (points[index].y !== null) {
|
|
@@ -70,20 +50,24 @@ export function lttbIndices(points, threshold) {
|
|
|
70
50
|
break;
|
|
71
51
|
}
|
|
72
52
|
}
|
|
73
|
-
if (chosen < 0)
|
|
53
|
+
if (chosen < 0)
|
|
54
|
+
chosen = rangeStart;
|
|
74
55
|
} else {
|
|
75
56
|
const anchorY = anchor.y;
|
|
76
57
|
for (let index = rangeStart; index < rangeEnd; index += 1) {
|
|
77
58
|
const point = points[index];
|
|
78
|
-
if (point.y === null || !Number.isFinite(point.y))
|
|
79
|
-
|
|
80
|
-
const area = Math.abs(
|
|
59
|
+
if (point.y === null || !Number.isFinite(point.y))
|
|
60
|
+
continue;
|
|
61
|
+
const area = Math.abs(
|
|
62
|
+
(anchor.x - averageX) * (point.y - anchorY) - (anchor.x - point.x) * (averageY - anchorY)
|
|
63
|
+
);
|
|
81
64
|
if (area > maxArea) {
|
|
82
65
|
maxArea = area;
|
|
83
66
|
chosen = index;
|
|
84
67
|
}
|
|
85
68
|
}
|
|
86
|
-
if (chosen < 0)
|
|
69
|
+
if (chosen < 0)
|
|
70
|
+
chosen = rangeStart;
|
|
87
71
|
}
|
|
88
72
|
indices.push(chosen);
|
|
89
73
|
previous = chosen;
|
|
@@ -91,19 +75,11 @@ export function lttbIndices(points, threshold) {
|
|
|
91
75
|
indices.push(length - 1);
|
|
92
76
|
return indices;
|
|
93
77
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
* 每个系列各自采样会得到不同的下标集合,折线之间就会错位,
|
|
100
|
-
* 看起来像两列数据在互相穿插。
|
|
101
|
-
*/
|
|
102
|
-
export function sampleSeriesIndices(values, threshold) {
|
|
103
|
-
const pointCount = values[0]?.length ?? 0;
|
|
104
|
-
if (pointCount === 0) return [];
|
|
105
|
-
|
|
106
|
-
// 用各系列的绝对值之和作为「形状强度」,避免只看第一条系列导致别的系列丢峰
|
|
78
|
+
function sampleSeriesIndices(values, threshold) {
|
|
79
|
+
var _a;
|
|
80
|
+
const pointCount = ((_a = values[0]) == null ? void 0 : _a.length) ?? 0;
|
|
81
|
+
if (pointCount === 0)
|
|
82
|
+
return [];
|
|
107
83
|
const combined = [];
|
|
108
84
|
for (let index = 0; index < pointCount; index += 1) {
|
|
109
85
|
let sum = 0;
|
|
@@ -115,10 +91,11 @@ export function sampleSeriesIndices(values, threshold) {
|
|
|
115
91
|
hasValue = true;
|
|
116
92
|
}
|
|
117
93
|
}
|
|
118
|
-
combined.push({
|
|
119
|
-
x: index,
|
|
120
|
-
y: hasValue ? sum : null
|
|
121
|
-
});
|
|
94
|
+
combined.push({ x: index, y: hasValue ? sum : null });
|
|
122
95
|
}
|
|
123
96
|
return lttbIndices(combined, threshold);
|
|
124
|
-
}
|
|
97
|
+
}
|
|
98
|
+
export {
|
|
99
|
+
lttbIndices,
|
|
100
|
+
sampleSeriesIndices
|
|
101
|
+
};
|
package/esm/core/sankey.js
CHANGED
|
@@ -1,40 +1,18 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* 2. 层内堆叠 —— 每层总流量归一化到可用高度,节点按流量占比占高;
|
|
9
|
-
* 3. 端口分配 —— 同一节点上的多条链路按顺序切分它的一条边,
|
|
10
|
-
* 这样带宽之和恰好等于节点高度。
|
|
11
|
-
*
|
|
12
|
-
* 含环的数据无法拓扑排序,这里用「松弛 n 轮后停止」兜底,
|
|
13
|
-
* 而不是抛错 —— 展示型图表遇到脏数据应该画出来,而不是白屏。
|
|
14
|
-
*/
|
|
15
|
-
export function layoutSankey(nodes, links, rect, options) {
|
|
16
|
-
const {
|
|
17
|
-
nodeWidth = 12,
|
|
18
|
-
nodeGap = 10,
|
|
19
|
-
colorOf
|
|
20
|
-
} = options;
|
|
21
|
-
if (!nodes.length || !links.length) return {
|
|
22
|
-
nodes: [],
|
|
23
|
-
links: [],
|
|
24
|
-
layers: 0
|
|
25
|
-
};
|
|
26
|
-
const names = nodes.map(node => node.name);
|
|
2
|
+
// src/core/sankey.ts
|
|
3
|
+
function layoutSankey(nodes, links, rect, options) {
|
|
4
|
+
const { nodeWidth = 12, nodeGap = 10, colorOf } = options;
|
|
5
|
+
if (!nodes.length || !links.length)
|
|
6
|
+
return { nodes: [], links: [], layers: 0 };
|
|
7
|
+
const names = nodes.map((node) => node.name);
|
|
27
8
|
const exists = new Set(names);
|
|
28
|
-
const validLinks = links.filter(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
layers: 0
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// ---- 1. 拓扑分层 ----
|
|
36
|
-
const layer = new Map();
|
|
37
|
-
names.forEach(name => layer.set(name, 0));
|
|
9
|
+
const validLinks = links.filter(
|
|
10
|
+
(link) => exists.has(link.source) && exists.has(link.target) && link.source !== link.target && Number.isFinite(link.value) && link.value > 0
|
|
11
|
+
);
|
|
12
|
+
if (!validLinks.length)
|
|
13
|
+
return { nodes: [], links: [], layers: 0 };
|
|
14
|
+
const layer = /* @__PURE__ */ new Map();
|
|
15
|
+
names.forEach((name) => layer.set(name, 0));
|
|
38
16
|
for (let round = 0; round < names.length; round += 1) {
|
|
39
17
|
let changed = false;
|
|
40
18
|
for (const link of validLinks) {
|
|
@@ -44,24 +22,24 @@ export function layoutSankey(nodes, links, rect, options) {
|
|
|
44
22
|
changed = true;
|
|
45
23
|
}
|
|
46
24
|
}
|
|
47
|
-
if (!changed)
|
|
25
|
+
if (!changed)
|
|
26
|
+
break;
|
|
48
27
|
}
|
|
49
28
|
const maxLayer = Math.max(...Array.from(layer.values()));
|
|
50
29
|
const layers = maxLayer + 1;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const inflow = new Map();
|
|
54
|
-
const outflow = new Map();
|
|
30
|
+
const inflow = /* @__PURE__ */ new Map();
|
|
31
|
+
const outflow = /* @__PURE__ */ new Map();
|
|
55
32
|
for (const link of validLinks) {
|
|
56
33
|
outflow.set(link.source, (outflow.get(link.source) ?? 0) + link.value);
|
|
57
34
|
inflow.set(link.target, (inflow.get(link.target) ?? 0) + link.value);
|
|
58
35
|
}
|
|
59
|
-
const valueOf = name => Math.max(inflow.get(name) ?? 0, outflow.get(name) ?? 0);
|
|
36
|
+
const valueOf = (name) => Math.max(inflow.get(name) ?? 0, outflow.get(name) ?? 0);
|
|
60
37
|
const columnGap = (rect.width - nodeWidth * layers) / Math.max(1, layers - 1);
|
|
61
|
-
const boxes = new Map();
|
|
38
|
+
const boxes = /* @__PURE__ */ new Map();
|
|
62
39
|
for (let current = 0; current < layers; current += 1) {
|
|
63
|
-
const column = names.filter(name => (layer.get(name) ?? 0) === current);
|
|
64
|
-
if (!column.length)
|
|
40
|
+
const column = names.filter((name) => (layer.get(name) ?? 0) === current);
|
|
41
|
+
if (!column.length)
|
|
42
|
+
continue;
|
|
65
43
|
const totalFlow = column.reduce((sum, name) => sum + valueOf(name), 0) || 1;
|
|
66
44
|
const available = rect.height - nodeGap * (column.length - 1);
|
|
67
45
|
const scale = available / totalFlow;
|
|
@@ -69,7 +47,7 @@ export function layoutSankey(nodes, links, rect, options) {
|
|
|
69
47
|
column.forEach((name, index) => {
|
|
70
48
|
const value = valueOf(name);
|
|
71
49
|
const height = Math.max(2, value * scale);
|
|
72
|
-
const node = nodes.find(item => item.name === name);
|
|
50
|
+
const node = nodes.find((item) => item.name === name);
|
|
73
51
|
boxes.set(name, {
|
|
74
52
|
name,
|
|
75
53
|
layer: current,
|
|
@@ -78,17 +56,14 @@ export function layoutSankey(nodes, links, rect, options) {
|
|
|
78
56
|
width: nodeWidth,
|
|
79
57
|
height,
|
|
80
58
|
value,
|
|
81
|
-
color: node
|
|
59
|
+
color: (node == null ? void 0 : node.color) ?? colorOf(current, index)
|
|
82
60
|
});
|
|
83
61
|
cursor += height + nodeGap;
|
|
84
62
|
});
|
|
85
63
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const sourceCursor = new Map();
|
|
90
|
-
const targetCursor = new Map();
|
|
91
|
-
names.forEach(name => {
|
|
64
|
+
const sourceCursor = /* @__PURE__ */ new Map();
|
|
65
|
+
const targetCursor = /* @__PURE__ */ new Map();
|
|
66
|
+
names.forEach((name) => {
|
|
92
67
|
const box = boxes.get(name);
|
|
93
68
|
if (box) {
|
|
94
69
|
sourceCursor.set(name, box.y);
|
|
@@ -97,14 +72,16 @@ export function layoutSankey(nodes, links, rect, options) {
|
|
|
97
72
|
});
|
|
98
73
|
const sortedLinks = [...validLinks].sort((a, b) => {
|
|
99
74
|
const layerDiff = (layer.get(a.target) ?? 0) - (layer.get(b.target) ?? 0);
|
|
100
|
-
if (layerDiff !== 0)
|
|
75
|
+
if (layerDiff !== 0)
|
|
76
|
+
return layerDiff;
|
|
101
77
|
return (layer.get(a.source) ?? 0) - (layer.get(b.source) ?? 0);
|
|
102
78
|
});
|
|
103
79
|
const paths = [];
|
|
104
80
|
for (const link of sortedLinks) {
|
|
105
81
|
const source = boxes.get(link.source);
|
|
106
82
|
const target = boxes.get(link.target);
|
|
107
|
-
if (!source || !target)
|
|
83
|
+
if (!source || !target)
|
|
84
|
+
continue;
|
|
108
85
|
const sourceTotal = Math.max(1, valueOf(source.name));
|
|
109
86
|
const targetTotal = Math.max(1, valueOf(target.name));
|
|
110
87
|
const sourceThickness = link.value / sourceTotal * source.height;
|
|
@@ -133,4 +110,7 @@ export function layoutSankey(nodes, links, rect, options) {
|
|
|
133
110
|
links: paths,
|
|
134
111
|
layers
|
|
135
112
|
};
|
|
136
|
-
}
|
|
113
|
+
}
|
|
114
|
+
export {
|
|
115
|
+
layoutSankey
|
|
116
|
+
};
|