-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget-closest-data.ts
More file actions
366 lines (334 loc) · 13.2 KB
/
get-closest-data.ts
File metadata and controls
366 lines (334 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import {Delaunay, bisector, sort} from 'd3';
import get from 'lodash/get';
import groupBy from 'lodash/groupBy';
import type {PreparedBarXData, PreparedScatterData, ShapeData} from '../../hooks';
import type {PreparedAreaData} from '../../hooks/useShapes/area/types';
import type {PreparedBarYData} from '../../hooks/useShapes/bar-y/types';
import type {PreparedLineData} from '../../hooks/useShapes/line/types';
import type {PreparedPieData} from '../../hooks/useShapes/pie/types';
import type {PreparedRadarData} from '../../hooks/useShapes/radar/types';
import type {PreparedSankeyData} from '../../hooks/useShapes/sankey/types';
import type {PreparedTreemapData} from '../../hooks/useShapes/treemap/types';
import type {PreparedWaterfallData} from '../../hooks/useShapes/waterfall';
import type {
AreaSeries,
BarXSeries,
ChartSeries,
ChartSeriesData,
LineSeries,
RadarSeries,
SankeySeries,
SankeySeriesData,
TooltipDataChunk,
TreemapSeries,
WaterfallSeries,
WaterfallSeriesData,
} from '../../types';
type GetClosestPointsArgs = {
position: [number, number];
shapesData: ShapeData[];
boundsHeight: number;
boundsWidth: number;
};
export type ShapePoint = {
x: number;
y0: number;
y1: number;
data: ChartSeriesData;
series: ChartSeries;
};
function getClosestYIndex(items: ShapePoint[], y: number) {
let closestYIndex = -1;
if (y < items[0]?.y0) {
closestYIndex = 0;
} else if (y > items[items.length - 1]?.y1) {
closestYIndex = items.length - 1;
} else {
closestYIndex = items.findIndex((p) => y > p.y0 && y < p.y1);
if (closestYIndex === -1) {
const sortedY = sort(
items.map((p, index) => ({index, y: p.y1 + (p.y0 - p.y1) / 2})),
(p) => p.y,
);
const sortedYIndex = bisector<{y: number}, number>((p) => p.y).center(sortedY, y);
closestYIndex = sortedY[sortedYIndex]?.index ?? -1;
}
}
return closestYIndex;
}
function getClosestPointsByXValue(x: number, y: number, points: ShapePoint[]) {
const sorted = sort(points, (p) => p.x);
const closestXIndex = bisector<ShapePoint, number>((p) => p.x).center(sorted, x);
if (closestXIndex === -1) {
return [];
}
const closestX = sorted[closestXIndex].x;
const filtered = points.filter((p) => p.x === closestX);
const groupedBySeries = Object.values(groupBy(filtered, (p) => get(p.series, 'id'))).map(
(items) => {
const sortedByY = sort(items, (p) => p.y0);
const index = getClosestYIndex(sortedByY, y);
return sortedByY[index === -1 ? 0 : index];
},
);
const closestPoints = sort(groupedBySeries, (p) => p.y0);
const closestYIndex = getClosestYIndex(closestPoints, y);
return closestPoints.map((p, i) => ({
data: p.data,
series: p.series,
closest: i === closestYIndex,
}));
}
function getSeriesType(shapeData: ShapeData) {
return (
get(shapeData, 'series.type') ||
get(shapeData, 'point.series.type') ||
get(shapeData, 'type')
);
}
export function getClosestPoints(args: GetClosestPointsArgs): TooltipDataChunk[] {
const {position, shapesData, boundsHeight, boundsWidth} = args;
const [pointerX, pointerY] = position;
const result: TooltipDataChunk[] = [];
const groups = groupBy(shapesData, getSeriesType);
// eslint-disable-next-line complexity
Object.entries(groups).forEach(([seriesType, list]) => {
switch (seriesType) {
case 'bar-x': {
const points = (list as PreparedBarXData[]).map<ShapePoint>((d) => ({
data: d.data,
series: d.series as BarXSeries,
x: d.x + d.width / 2,
y0: d.y,
y1: d.y + d.height,
}));
result.push(
...(getClosestPointsByXValue(pointerX, pointerY, points) as TooltipDataChunk[]),
);
break;
}
case 'waterfall': {
const points = (list as PreparedWaterfallData[]).map<ShapePoint>((d) => ({
data: d.data as WaterfallSeriesData,
series: d.series as WaterfallSeries,
x: d.x + d.width / 2,
y0: d.y,
y1: d.y + d.height,
}));
result.push(
...(getClosestPointsByXValue(pointerX, pointerY, points) as TooltipDataChunk[]),
);
break;
}
case 'area': {
const points = (list as PreparedAreaData[]).reduce<ShapePoint[]>((acc, d) => {
Array.prototype.push.apply(
acc,
d.points.map((p) => ({
data: p.data,
series: p.series as AreaSeries,
x: p.x,
y0: p.y0,
y1: p.y,
})),
);
return acc;
}, []);
result.push(
...(getClosestPointsByXValue(pointerX, pointerY, points) as TooltipDataChunk[]),
);
break;
}
case 'line': {
const points = (list as PreparedLineData[]).reduce<ShapePoint[]>((acc, d) => {
acc.push(
...d.points.map((p) => ({
data: p.data,
series: p.series as LineSeries,
x: p.x,
y0: p.y,
y1: p.y,
})),
);
return acc;
}, []);
result.push(
...(getClosestPointsByXValue(pointerX, pointerY, points) as TooltipDataChunk[]),
);
break;
}
case 'bar-y': {
const points = list as PreparedBarYData[];
const sorted = sort(points, (p) => p.y);
const closestYIndex = bisector<PreparedBarYData, number>((p) => p.y).center(
sorted,
pointerY,
);
let closestPoints: PreparedBarYData[] = [];
let closestXIndex = -1;
if (closestYIndex !== -1) {
const closestY = sorted[closestYIndex].y;
closestPoints = sort(
points.filter((p) => p.y === closestY),
(p) => p.x,
);
const lastPoint = closestPoints[closestPoints.length - 1];
if (pointerX < closestPoints[0]?.x) {
closestXIndex = 0;
} else if (lastPoint && pointerX > lastPoint.x + lastPoint.width) {
closestXIndex = closestPoints.length - 1;
} else {
closestXIndex = closestPoints.findIndex(
(p) => pointerX > p.x && pointerX < p.x + p.width,
);
}
}
result.push(
...(closestPoints.map((p, i) => ({
data: p.data,
series: p.series,
closest: i === closestXIndex,
})) as TooltipDataChunk[]),
);
break;
}
case 'scatter': {
const points = list as PreparedScatterData[];
const delaunayX = Delaunay.from(
points,
(d) => d.point.x,
(d) => d.point.y,
);
const closestPoint = points[delaunayX.find(pointerX, pointerY)];
if (closestPoint) {
result.push({
data: closestPoint.point.data,
series: closestPoint.point.series,
closest: true,
});
}
break;
}
case 'pie': {
const points = (list as PreparedPieData[]).map((d) => d.segments).flat();
const closestPoint = points.find((p) => {
const {center} = p.data.pie;
const x = pointerX - center[0];
const y = pointerY - center[1];
let angle = Math.atan2(y, x) + 0.5 * Math.PI;
angle = angle < 0 ? Math.PI * 2 + angle : angle;
const polarRadius = getRadius({center, pointer: [pointerX, pointerY]});
return (
angle >= p.startAngle && angle <= p.endAngle && polarRadius < p.data.radius
);
});
if (closestPoint) {
result.push({
data: closestPoint.data.series.data,
series: closestPoint.data.series,
closest: true,
});
}
break;
}
case 'treemap': {
const data = list as unknown as PreparedTreemapData[];
const closestPoint = data[0]?.leaves.find((l) => {
return (
pointerX >= l.x0 && pointerX <= l.x1 && pointerY >= l.y0 && pointerY <= l.y1
);
});
if (closestPoint) {
result.push({
data: closestPoint.data,
series: data[0].series as unknown as TreemapSeries,
closest: true,
});
}
break;
}
case 'sankey': {
const [data] = list as unknown as PreparedSankeyData[];
const closestLink = data.links.find((d) => {
return isInsidePath({
path: d.path ?? '',
strokeWidth: d.strokeWidth,
point: [pointerX, pointerY],
width: boundsWidth,
height: boundsHeight,
});
});
if (closestLink) {
result.push({
data: closestLink.source as SankeySeriesData,
target: closestLink.target,
series: data.series as SankeySeries,
closest: true,
});
}
break;
}
case 'radar': {
const [radarData] = list as unknown as PreparedRadarData[];
const radius = getRadius({center: radarData.center, pointer: [pointerX, pointerY]});
if (radius <= radarData.radius) {
const radarShapes = radarData.shapes.filter((shape) =>
isInsidePath({
path: shape.path,
point: [pointerX, pointerY],
width: boundsWidth,
height: boundsHeight,
strokeWidth: shape.borderWidth,
}),
);
const points = radarShapes.map((shape) => shape.points).flat();
const delaunayX = Delaunay.from(
points,
(d) => d.position[0],
(d) => d.position[1],
);
const closestPoint = points[delaunayX.find(pointerX, pointerY)];
if (closestPoint) {
radarData.shapes.forEach((shape) => {
result.push({
data: shape.points[closestPoint.index].data,
series: shape.series as RadarSeries,
category: shape.series.categories[closestPoint.index],
closest: shape.series === closestPoint.series,
});
});
}
}
break;
}
}
});
return result;
}
function isInsidePath(args: {
path: string;
point: [number, number];
width: number;
height: number;
strokeWidth: number;
}) {
const {path, point, width, height, strokeWidth} = args;
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
if (ctx) {
ctx.lineWidth = strokeWidth;
const path2D = new Path2D(path);
ctx.stroke(path2D);
return ctx.isPointInPath(path2D, ...point) || ctx.isPointInStroke(path2D, ...point);
}
return null;
}
function getRadius(args: {pointer: [number, number]; center: [number, number]}) {
const {pointer, center} = args;
const x = pointer[0] - center[0];
const y = pointer[1] - center[1];
const polarRadius = Math.sqrt(x * x + y * y);
return polarRadius;
}