@aura-react-comp/charts 0.0.0-stage → 0.2.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/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 +30 -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 +27 -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 +346 -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 +25 -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 +199 -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 +216 -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 +223 -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 +280 -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 +32 -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 +271 -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 +262 -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 +136 -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 +247 -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 +304 -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 +151 -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 +199 -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 +238 -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 +280 -0
- package/esm/components/Legend.d.ts +24 -0
- package/esm/components/Legend.d.ts.map +1 -0
- package/esm/components/Legend.js +49 -0
- package/esm/components/Tooltip.d.ts +24 -0
- package/esm/components/Tooltip.d.ts.map +1 -0
- package/esm/components/Tooltip.js +84 -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 +141 -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 +92 -0
- package/esm/core/force.d.ts +43 -0
- package/esm/core/force.d.ts.map +1 -0
- package/esm/core/force.js +133 -0
- package/esm/core/format.d.ts +34 -0
- package/esm/core/format.d.ts.map +1 -0
- package/esm/core/format.js +80 -0
- package/esm/core/hierarchy.d.ts +77 -0
- package/esm/core/hierarchy.d.ts.map +1 -0
- package/esm/core/hierarchy.js +232 -0
- package/esm/core/layout.d.ts +67 -0
- package/esm/core/layout.d.ts.map +1 -0
- package/esm/core/layout.js +120 -0
- package/esm/core/sampling.d.ts +32 -0
- package/esm/core/sampling.d.ts.map +1 -0
- package/esm/core/sampling.js +123 -0
- package/esm/core/sankey.d.ts +67 -0
- package/esm/core/sankey.d.ts.map +1 -0
- package/esm/core/sankey.js +135 -0
- package/esm/core/scale.d.ts +98 -0
- package/esm/core/scale.d.ts.map +1 -0
- package/esm/core/scale.js +206 -0
- package/esm/core/shape.d.ts +60 -0
- package/esm/core/shape.d.ts.map +1 -0
- package/esm/core/shape.js +245 -0
- package/esm/global.d.ts +6 -0
- package/esm/hooks/useChartTheme.d.ts +10 -0
- package/esm/hooks/useChartTheme.d.ts.map +1 -0
- package/esm/hooks/useChartTheme.js +33 -0
- package/esm/hooks/useChartTransition.d.ts +20 -0
- package/esm/hooks/useChartTransition.d.ts.map +1 -0
- package/esm/hooks/useChartTransition.js +119 -0
- package/esm/hooks/useElementSize.d.ts +17 -0
- package/esm/hooks/useElementSize.d.ts.map +1 -0
- package/esm/hooks/useElementSize.js +46 -0
- package/esm/index.d.ts +62 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +47 -0
- package/esm/internal/CartesianChart.d.ts +16 -0
- package/esm/internal/CartesianChart.d.ts.map +1 -0
- package/esm/internal/CartesianChart.js +810 -0
- package/esm/internal/model.d.ts +67 -0
- package/esm/internal/model.d.ts.map +1 -0
- package/esm/internal/model.js +145 -0
- package/esm/scene/canvas.d.ts +18 -0
- package/esm/scene/canvas.d.ts.map +1 -0
- package/esm/scene/canvas.js +137 -0
- package/esm/scene/svg.d.ts +12 -0
- package/esm/scene/svg.d.ts.map +1 -0
- package/esm/scene/svg.js +115 -0
- package/esm/scene/types.d.ts +89 -0
- package/esm/scene/types.d.ts.map +1 -0
- package/esm/scene/types.js +20 -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 +297 -0
- package/esm/types.d.ts +168 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +1 -0
- package/package.json +64 -3
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ChartTheme } from '../theme';
|
|
2
|
+
/** 层级数据:叶子节点用 value,分支节点可不填(由子节点求和) */
|
|
3
|
+
export interface HierarchyNode {
|
|
4
|
+
name: string;
|
|
5
|
+
value?: number;
|
|
6
|
+
children?: HierarchyNode[];
|
|
7
|
+
color?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Rect {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}
|
|
15
|
+
/** 递归求和:分支节点取子节点之和,叶子取自身 value */
|
|
16
|
+
export declare function sumValues(node: HierarchyNode): number;
|
|
17
|
+
/** 叶子总数,用于树图的纵向排布 */
|
|
18
|
+
export declare function countLeaves(node: HierarchyNode): number;
|
|
19
|
+
/** 树的最大深度(根为 0) */
|
|
20
|
+
export declare function maxDepth(node: HierarchyNode, depth?: number): number;
|
|
21
|
+
export interface TreemapCell extends Rect {
|
|
22
|
+
node: HierarchyNode;
|
|
23
|
+
/** 层级深度,根的直接子节点为 0 */
|
|
24
|
+
depth: number;
|
|
25
|
+
/** 归一化数值(0~1),用于颜色深浅 */
|
|
26
|
+
ratio: number;
|
|
27
|
+
/** 该格最终使用的颜色(顶层取色板,子层继承父色的浅化版) */
|
|
28
|
+
color: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Squarified Treemap 布局(Bruls, Huizing & van Wijk, 2000)。
|
|
32
|
+
*
|
|
33
|
+
* 为什么不用「交替横竖切」(slice-and-dice):那个算法会产生极端细长的矩形,
|
|
34
|
+
* 面积虽然对得上,但细条既放不下文字、也难以目测比较。
|
|
35
|
+
* squarified 通过逐行控制最差宽高比,让每块尽量接近正方形。
|
|
36
|
+
*/
|
|
37
|
+
export declare function squarify(values: number[], rect: Rect): Rect[];
|
|
38
|
+
export interface TreemapOptions {
|
|
39
|
+
/** 最多展开到第几层(0 表示只画根的直接子节点) */
|
|
40
|
+
maxDepth?: number;
|
|
41
|
+
theme: ChartTheme;
|
|
42
|
+
/**
|
|
43
|
+
* 取色函数。depth 为 0 时返回顶层颜色;
|
|
44
|
+
* 更深层通常返回父色的浅化版本。
|
|
45
|
+
*/
|
|
46
|
+
colorOf?: (depth: number, index: number, parentColor?: string) => string;
|
|
47
|
+
/**
|
|
48
|
+
* 会继续展开的格子,在顶部预留的标题条高度。
|
|
49
|
+
* 不留这条,父级名称会被子块直接盖住。
|
|
50
|
+
*/
|
|
51
|
+
headerHeight?: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 递归铺满:先对根的直接子节点做 squarify,再在每个矩形内部继续切分。
|
|
55
|
+
*/
|
|
56
|
+
export declare function buildTreemap(root: HierarchyNode, rect: Rect, options: TreemapOptions): TreemapCell[];
|
|
57
|
+
export interface TreeLayoutNode {
|
|
58
|
+
node: HierarchyNode;
|
|
59
|
+
depth: number;
|
|
60
|
+
x: number;
|
|
61
|
+
y: number;
|
|
62
|
+
/** 父节点在返回数组中的下标,根为 -1 */
|
|
63
|
+
parent: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 树图布局:横向展开(深度 → x),纵向按叶子顺序均分。
|
|
67
|
+
*
|
|
68
|
+
* 父节点的 y 取子节点 y 的均值,这样连线不会交叉,
|
|
69
|
+
* 也让「同一父节点下的兄弟」在视觉上聚成一簇。
|
|
70
|
+
*/
|
|
71
|
+
export declare function layoutTree(root: HierarchyNode, rect: Rect): {
|
|
72
|
+
nodes: TreeLayoutNode[];
|
|
73
|
+
depth: number;
|
|
74
|
+
};
|
|
75
|
+
/** 树图连线:正交折线(父 → 子),比贝塞尔更清晰地表达层级 */
|
|
76
|
+
export declare function treeLinkPath(parent: TreeLayoutNode, child: TreeLayoutNode, nodeWidth: number): string;
|
|
77
|
+
//# sourceMappingURL=hierarchy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hierarchy.d.ts","sourceRoot":"","sources":["hierarchy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,uCAAuC;AACvC,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,IAAI;IACnB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,kCAAkC;AAClC,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM,CAKrD;AAED,qBAAqB;AACrB,wBAAgB,WAAW,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM,CAGvD;AAED,mBAAmB;AACnB,wBAAgB,QAAQ,CAAC,IAAI,EAAE,aAAa,EAAE,KAAK,SAAI,GAAG,MAAM,CAG/D;AAED,MAAM,WAAW,WAAY,SAAQ,IAAI;IACvC,IAAI,EAAE,aAAa,CAAC;IACpB,sBAAsB;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,wBAAwB;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,CAgG7D;AAuBD,MAAM,WAAW,cAAc;IAC7B,8BAA8B;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,UAAU,CAAC;IAClB;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IACzE;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,aAAa,EACnB,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,cAAc,GACtB,WAAW,EAAE,CA8Df;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,aAAa,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,yBAAyB;IACzB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,aAAa,EACnB,IAAI,EAAE,IAAI,GACT;IAAE,KAAK,EAAE,cAAc,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CA+B5C;AAED,oCAAoC;AACpC,wBAAgB,YAAY,CAC1B,MAAM,EAAE,cAAc,EACtB,KAAK,EAAE,cAAc,EACrB,SAAS,EAAE,MAAM,GAChB,MAAM,CAOR"}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/** 层级数据:叶子节点用 value,分支节点可不填(由子节点求和) */
|
|
2
|
+
|
|
3
|
+
/** 递归求和:分支节点取子节点之和,叶子取自身 value */
|
|
4
|
+
export function sumValues(node) {
|
|
5
|
+
if (node.children?.length) {
|
|
6
|
+
return node.children.reduce((sum, child) => sum + sumValues(child), 0);
|
|
7
|
+
}
|
|
8
|
+
return Math.max(0, node.value ?? 0);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** 叶子总数,用于树图的纵向排布 */
|
|
12
|
+
export function countLeaves(node) {
|
|
13
|
+
if (!node.children?.length) return 1;
|
|
14
|
+
return node.children.reduce((sum, child) => sum + countLeaves(child), 0);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 树的最大深度(根为 0) */
|
|
18
|
+
export function maxDepth(node, depth = 0) {
|
|
19
|
+
if (!node.children?.length) return depth;
|
|
20
|
+
return Math.max(...node.children.map(child => maxDepth(child, depth + 1)));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Squarified Treemap 布局(Bruls, Huizing & van Wijk, 2000)。
|
|
24
|
+
*
|
|
25
|
+
* 为什么不用「交替横竖切」(slice-and-dice):那个算法会产生极端细长的矩形,
|
|
26
|
+
* 面积虽然对得上,但细条既放不下文字、也难以目测比较。
|
|
27
|
+
* squarified 通过逐行控制最差宽高比,让每块尽量接近正方形。
|
|
28
|
+
*/
|
|
29
|
+
export function squarify(values, rect) {
|
|
30
|
+
const total = values.reduce((sum, value) => sum + value, 0);
|
|
31
|
+
if (total <= 0 || rect.width <= 0 || rect.height <= 0) {
|
|
32
|
+
return values.map(() => ({
|
|
33
|
+
x: rect.x,
|
|
34
|
+
y: rect.y,
|
|
35
|
+
width: 0,
|
|
36
|
+
height: 0
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 按面积降序,squarified 的前提是大块先摆
|
|
41
|
+
const order = values.map((value, index) => ({
|
|
42
|
+
value,
|
|
43
|
+
index
|
|
44
|
+
})).sort((a, b) => b.value - a.value);
|
|
45
|
+
const result = values.map(() => ({
|
|
46
|
+
x: 0,
|
|
47
|
+
y: 0,
|
|
48
|
+
width: 0,
|
|
49
|
+
height: 0
|
|
50
|
+
}));
|
|
51
|
+
const area = rect.width * rect.height;
|
|
52
|
+
const scale = area / total;
|
|
53
|
+
let free = {
|
|
54
|
+
...rect
|
|
55
|
+
};
|
|
56
|
+
let cursor = 0;
|
|
57
|
+
while (cursor < order.length) {
|
|
58
|
+
const shortSide = Math.min(free.width, free.height);
|
|
59
|
+
if (shortSide <= 0) break;
|
|
60
|
+
|
|
61
|
+
// 贪心地把当前行填到「宽高比不再变好」为止
|
|
62
|
+
const row = [];
|
|
63
|
+
let rowSum = 0;
|
|
64
|
+
let bestRatio = Number.POSITIVE_INFINITY;
|
|
65
|
+
while (cursor < order.length) {
|
|
66
|
+
const candidate = order[cursor];
|
|
67
|
+
const nextSum = rowSum + candidate.value;
|
|
68
|
+
const ratio = worstAspectRatio([...row.map(item => item.value), candidate.value], nextSum * scale, shortSide);
|
|
69
|
+
if (row.length === 0 || ratio <= bestRatio) {
|
|
70
|
+
row.push(candidate);
|
|
71
|
+
rowSum = nextSum;
|
|
72
|
+
bestRatio = ratio;
|
|
73
|
+
cursor += 1;
|
|
74
|
+
} else {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 摆放这一行
|
|
80
|
+
const rowArea = rowSum * scale;
|
|
81
|
+
const horizontal = free.width >= free.height;
|
|
82
|
+
// 沿短边铺:短边是高时竖着排,短边是宽时横着排
|
|
83
|
+
const thickness = rowArea / Math.max(1e-6, horizontal ? free.height : free.width);
|
|
84
|
+
let offset = 0;
|
|
85
|
+
for (const item of row) {
|
|
86
|
+
const length = item.value * scale / Math.max(1e-6, thickness);
|
|
87
|
+
if (horizontal) {
|
|
88
|
+
result[item.index] = {
|
|
89
|
+
x: free.x,
|
|
90
|
+
y: free.y + offset,
|
|
91
|
+
width: thickness,
|
|
92
|
+
height: length
|
|
93
|
+
};
|
|
94
|
+
} else {
|
|
95
|
+
result[item.index] = {
|
|
96
|
+
x: free.x + offset,
|
|
97
|
+
y: free.y,
|
|
98
|
+
width: length,
|
|
99
|
+
height: thickness
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
offset += length;
|
|
103
|
+
}
|
|
104
|
+
if (horizontal) {
|
|
105
|
+
free = {
|
|
106
|
+
x: free.x + thickness,
|
|
107
|
+
y: free.y,
|
|
108
|
+
width: Math.max(0, free.width - thickness),
|
|
109
|
+
height: free.height
|
|
110
|
+
};
|
|
111
|
+
} else {
|
|
112
|
+
free = {
|
|
113
|
+
x: free.x,
|
|
114
|
+
y: free.y + thickness,
|
|
115
|
+
width: free.width,
|
|
116
|
+
height: Math.max(0, free.height - thickness)
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 一行的最差宽高比。
|
|
125
|
+
*
|
|
126
|
+
* 公式来自原论文:给定行内各块面积、该行总面积与所在边的长度,
|
|
127
|
+
* 返回值越接近 1,矩形越接近正方形。
|
|
128
|
+
*/
|
|
129
|
+
function worstAspectRatio(row, rowArea, side) {
|
|
130
|
+
if (rowArea <= 0 || side <= 0) return Number.POSITIVE_INFINITY;
|
|
131
|
+
const max = Math.max(...row);
|
|
132
|
+
const min = Math.min(...row);
|
|
133
|
+
if (min <= 0) return Number.POSITIVE_INFINITY;
|
|
134
|
+
return Math.max(side * side * max / (rowArea * rowArea), rowArea * rowArea / (side * side * min));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* 递归铺满:先对根的直接子节点做 squarify,再在每个矩形内部继续切分。
|
|
138
|
+
*/
|
|
139
|
+
export function buildTreemap(root, rect, options) {
|
|
140
|
+
const {
|
|
141
|
+
maxDepth: depthLimit = 1,
|
|
142
|
+
theme,
|
|
143
|
+
colorOf,
|
|
144
|
+
headerHeight = 0
|
|
145
|
+
} = options;
|
|
146
|
+
const cells = [];
|
|
147
|
+
const total = sumValues(root) || 1;
|
|
148
|
+
const walk = (node, area, depth, parentColor) => {
|
|
149
|
+
const children = node.children ?? [];
|
|
150
|
+
// depth 是「本层要渲染的层级」:maxDepth=1 时只渲染 depth 0
|
|
151
|
+
if (!children.length || depth >= depthLimit) return;
|
|
152
|
+
const values = children.map(sumValues);
|
|
153
|
+
const rects = squarify(values, area);
|
|
154
|
+
children.forEach((child, index) => {
|
|
155
|
+
const box = rects[index];
|
|
156
|
+
if (box.width <= 0 || box.height <= 0) return;
|
|
157
|
+
const color = child.color ?? colorOf?.(depth, index, parentColor) ?? theme.palette[index % theme.palette.length];
|
|
158
|
+
cells.push({
|
|
159
|
+
node: child,
|
|
160
|
+
depth,
|
|
161
|
+
x: box.x,
|
|
162
|
+
y: box.y,
|
|
163
|
+
width: box.width,
|
|
164
|
+
height: box.height,
|
|
165
|
+
ratio: Math.min(1, sumValues(child) / total),
|
|
166
|
+
color
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// 该格还会继续展开时,把子块的区域向下让出标题条
|
|
170
|
+
const willExpand = Boolean(child.children?.length) && depth + 1 < depthLimit;
|
|
171
|
+
const inner = willExpand && headerHeight > 0 && box.height > headerHeight + 24 ? {
|
|
172
|
+
x: box.x,
|
|
173
|
+
y: box.y + headerHeight,
|
|
174
|
+
width: box.width,
|
|
175
|
+
height: box.height - headerHeight
|
|
176
|
+
} : box;
|
|
177
|
+
walk(child, inner, depth + 1, color);
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
walk(root, rect, 0);
|
|
181
|
+
return cells;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 树图布局:横向展开(深度 → x),纵向按叶子顺序均分。
|
|
185
|
+
*
|
|
186
|
+
* 父节点的 y 取子节点 y 的均值,这样连线不会交叉,
|
|
187
|
+
* 也让「同一父节点下的兄弟」在视觉上聚成一簇。
|
|
188
|
+
*/
|
|
189
|
+
export function layoutTree(root, rect) {
|
|
190
|
+
const depth = Math.max(1, maxDepth(root));
|
|
191
|
+
const leaves = Math.max(1, countLeaves(root));
|
|
192
|
+
const rowHeight = rect.height / leaves;
|
|
193
|
+
const columnWidth = depth > 0 ? rect.width / depth : rect.width;
|
|
194
|
+
const nodes = [];
|
|
195
|
+
let leafCursor = 0;
|
|
196
|
+
const walk = (node, level, parent) => {
|
|
197
|
+
const index = nodes.length;
|
|
198
|
+
const x = rect.x + level * columnWidth;
|
|
199
|
+
nodes.push({
|
|
200
|
+
node,
|
|
201
|
+
depth: level,
|
|
202
|
+
x,
|
|
203
|
+
y: 0,
|
|
204
|
+
parent
|
|
205
|
+
});
|
|
206
|
+
if (!node.children?.length) {
|
|
207
|
+
const y = rect.y + leafCursor * rowHeight + rowHeight / 2;
|
|
208
|
+
leafCursor += 1;
|
|
209
|
+
nodes[index].y = y;
|
|
210
|
+
return y;
|
|
211
|
+
}
|
|
212
|
+
const childYs = node.children.map(child => walk(child, level + 1, index));
|
|
213
|
+
const y = childYs.reduce((sum, value) => sum + value, 0) / Math.max(1, childYs.length);
|
|
214
|
+
nodes[index].y = y;
|
|
215
|
+
return y;
|
|
216
|
+
};
|
|
217
|
+
walk(root, 0, -1);
|
|
218
|
+
return {
|
|
219
|
+
nodes,
|
|
220
|
+
depth
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 树图连线:正交折线(父 → 子),比贝塞尔更清晰地表达层级 */
|
|
225
|
+
export function treeLinkPath(parent, child, nodeWidth) {
|
|
226
|
+
const x1 = parent.x + nodeWidth;
|
|
227
|
+
const y1 = parent.y;
|
|
228
|
+
const x2 = child.x;
|
|
229
|
+
const y2 = child.y;
|
|
230
|
+
const midX = x1 + (x2 - x1) / 2;
|
|
231
|
+
return `M${x1},${y1}H${midX}V${y2}H${x2}`;
|
|
232
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ChartTheme } from '../theme';
|
|
2
|
+
/**
|
|
3
|
+
* 布局计算:把「容器尺寸」换算成「绘图区」。
|
|
4
|
+
*
|
|
5
|
+
* 难点在于坐标轴文字会占用边缘空间,而可用的边缘空间又取决于容器大小 ——
|
|
6
|
+
* 这里采用业界通行的两遍法:
|
|
7
|
+
* 第一遍用完整容器高度估算刻度数量 → 得到刻度标签;
|
|
8
|
+
* 第二遍按标签实际宽度(估算)反推 margin。
|
|
9
|
+
* 两遍都是纯计算,不触碰 DOM,因此可在 SSR 与测试中直接跑。
|
|
10
|
+
*/
|
|
11
|
+
export interface Margin {
|
|
12
|
+
top: number;
|
|
13
|
+
right: number;
|
|
14
|
+
bottom: number;
|
|
15
|
+
left: number;
|
|
16
|
+
}
|
|
17
|
+
export interface PlotArea {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
}
|
|
23
|
+
/** x 轴标签的排布方式 */
|
|
24
|
+
export type LabelLayout = 'horizontal' | 'rotate' | 'hide';
|
|
25
|
+
export interface XAxisLayout {
|
|
26
|
+
layout: LabelLayout;
|
|
27
|
+
/** 旋转角度(负值为逆时针) */
|
|
28
|
+
rotate: number;
|
|
29
|
+
/** 字号 */
|
|
30
|
+
fontSize: number;
|
|
31
|
+
/** 该轴需要占用的底部空间 */
|
|
32
|
+
height: number;
|
|
33
|
+
/** 每隔几个标签显示一个,0 表示全部显示 */
|
|
34
|
+
interval: number;
|
|
35
|
+
}
|
|
36
|
+
export interface ComputeLayoutInput {
|
|
37
|
+
width: number;
|
|
38
|
+
height: number;
|
|
39
|
+
/** y 轴刻度标签文本 */
|
|
40
|
+
yLabels: string[];
|
|
41
|
+
/** x 轴刻度标签文本 */
|
|
42
|
+
xLabels: string[];
|
|
43
|
+
theme: ChartTheme;
|
|
44
|
+
/** 是否显示 x 轴,false 时不预留底部空间 */
|
|
45
|
+
showXAxis?: boolean;
|
|
46
|
+
showYAxis?: boolean;
|
|
47
|
+
/** 用户强制指定的 margin 覆盖项 */
|
|
48
|
+
margin?: Partial<Margin>;
|
|
49
|
+
}
|
|
50
|
+
export interface LayoutResult {
|
|
51
|
+
plot: PlotArea;
|
|
52
|
+
margin: Margin;
|
|
53
|
+
xAxis: XAxisLayout;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 计算 x 轴标签布局。
|
|
57
|
+
*
|
|
58
|
+
* 判定顺序:
|
|
59
|
+
* 1. 全部横排放得下 → horizontal
|
|
60
|
+
* 2. 斜排 35° 放得下 → rotate
|
|
61
|
+
* 3. 都不行 → 抽样显示(interval),强制横排,避免出现密集的"斜线森林"
|
|
62
|
+
*/
|
|
63
|
+
export declare function computeXAxisLayout(labels: readonly string[], availableWidth: number, theme: ChartTheme): XAxisLayout;
|
|
64
|
+
export declare function computeLayout(input: ComputeLayoutInput): LayoutResult;
|
|
65
|
+
/** 数值轴刻度数量的经验值:约每 44px 一个刻度,限制在 3~8 之间 */
|
|
66
|
+
export declare function suggestTickCount(plotHeight: number): number;
|
|
67
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["layout.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C;;;;;;;;GAQG;AAEH,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,iBAAiB;AACjB,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE3D,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,WAAW,CAAC;IACpB,mBAAmB;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS;IACT,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,0BAA0B;IAC1B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,gBAAgB;IAChB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,gBAAgB;IAChB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,KAAK,EAAE,UAAU,CAAC;IAClB,8BAA8B;IAC9B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,yBAAyB;IACzB,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,WAAW,CAAC;CACpB;AAID;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,cAAc,EAAE,MAAM,EACtB,KAAK,EAAE,UAAU,GAChB,WAAW,CAwDb;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,YAAY,CAoDrE;AAED,0CAA0C;AAC1C,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAE3D"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { estimateTextWidth } from "./format";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 布局计算:把「容器尺寸」换算成「绘图区」。
|
|
5
|
+
*
|
|
6
|
+
* 难点在于坐标轴文字会占用边缘空间,而可用的边缘空间又取决于容器大小 ——
|
|
7
|
+
* 这里采用业界通行的两遍法:
|
|
8
|
+
* 第一遍用完整容器高度估算刻度数量 → 得到刻度标签;
|
|
9
|
+
* 第二遍按标签实际宽度(估算)反推 margin。
|
|
10
|
+
* 两遍都是纯计算,不触碰 DOM,因此可在 SSR 与测试中直接跑。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** x 轴标签的排布方式 */
|
|
14
|
+
|
|
15
|
+
const AXIS_GAP = 8;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 计算 x 轴标签布局。
|
|
19
|
+
*
|
|
20
|
+
* 判定顺序:
|
|
21
|
+
* 1. 全部横排放得下 → horizontal
|
|
22
|
+
* 2. 斜排 35° 放得下 → rotate
|
|
23
|
+
* 3. 都不行 → 抽样显示(interval),强制横排,避免出现密集的"斜线森林"
|
|
24
|
+
*/
|
|
25
|
+
export function computeXAxisLayout(labels, availableWidth, theme) {
|
|
26
|
+
const fontSize = theme.font.size;
|
|
27
|
+
const count = labels.length;
|
|
28
|
+
if (count === 0) {
|
|
29
|
+
return {
|
|
30
|
+
layout: 'horizontal',
|
|
31
|
+
rotate: 0,
|
|
32
|
+
fontSize,
|
|
33
|
+
height: 0,
|
|
34
|
+
interval: 0
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const widths = labels.map(label => estimateTextWidth(label, fontSize));
|
|
38
|
+
const maxWidth = Math.max(...widths);
|
|
39
|
+
const slot = availableWidth / count;
|
|
40
|
+
if (maxWidth + 12 <= slot) {
|
|
41
|
+
return {
|
|
42
|
+
layout: 'horizontal',
|
|
43
|
+
rotate: 0,
|
|
44
|
+
fontSize,
|
|
45
|
+
height: fontSize + AXIS_GAP,
|
|
46
|
+
interval: 0
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const rotate = -35;
|
|
50
|
+
const rad = Math.abs(rotate) * Math.PI / 180;
|
|
51
|
+
const rotatedWidth = maxWidth * Math.cos(rad) + fontSize * Math.sin(rad);
|
|
52
|
+
if (rotatedWidth + 4 <= slot) {
|
|
53
|
+
const height = Math.ceil(maxWidth * Math.sin(rad) + fontSize * Math.cos(rad)) + AXIS_GAP;
|
|
54
|
+
return {
|
|
55
|
+
layout: 'rotate',
|
|
56
|
+
rotate,
|
|
57
|
+
fontSize,
|
|
58
|
+
height: Math.min(height, 72),
|
|
59
|
+
interval: 0
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 抽样:至少保证标签间距不小于 1.6 个字符宽
|
|
64
|
+
const interval = Math.max(1, Math.ceil(count / Math.max(1, Math.floor(availableWidth / (maxWidth + 12)))));
|
|
65
|
+
return {
|
|
66
|
+
layout: 'horizontal',
|
|
67
|
+
rotate: 0,
|
|
68
|
+
fontSize,
|
|
69
|
+
height: fontSize + AXIS_GAP,
|
|
70
|
+
interval
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export function computeLayout(input) {
|
|
74
|
+
const {
|
|
75
|
+
width,
|
|
76
|
+
height,
|
|
77
|
+
yLabels,
|
|
78
|
+
xLabels,
|
|
79
|
+
theme,
|
|
80
|
+
showXAxis = true,
|
|
81
|
+
showYAxis = true
|
|
82
|
+
} = input;
|
|
83
|
+
const maxYLabelWidth = yLabels.length ? Math.max(...yLabels.map(label => estimateTextWidth(label, theme.font.size))) : 0;
|
|
84
|
+
const left = showYAxis ? Math.ceil(maxYLabelWidth + theme.axis.tickSize + AXIS_GAP) : 12;
|
|
85
|
+
const right = 16;
|
|
86
|
+
|
|
87
|
+
// 第一遍:先按横向标签预留底部空间,得出可用宽度
|
|
88
|
+
const provisionalPlotWidth = Math.max(0, width - left - right);
|
|
89
|
+
const xAxis = showXAxis ? computeXAxisLayout(xLabels, provisionalPlotWidth, theme) : {
|
|
90
|
+
layout: 'horizontal',
|
|
91
|
+
rotate: 0,
|
|
92
|
+
fontSize: theme.font.size,
|
|
93
|
+
height: 0,
|
|
94
|
+
interval: 0
|
|
95
|
+
};
|
|
96
|
+
const bottom = showXAxis ? xAxis.height + 4 : 12;
|
|
97
|
+
const top = 12;
|
|
98
|
+
const margin = {
|
|
99
|
+
top: input.margin?.top ?? top,
|
|
100
|
+
right: input.margin?.right ?? right,
|
|
101
|
+
bottom: input.margin?.bottom ?? bottom,
|
|
102
|
+
left: input.margin?.left ?? left
|
|
103
|
+
};
|
|
104
|
+
const plot = {
|
|
105
|
+
x: margin.left,
|
|
106
|
+
y: margin.top,
|
|
107
|
+
width: Math.max(0, width - margin.left - margin.right),
|
|
108
|
+
height: Math.max(0, height - margin.top - margin.bottom)
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
plot,
|
|
112
|
+
margin,
|
|
113
|
+
xAxis
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 数值轴刻度数量的经验值:约每 44px 一个刻度,限制在 3~8 之间 */
|
|
118
|
+
export function suggestTickCount(plotHeight) {
|
|
119
|
+
return Math.min(8, Math.max(3, Math.round(plotHeight / 44)));
|
|
120
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 降采样。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:数据点超过几百个后,屏幕像素已经不够区分相邻点了,
|
|
5
|
+
* 继续逐点渲染只会拖慢滚动而不会增加信息量。
|
|
6
|
+
* 但「每隔 N 个取一个」会丢掉尖峰——那些恰恰是读者最关心的。
|
|
7
|
+
*/
|
|
8
|
+
export interface SamplePoint {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number | null;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Largest-Triangle-Three-Buckets(LTTB)。
|
|
14
|
+
*
|
|
15
|
+
* 把数据切成 threshold 个桶,每个桶里选一个点,使得它与前后选中点
|
|
16
|
+
* 构成的三角形面积最大——面积大意味着这个点「离直线最远」,
|
|
17
|
+
* 也就是最能代表该段形状特征的极值点。
|
|
18
|
+
*
|
|
19
|
+
* 复杂度 O(n),且能保住尖峰,是目前综合表现最好的保形降采样之一。
|
|
20
|
+
*
|
|
21
|
+
* @returns 选中点的原始下标(升序),首尾必选
|
|
22
|
+
*/
|
|
23
|
+
export declare function lttbIndices(points: readonly SamplePoint[], threshold: number): number[];
|
|
24
|
+
/**
|
|
25
|
+
* 对「多系列共享 x」的数据做采样。
|
|
26
|
+
*
|
|
27
|
+
* 关键点:**必须按 x 下标采样一次,再让所有系列共用这套下标**。
|
|
28
|
+
* 每个系列各自采样会得到不同的下标集合,折线之间就会错位,
|
|
29
|
+
* 看起来像两列数据在互相穿插。
|
|
30
|
+
*/
|
|
31
|
+
export declare function sampleSeriesIndices(values: readonly (number | null)[][], threshold: number): number[];
|
|
32
|
+
//# sourceMappingURL=sampling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sampling.d.ts","sourceRoot":"","sources":["sampling.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,WAAW;IAC1B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAClB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,SAAS,WAAW,EAAE,EAC9B,SAAS,EAAE,MAAM,GAChB,MAAM,EAAE,CAuFV;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,EAAE,EACpC,SAAS,EAAE,MAAM,GAChB,MAAM,EAAE,CAoBV"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 降采样。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:数据点超过几百个后,屏幕像素已经不够区分相邻点了,
|
|
5
|
+
* 继续逐点渲染只会拖慢滚动而不会增加信息量。
|
|
6
|
+
* 但「每隔 N 个取一个」会丢掉尖峰——那些恰恰是读者最关心的。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Largest-Triangle-Three-Buckets(LTTB)。
|
|
11
|
+
*
|
|
12
|
+
* 把数据切成 threshold 个桶,每个桶里选一个点,使得它与前后选中点
|
|
13
|
+
* 构成的三角形面积最大——面积大意味着这个点「离直线最远」,
|
|
14
|
+
* 也就是最能代表该段形状特征的极值点。
|
|
15
|
+
*
|
|
16
|
+
* 复杂度 O(n),且能保住尖峰,是目前综合表现最好的保形降采样之一。
|
|
17
|
+
*
|
|
18
|
+
* @returns 选中点的原始下标(升序),首尾必选
|
|
19
|
+
*/
|
|
20
|
+
export function lttbIndices(points, threshold) {
|
|
21
|
+
const length = points.length;
|
|
22
|
+
if (length <= 2) return points.map((_, index) => index);
|
|
23
|
+
if (threshold >= length) {
|
|
24
|
+
// 不需要降采样,直接全取
|
|
25
|
+
return points.map((_, index) => index);
|
|
26
|
+
}
|
|
27
|
+
if (threshold <= 2) {
|
|
28
|
+
// 只保留首尾,至少能看出整体范围
|
|
29
|
+
return [0, length - 1];
|
|
30
|
+
}
|
|
31
|
+
const indices = [0];
|
|
32
|
+
// 每桶的跨度;最后一个点单独处理,所以是 (n-2)/(threshold-2)
|
|
33
|
+
const bucketSize = (length - 2) / (threshold - 2);
|
|
34
|
+
let previous = 0;
|
|
35
|
+
for (let bucket = 0; bucket < threshold - 2; bucket += 1) {
|
|
36
|
+
// 下一桶的平均点,用来算三角形面积
|
|
37
|
+
const nextStart = Math.floor((bucket + 1) * bucketSize) + 1;
|
|
38
|
+
const nextEnd = Math.min(Math.floor((bucket + 2) * bucketSize) + 1, length);
|
|
39
|
+
let averageX = 0;
|
|
40
|
+
let averageY = 0;
|
|
41
|
+
let averageCount = 0;
|
|
42
|
+
for (let index = nextStart; index < nextEnd; index += 1) {
|
|
43
|
+
const point = points[index];
|
|
44
|
+
if (point.y === null || !Number.isFinite(point.y)) continue;
|
|
45
|
+
averageX += point.x;
|
|
46
|
+
averageY += point.y;
|
|
47
|
+
averageCount += 1;
|
|
48
|
+
}
|
|
49
|
+
if (averageCount === 0) {
|
|
50
|
+
// 下一桶全是缺失值,就退化为取桶中点
|
|
51
|
+
const fallback = Math.min(Math.floor(nextStart), length - 1);
|
|
52
|
+
averageX = points[fallback].x;
|
|
53
|
+
averageY = 0;
|
|
54
|
+
averageCount = 1;
|
|
55
|
+
}
|
|
56
|
+
averageX /= averageCount;
|
|
57
|
+
averageY /= averageCount;
|
|
58
|
+
const rangeStart = Math.floor(bucket * bucketSize) + 1;
|
|
59
|
+
const rangeEnd = Math.min(Math.floor((bucket + 1) * bucketSize) + 1, length - 1);
|
|
60
|
+
const anchor = points[previous];
|
|
61
|
+
let maxArea = -1;
|
|
62
|
+
let chosen = -1;
|
|
63
|
+
|
|
64
|
+
// 锚点本身缺失时无法算面积,退化为取桶内第一个有效点
|
|
65
|
+
if (anchor.y === null || !Number.isFinite(anchor.y)) {
|
|
66
|
+
for (let index = rangeStart; index < rangeEnd; index += 1) {
|
|
67
|
+
if (points[index].y !== null) {
|
|
68
|
+
chosen = index;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (chosen < 0) chosen = rangeStart;
|
|
73
|
+
} else {
|
|
74
|
+
const anchorY = anchor.y;
|
|
75
|
+
for (let index = rangeStart; index < rangeEnd; index += 1) {
|
|
76
|
+
const point = points[index];
|
|
77
|
+
if (point.y === null || !Number.isFinite(point.y)) continue;
|
|
78
|
+
// 三角形面积的两倍(省掉除以 2)
|
|
79
|
+
const area = Math.abs((anchor.x - averageX) * (point.y - anchorY) - (anchor.x - point.x) * (averageY - anchorY));
|
|
80
|
+
if (area > maxArea) {
|
|
81
|
+
maxArea = area;
|
|
82
|
+
chosen = index;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (chosen < 0) chosen = rangeStart;
|
|
86
|
+
}
|
|
87
|
+
indices.push(chosen);
|
|
88
|
+
previous = chosen;
|
|
89
|
+
}
|
|
90
|
+
indices.push(length - 1);
|
|
91
|
+
return indices;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 对「多系列共享 x」的数据做采样。
|
|
96
|
+
*
|
|
97
|
+
* 关键点:**必须按 x 下标采样一次,再让所有系列共用这套下标**。
|
|
98
|
+
* 每个系列各自采样会得到不同的下标集合,折线之间就会错位,
|
|
99
|
+
* 看起来像两列数据在互相穿插。
|
|
100
|
+
*/
|
|
101
|
+
export function sampleSeriesIndices(values, threshold) {
|
|
102
|
+
const pointCount = values[0]?.length ?? 0;
|
|
103
|
+
if (pointCount === 0) return [];
|
|
104
|
+
|
|
105
|
+
// 用各系列的绝对值之和作为「形状强度」,避免只看第一条系列导致别的系列丢峰
|
|
106
|
+
const combined = [];
|
|
107
|
+
for (let index = 0; index < pointCount; index += 1) {
|
|
108
|
+
let sum = 0;
|
|
109
|
+
let hasValue = false;
|
|
110
|
+
for (const row of values) {
|
|
111
|
+
const value = row[index];
|
|
112
|
+
if (value !== null && Number.isFinite(value)) {
|
|
113
|
+
sum += Math.abs(value);
|
|
114
|
+
hasValue = true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
combined.push({
|
|
118
|
+
x: index,
|
|
119
|
+
y: hasValue ? sum : null
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return lttbIndices(combined, threshold);
|
|
123
|
+
}
|