@aura-react-comp/charts 0.2.0 → 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.
Files changed (98) hide show
  1. package/esm/charts/area-chart/index.d.ts +2 -2
  2. package/esm/charts/area-chart/index.js +28 -23
  3. package/esm/charts/bar-chart/index.d.ts +1 -1
  4. package/esm/charts/bar-chart/index.js +18 -21
  5. package/esm/charts/candlestick-chart/index.d.ts +2 -2
  6. package/esm/charts/candlestick-chart/index.js +243 -180
  7. package/esm/charts/combo-chart/index.d.ts +1 -1
  8. package/esm/charts/combo-chart/index.js +9 -20
  9. package/esm/charts/funnel-chart/index.d.ts +2 -2
  10. package/esm/charts/funnel-chart/index.js +134 -108
  11. package/esm/charts/gauge-chart/index.d.ts +1 -1
  12. package/esm/charts/gauge-chart/index.js +103 -105
  13. package/esm/charts/graph-chart/index.d.ts +3 -3
  14. package/esm/charts/graph-chart/index.js +155 -114
  15. package/esm/charts/heatmap-chart/index.d.ts +2 -2
  16. package/esm/charts/heatmap-chart/index.js +180 -135
  17. package/esm/charts/line-chart/index.d.ts +2 -2
  18. package/esm/charts/line-chart/index.js +29 -24
  19. package/esm/charts/pie-chart/index.d.ts +2 -2
  20. package/esm/charts/pie-chart/index.d.ts.map +1 -1
  21. package/esm/charts/pie-chart/index.js +199 -139
  22. package/esm/charts/radar-chart/index.d.ts +2 -2
  23. package/esm/charts/radar-chart/index.d.ts.map +1 -1
  24. package/esm/charts/radar-chart/index.js +155 -128
  25. package/esm/charts/ring-progress/index.d.ts +1 -1
  26. package/esm/charts/ring-progress/index.js +82 -82
  27. package/esm/charts/sankey-chart/index.d.ts +3 -3
  28. package/esm/charts/sankey-chart/index.js +172 -141
  29. package/esm/charts/scatter-chart/index.d.ts +3 -3
  30. package/esm/charts/scatter-chart/index.d.ts.map +1 -1
  31. package/esm/charts/scatter-chart/index.js +226 -159
  32. package/esm/charts/sparkline/index.d.ts +1 -1
  33. package/esm/charts/sparkline/index.js +82 -66
  34. package/esm/charts/tree-chart/index.d.ts +3 -3
  35. package/esm/charts/tree-chart/index.js +129 -102
  36. package/esm/charts/treemap-chart/index.d.ts +3 -3
  37. package/esm/charts/treemap-chart/index.js +163 -141
  38. package/esm/charts/waterfall-chart/index.d.ts +2 -2
  39. package/esm/charts/waterfall-chart/index.js +166 -121
  40. package/esm/components/Legend.d.ts +5 -4
  41. package/esm/components/Legend.d.ts.map +1 -1
  42. package/esm/components/Legend.js +80 -48
  43. package/esm/components/Tooltip.d.ts +2 -2
  44. package/esm/components/Tooltip.js +63 -71
  45. package/esm/components/chart/index.d.ts +5 -2
  46. package/esm/components/chart/index.d.ts.map +1 -1
  47. package/esm/components/chart/index.js +81 -102
  48. package/esm/components/chart.less +13 -0
  49. package/esm/core/animation.js +32 -43
  50. package/esm/core/export.d.ts +34 -0
  51. package/esm/core/export.d.ts.map +1 -0
  52. package/esm/core/export.js +390 -0
  53. package/esm/core/force.js +23 -45
  54. package/esm/core/format.js +49 -52
  55. package/esm/core/hierarchy.d.ts +1 -1
  56. package/esm/core/hierarchy.js +61 -92
  57. package/esm/core/layout.d.ts +1 -1
  58. package/esm/core/layout.js +33 -50
  59. package/esm/core/sampling.js +31 -53
  60. package/esm/core/sankey.js +36 -55
  61. package/esm/core/scale.js +73 -86
  62. package/esm/core/shape.js +102 -133
  63. package/esm/core/sync.d.ts +41 -0
  64. package/esm/core/sync.d.ts.map +1 -0
  65. package/esm/core/sync.js +120 -0
  66. package/esm/hooks/useChartExport.d.ts +19 -0
  67. package/esm/hooks/useChartExport.d.ts.map +1 -0
  68. package/esm/hooks/useChartExport.js +35 -0
  69. package/esm/hooks/useChartTheme.d.ts +6 -5
  70. package/esm/hooks/useChartTheme.d.ts.map +1 -1
  71. package/esm/hooks/useChartTheme.js +18 -18
  72. package/esm/hooks/useChartTransition.d.ts +1 -1
  73. package/esm/hooks/useChartTransition.js +59 -73
  74. package/esm/hooks/useElementSize.js +20 -28
  75. package/esm/hooks/usePlotPointer.d.ts +23 -0
  76. package/esm/hooks/usePlotPointer.d.ts.map +1 -0
  77. package/esm/hooks/usePlotPointer.js +94 -0
  78. package/esm/index.d.ts +56 -50
  79. package/esm/index.d.ts.map +1 -1
  80. package/esm/index.js +137 -47
  81. package/esm/internal/CartesianChart.d.ts +1 -1
  82. package/esm/internal/CartesianChart.d.ts.map +1 -1
  83. package/esm/internal/CartesianChart.js +667 -485
  84. package/esm/internal/model.d.ts +2 -2
  85. package/esm/internal/model.js +45 -60
  86. package/esm/scene/canvas.d.ts +1 -1
  87. package/esm/scene/canvas.js +101 -107
  88. package/esm/scene/svg.d.ts +1 -1
  89. package/esm/scene/svg.js +107 -99
  90. package/esm/scene/types.js +9 -15
  91. package/esm/style.css +10 -0
  92. package/esm/theme/index.d.ts +2 -2
  93. package/esm/theme/index.d.ts.map +1 -1
  94. package/esm/theme/index.js +145 -170
  95. package/esm/types.d.ts +7 -3
  96. package/esm/types.d.ts.map +1 -1
  97. package/esm/types.js +1 -1
  98. package/package.json +2 -2
@@ -1,47 +1,30 @@
1
- /** 层级数据:叶子节点用 value,分支节点可不填(由子节点求和) */
2
-
3
- /** 递归求和:分支节点取子节点之和,叶子取自身 value */
4
- export function sumValues(node) {
5
- if (node.children?.length) {
1
+ 'use client';
2
+ // src/core/hierarchy.ts
3
+ function sumValues(node) {
4
+ var _a;
5
+ if ((_a = node.children) == null ? void 0 : _a.length) {
6
6
  return node.children.reduce((sum, child) => sum + sumValues(child), 0);
7
7
  }
8
8
  return Math.max(0, node.value ?? 0);
9
9
  }
10
-
11
- /** 叶子总数,用于树图的纵向排布 */
12
- export function countLeaves(node) {
13
- if (!node.children?.length) return 1;
10
+ function countLeaves(node) {
11
+ var _a;
12
+ if (!((_a = node.children) == null ? void 0 : _a.length))
13
+ return 1;
14
14
  return node.children.reduce((sum, child) => sum + countLeaves(child), 0);
15
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)));
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)));
21
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) {
22
+ function squarify(values, rect) {
30
23
  const total = values.reduce((sum, value) => sum + value, 0);
31
24
  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
- }));
25
+ return values.map(() => ({ x: rect.x, y: rect.y, width: 0, height: 0 }));
38
26
  }
39
-
40
- // 按面积降序,squarified 的前提是大块先摆
41
- const order = values.map((value, index) => ({
42
- value,
43
- index
44
- })).sort((a, b) => b.value - a.value);
27
+ const order = values.map((value, index) => ({ value, index })).sort((a, b) => b.value - a.value);
45
28
  const result = values.map(() => ({
46
29
  x: 0,
47
30
  y: 0,
@@ -50,22 +33,23 @@ export function squarify(values, rect) {
50
33
  }));
51
34
  const area = rect.width * rect.height;
52
35
  const scale = area / total;
53
- let free = {
54
- ...rect
55
- };
36
+ let free = { ...rect };
56
37
  let cursor = 0;
57
38
  while (cursor < order.length) {
58
39
  const shortSide = Math.min(free.width, free.height);
59
- if (shortSide <= 0) break;
60
-
61
- // 贪心地把当前行填到「宽高比不再变好」为止
40
+ if (shortSide <= 0)
41
+ break;
62
42
  const row = [];
63
43
  let rowSum = 0;
64
44
  let bestRatio = Number.POSITIVE_INFINITY;
65
45
  while (cursor < order.length) {
66
46
  const candidate = order[cursor];
67
47
  const nextSum = rowSum + candidate.value;
68
- const ratio = worstAspectRatio([...row.map(item => item.value), candidate.value], nextSum * scale, shortSide);
48
+ const ratio = worstAspectRatio(
49
+ [...row.map((item) => item.value), candidate.value],
50
+ nextSum * scale,
51
+ shortSide
52
+ );
69
53
  if (row.length === 0 || ratio <= bestRatio) {
70
54
  row.push(candidate);
71
55
  rowSum = nextSum;
@@ -75,11 +59,8 @@ export function squarify(values, rect) {
75
59
  break;
76
60
  }
77
61
  }
78
-
79
- // 摆放这一行
80
62
  const rowArea = rowSum * scale;
81
63
  const horizontal = free.width >= free.height;
82
- // 沿短边铺:短边是高时竖着排,短边是宽时横着排
83
64
  const thickness = rowArea / Math.max(1e-6, horizontal ? free.height : free.width);
84
65
  let offset = 0;
85
66
  for (const item of row) {
@@ -119,24 +100,19 @@ export function squarify(values, rect) {
119
100
  }
120
101
  return result;
121
102
  }
122
-
123
- /**
124
- * 一行的最差宽高比。
125
- *
126
- * 公式来自原论文:给定行内各块面积、该行总面积与所在边的长度,
127
- * 返回值越接近 1,矩形越接近正方形。
128
- */
129
103
  function worstAspectRatio(row, rowArea, side) {
130
- if (rowArea <= 0 || side <= 0) return Number.POSITIVE_INFINITY;
104
+ if (rowArea <= 0 || side <= 0)
105
+ return Number.POSITIVE_INFINITY;
131
106
  const max = Math.max(...row);
132
107
  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));
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
+ );
135
114
  }
136
- /**
137
- * 递归铺满:先对根的直接子节点做 squarify,再在每个矩形内部继续切分。
138
- */
139
- export function buildTreemap(root, rect, options) {
115
+ function buildTreemap(root, rect, options) {
140
116
  const {
141
117
  maxDepth: depthLimit = 1,
142
118
  theme,
@@ -147,14 +123,16 @@ export function buildTreemap(root, rect, options) {
147
123
  const total = sumValues(root) || 1;
148
124
  const walk = (node, area, depth, parentColor) => {
149
125
  const children = node.children ?? [];
150
- // depth 是「本层要渲染的层级」:maxDepth=1 时只渲染 depth 0
151
- if (!children.length || depth >= depthLimit) return;
126
+ if (!children.length || depth >= depthLimit)
127
+ return;
152
128
  const values = children.map(sumValues);
153
129
  const rects = squarify(values, area);
154
130
  children.forEach((child, index) => {
131
+ var _a;
155
132
  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];
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];
158
136
  cells.push({
159
137
  node: child,
160
138
  depth,
@@ -165,9 +143,7 @@ export function buildTreemap(root, rect, options) {
165
143
  ratio: Math.min(1, sumValues(child) / total),
166
144
  color
167
145
  });
168
-
169
- // 该格还会继续展开时,把子块的区域向下让出标题条
170
- const willExpand = Boolean(child.children?.length) && depth + 1 < depthLimit;
146
+ const willExpand = Boolean((_a = child.children) == null ? void 0 : _a.length) && depth + 1 < depthLimit;
171
147
  const inner = willExpand && headerHeight > 0 && box.height > headerHeight + 24 ? {
172
148
  x: box.x,
173
149
  y: box.y + headerHeight,
@@ -180,13 +156,7 @@ export function buildTreemap(root, rect, options) {
180
156
  walk(root, rect, 0);
181
157
  return cells;
182
158
  }
183
- /**
184
- * 树图布局:横向展开(深度 → x),纵向按叶子顺序均分。
185
- *
186
- * 父节点的 y 取子节点 y 的均值,这样连线不会交叉,
187
- * 也让「同一父节点下的兄弟」在视觉上聚成一簇。
188
- */
189
- export function layoutTree(root, rect) {
159
+ function layoutTree(root, rect) {
190
160
  const depth = Math.max(1, maxDepth(root));
191
161
  const leaves = Math.max(1, countLeaves(root));
192
162
  const rowHeight = rect.height / leaves;
@@ -194,39 +164,38 @@ export function layoutTree(root, rect) {
194
164
  const nodes = [];
195
165
  let leafCursor = 0;
196
166
  const walk = (node, level, parent) => {
167
+ var _a;
197
168
  const index = nodes.length;
198
169
  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;
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;
208
173
  leafCursor += 1;
209
- nodes[index].y = y;
210
- return y;
174
+ nodes[index].y = y2;
175
+ return y2;
211
176
  }
212
- const childYs = node.children.map(child => walk(child, level + 1, index));
177
+ const childYs = node.children.map((child) => walk(child, level + 1, index));
213
178
  const y = childYs.reduce((sum, value) => sum + value, 0) / Math.max(1, childYs.length);
214
179
  nodes[index].y = y;
215
180
  return y;
216
181
  };
217
182
  walk(root, 0, -1);
218
- return {
219
- nodes,
220
- depth
221
- };
183
+ return { nodes, depth };
222
184
  }
223
-
224
- /** 树图连线:正交折线(父 → 子),比贝塞尔更清晰地表达层级 */
225
- export function treeLinkPath(parent, child, nodeWidth) {
185
+ function treeLinkPath(parent, child, nodeWidth) {
226
186
  const x1 = parent.x + nodeWidth;
227
187
  const y1 = parent.y;
228
188
  const x2 = child.x;
229
189
  const y2 = child.y;
230
190
  const midX = x1 + (x2 - x1) / 2;
231
191
  return `M${x1},${y1}H${midX}V${y2}H${x2}`;
232
- }
192
+ }
193
+ export {
194
+ buildTreemap,
195
+ countLeaves,
196
+ layoutTree,
197
+ maxDepth,
198
+ squarify,
199
+ sumValues,
200
+ treeLinkPath
201
+ };
@@ -1,4 +1,4 @@
1
- import type { ChartTheme } from '../theme';
1
+ import type { ChartTheme } from '../theme/index.js';
2
2
  /**
3
3
  * 布局计算:把「容器尺寸」换算成「绘图区」。
4
4
  *
@@ -1,45 +1,25 @@
1
+ 'use client';
2
+ // src/core/layout.ts
1
3
  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) {
4
+ var AXIS_GAP = 8;
5
+ function computeXAxisLayout(labels, availableWidth, theme) {
26
6
  const fontSize = theme.font.size;
27
7
  const count = labels.length;
28
8
  if (count === 0) {
29
9
  return {
30
- layout: 'horizontal',
10
+ layout: "horizontal",
31
11
  rotate: 0,
32
12
  fontSize,
33
13
  height: 0,
34
14
  interval: 0
35
15
  };
36
16
  }
37
- const widths = labels.map(label => estimateTextWidth(label, fontSize));
17
+ const widths = labels.map((label) => estimateTextWidth(label, fontSize));
38
18
  const maxWidth = Math.max(...widths);
39
19
  const slot = availableWidth / count;
40
20
  if (maxWidth + 12 <= slot) {
41
21
  return {
42
- layout: 'horizontal',
22
+ layout: "horizontal",
43
23
  rotate: 0,
44
24
  fontSize,
45
25
  height: fontSize + AXIS_GAP,
@@ -52,25 +32,29 @@ export function computeXAxisLayout(labels, availableWidth, theme) {
52
32
  if (rotatedWidth + 4 <= slot) {
53
33
  const height = Math.ceil(maxWidth * Math.sin(rad) + fontSize * Math.cos(rad)) + AXIS_GAP;
54
34
  return {
55
- layout: 'rotate',
35
+ layout: "rotate",
56
36
  rotate,
57
37
  fontSize,
58
38
  height: Math.min(height, 72),
59
39
  interval: 0
60
40
  };
61
41
  }
62
-
63
- // 抽样:至少保证标签间距不小于 1.6 个字符宽
64
- const interval = Math.max(1, Math.ceil(count / Math.max(1, Math.floor(availableWidth / (maxWidth + 12)))));
42
+ const interval = Math.max(
43
+ 1,
44
+ Math.ceil(
45
+ count / Math.max(1, Math.floor(availableWidth / (maxWidth + 12)))
46
+ )
47
+ );
65
48
  return {
66
- layout: 'horizontal',
49
+ layout: "horizontal",
67
50
  rotate: 0,
68
51
  fontSize,
69
52
  height: fontSize + AXIS_GAP,
70
53
  interval
71
54
  };
72
55
  }
73
- export function computeLayout(input) {
56
+ function computeLayout(input) {
57
+ var _a, _b, _c, _d;
74
58
  const {
75
59
  width,
76
60
  height,
@@ -80,14 +64,14 @@ export function computeLayout(input) {
80
64
  showXAxis = true,
81
65
  showYAxis = true
82
66
  } = input;
83
- const maxYLabelWidth = yLabels.length ? Math.max(...yLabels.map(label => estimateTextWidth(label, theme.font.size))) : 0;
67
+ const maxYLabelWidth = yLabels.length ? Math.max(
68
+ ...yLabels.map((label) => estimateTextWidth(label, theme.font.size))
69
+ ) : 0;
84
70
  const left = showYAxis ? Math.ceil(maxYLabelWidth + theme.axis.tickSize + AXIS_GAP) : 12;
85
71
  const right = 16;
86
-
87
- // 第一遍:先按横向标签预留底部空间,得出可用宽度
88
72
  const provisionalPlotWidth = Math.max(0, width - left - right);
89
73
  const xAxis = showXAxis ? computeXAxisLayout(xLabels, provisionalPlotWidth, theme) : {
90
- layout: 'horizontal',
74
+ layout: "horizontal",
91
75
  rotate: 0,
92
76
  fontSize: theme.font.size,
93
77
  height: 0,
@@ -96,10 +80,10 @@ export function computeLayout(input) {
96
80
  const bottom = showXAxis ? xAxis.height + 4 : 12;
97
81
  const top = 12;
98
82
  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
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
103
87
  };
104
88
  const plot = {
105
89
  x: margin.left,
@@ -107,14 +91,13 @@ export function computeLayout(input) {
107
91
  width: Math.max(0, width - margin.left - margin.right),
108
92
  height: Math.max(0, height - margin.top - margin.bottom)
109
93
  };
110
- return {
111
- plot,
112
- margin,
113
- xAxis
114
- };
94
+ return { plot, margin, xAxis };
115
95
  }
116
-
117
- /** 数值轴刻度数量的经验值:约每 44px 一个刻度,限制在 3~8 之间 */
118
- export function suggestTickCount(plotHeight) {
96
+ function suggestTickCount(plotHeight) {
119
97
  return Math.min(8, Math.max(3, Math.round(plotHeight / 44)));
120
- }
98
+ }
99
+ export {
100
+ computeLayout,
101
+ computeXAxisLayout,
102
+ suggestTickCount
103
+ };
@@ -1,39 +1,19 @@
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) {
1
+ 'use client';
2
+ // src/core/sampling.ts
3
+ function lttbIndices(points, threshold) {
21
4
  const length = points.length;
22
- if (length <= 2) return points.map((_, index) => index);
5
+ if (length <= 2)
6
+ return points.map((_, index) => index);
23
7
  if (threshold >= length) {
24
- // 不需要降采样,直接全取
25
8
  return points.map((_, index) => index);
26
9
  }
27
10
  if (threshold <= 2) {
28
- // 只保留首尾,至少能看出整体范围
29
11
  return [0, length - 1];
30
12
  }
31
13
  const indices = [0];
32
- // 每桶的跨度;最后一个点单独处理,所以是 (n-2)/(threshold-2)
33
14
  const bucketSize = (length - 2) / (threshold - 2);
34
15
  let previous = 0;
35
16
  for (let bucket = 0; bucket < threshold - 2; bucket += 1) {
36
- // 下一桶的平均点,用来算三角形面积
37
17
  const nextStart = Math.floor((bucket + 1) * bucketSize) + 1;
38
18
  const nextEnd = Math.min(Math.floor((bucket + 2) * bucketSize) + 1, length);
39
19
  let averageX = 0;
@@ -41,13 +21,13 @@ export function lttbIndices(points, threshold) {
41
21
  let averageCount = 0;
42
22
  for (let index = nextStart; index < nextEnd; index += 1) {
43
23
  const point = points[index];
44
- if (point.y === null || !Number.isFinite(point.y)) continue;
24
+ if (point.y === null || !Number.isFinite(point.y))
25
+ continue;
45
26
  averageX += point.x;
46
27
  averageY += point.y;
47
28
  averageCount += 1;
48
29
  }
49
30
  if (averageCount === 0) {
50
- // 下一桶全是缺失值,就退化为取桶中点
51
31
  const fallback = Math.min(Math.floor(nextStart), length - 1);
52
32
  averageX = points[fallback].x;
53
33
  averageY = 0;
@@ -56,12 +36,13 @@ export function lttbIndices(points, threshold) {
56
36
  averageX /= averageCount;
57
37
  averageY /= averageCount;
58
38
  const rangeStart = Math.floor(bucket * bucketSize) + 1;
59
- const rangeEnd = Math.min(Math.floor((bucket + 1) * bucketSize) + 1, length - 1);
39
+ const rangeEnd = Math.min(
40
+ Math.floor((bucket + 1) * bucketSize) + 1,
41
+ length - 1
42
+ );
60
43
  const anchor = points[previous];
61
44
  let maxArea = -1;
62
45
  let chosen = -1;
63
-
64
- // 锚点本身缺失时无法算面积,退化为取桶内第一个有效点
65
46
  if (anchor.y === null || !Number.isFinite(anchor.y)) {
66
47
  for (let index = rangeStart; index < rangeEnd; index += 1) {
67
48
  if (points[index].y !== null) {
@@ -69,20 +50,24 @@ export function lttbIndices(points, threshold) {
69
50
  break;
70
51
  }
71
52
  }
72
- if (chosen < 0) chosen = rangeStart;
53
+ if (chosen < 0)
54
+ chosen = rangeStart;
73
55
  } else {
74
56
  const anchorY = anchor.y;
75
57
  for (let index = rangeStart; index < rangeEnd; index += 1) {
76
58
  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));
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
+ );
80
64
  if (area > maxArea) {
81
65
  maxArea = area;
82
66
  chosen = index;
83
67
  }
84
68
  }
85
- if (chosen < 0) chosen = rangeStart;
69
+ if (chosen < 0)
70
+ chosen = rangeStart;
86
71
  }
87
72
  indices.push(chosen);
88
73
  previous = chosen;
@@ -90,19 +75,11 @@ export function lttbIndices(points, threshold) {
90
75
  indices.push(length - 1);
91
76
  return indices;
92
77
  }
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
- // 用各系列的绝对值之和作为「形状强度」,避免只看第一条系列导致别的系列丢峰
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 [];
106
83
  const combined = [];
107
84
  for (let index = 0; index < pointCount; index += 1) {
108
85
  let sum = 0;
@@ -114,10 +91,11 @@ export function sampleSeriesIndices(values, threshold) {
114
91
  hasValue = true;
115
92
  }
116
93
  }
117
- combined.push({
118
- x: index,
119
- y: hasValue ? sum : null
120
- });
94
+ combined.push({ x: index, y: hasValue ? sum : null });
121
95
  }
122
96
  return lttbIndices(combined, threshold);
123
- }
97
+ }
98
+ export {
99
+ lttbIndices,
100
+ sampleSeriesIndices
101
+ };