-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuseChartTooltipHandlers.ts
More file actions
346 lines (312 loc) · 11.9 KB
/
useChartTooltipHandlers.ts
File metadata and controls
346 lines (312 loc) · 11.9 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
import * as d3 from 'd3';
import { useCallback } from 'react';
import { computeTooltipPosition } from '@/lib/d3-chart/layers/scatter-points';
import { useStickyTooltip } from './useStickyTooltip';
export type RulerType = 'vertical' | 'horizontal' | 'crosshair' | 'none';
export interface ChartTooltipConfig<TData> {
/**
* Type of ruler to display
* - 'vertical': Single vertical line (for bar charts)
* - 'crosshair': Both vertical and horizontal lines (for scatter charts)
*/
rulerType: RulerType;
/**
* Function to generate tooltip HTML content
* @param data - The data point
* @param isPinned - Whether the tooltip is pinned
*/
generateTooltipContent: (data: TData, isPinned: boolean) => string;
/**
* Function to get X coordinate for ruler positioning
* For bar charts: typically xScale(label) + bandwidth/2
* For scatter charts: xScale(data.x)
*/
getRulerX?: (
data: TData,
xScale:
| d3.ScaleBand<string>
| d3.ScaleLinear<number, number, never>
| d3.ScaleLogarithmic<number, number, never>,
) => number;
/**
* Function to get Y coordinate for ruler positioning (crosshair only)
* For scatter charts: yScale(data.y)
*/
getRulerY?: (
data: TData,
yScale:
| d3.ScaleBand<string>
| d3.ScaleLinear<number, number, never>
| d3.ScaleLogarithmic<number, number, never>,
) => number;
/**
* Optional function to apply hover state to element
* For scatter charts with custom shapes
*/
onHoverStart?: (selection: d3.Selection<any, TData, any, any>, data: TData) => void;
/**
* Optional function to remove hover state from element
*/
onHoverEnd?: (selection: d3.Selection<any, TData, any, any>, data: TData) => void;
/**
* Optional analytics tracking function called on click
*/
onPointClick?: (data: TData) => void;
}
export interface ChartTooltipHandlers<TData> {
pinnedPoint: TData | null;
pinnedPointIsOverlay: boolean;
pinTooltip: (data: TData, isOverlay?: boolean) => void;
dismissTooltip: (clearPinnedPoint?: boolean) => void;
isPinned: () => boolean;
hideTooltipElements: (
tooltipRef: React.RefObject<HTMLDivElement | null>,
svgRef: React.RefObject<SVGSVGElement | null>,
) => void;
/**
* Creates ruler elements in the chart
* @param group - D3 selection of the group to append rulers to
* @param rulerType - Type of ruler ('vertical' or 'crosshair')
* @param width - Chart width (for horizontal ruler)
* @param height - Chart height
* @param foregroundColor - Color for the ruler lines
* @returns Object with ruler group and individual ruler selections
*/
createRulers: (
group: d3.Selection<SVGGElement, unknown, null, undefined>,
rulerType: RulerType,
width: number,
height: number,
foregroundColor: string,
) => {
rulerGroup: d3.Selection<SVGGElement, unknown, null, undefined>;
verticalRuler?: d3.Selection<SVGLineElement, unknown, null, undefined>;
horizontalRuler?: d3.Selection<SVGLineElement, unknown, null, undefined>;
};
/**
* Attaches tooltip event handlers to a D3 selection
* @param selection - D3 selection to attach handlers to
* @param config - Tooltip configuration
* @param containerElement - Container element for positioning
* @param tooltipElement - Tooltip div element
* @param rulers - Ruler elements from createRulers
* @param xScale - X scale for ruler positioning
* @param yScale - Y scale for ruler positioning (optional, for crosshairs)
* @param svgRef - SVG ref for zoom transform (optional, for scatter charts)
*/
attachHandlers: (
selection: d3.Selection<any, TData, any, any>,
config: ChartTooltipConfig<TData>,
containerElement: HTMLDivElement,
tooltipElement: d3.Selection<any, unknown, any, any>,
rulers: {
rulerGroup: d3.Selection<SVGGElement, unknown, null, undefined>;
verticalRuler: d3.Selection<SVGLineElement, unknown, null, undefined>;
horizontalRuler?: d3.Selection<SVGLineElement, unknown, null, undefined>;
},
xScale:
| d3.ScaleBand<string>
| d3.ScaleLinear<number, number, never>
| d3.ScaleLogarithmic<number, number, never>,
yScale?: d3.ScaleLinear<number, number, never> | d3.ScaleLogarithmic<number, number, never>,
svgRef?: React.RefObject<SVGSVGElement | null>,
zoomAxes?: 'x' | 'y' | 'both',
) => void;
}
/**
* Hook for managing chart tooltip interactions with rulers/crosshairs
* Consolidates common tooltip patterns across all D3 charts
*/
export function useChartTooltipHandlers<TData>(): ChartTooltipHandlers<TData> {
const {
pinnedPoint,
pinnedPointIsOverlay,
pinTooltip,
dismissTooltip,
isPinned,
hideTooltipElements,
} = useStickyTooltip<TData>();
const createRulers = useCallback(
(
group: d3.Selection<SVGGElement, unknown, null, undefined>,
rulerType: RulerType,
width: number,
height: number,
foregroundColor: string,
) => {
// Idempotent: reuse existing ruler group or create new
let rulerGroup = group.select<SVGGElement>('.ruler-group');
if (rulerGroup.empty()) {
rulerGroup = group.append('g').attr('class', 'ruler-group').style('pointer-events', 'none');
}
rulerGroup.style('display', 'none');
let verticalRuler: d3.Selection<SVGLineElement, unknown, null, undefined> | undefined;
if (rulerType !== 'horizontal' && rulerType !== 'none') {
let vRuler = rulerGroup.select<SVGLineElement>('.vertical-ruler');
if (vRuler.empty()) {
vRuler = rulerGroup
.append('line')
.attr('class', 'vertical-ruler')
.attr('stroke-width', 1)
.attr('stroke-dasharray', '4 4')
.attr('opacity', 0.8);
}
vRuler.attr('stroke', foregroundColor).attr('y1', 0).attr('y2', height);
verticalRuler = vRuler;
}
let horizontalRuler: d3.Selection<SVGLineElement, unknown, null, undefined> | undefined;
if (rulerType === 'crosshair' || rulerType === 'horizontal') {
let hRuler = rulerGroup.select<SVGLineElement>('.horizontal-ruler');
if (hRuler.empty()) {
hRuler = rulerGroup
.append('line')
.attr('class', 'horizontal-ruler')
.attr('stroke-width', 1)
.attr('stroke-dasharray', '4 4')
.attr('opacity', 0.8);
}
hRuler.attr('stroke', foregroundColor).attr('x1', 0).attr('x2', width);
horizontalRuler = hRuler;
}
return { rulerGroup, verticalRuler, horizontalRuler };
},
[],
);
const attachHandlers = useCallback(
(
selection: d3.Selection<any, TData, any, any>,
config: ChartTooltipConfig<TData>,
containerElement: HTMLDivElement,
tooltipElement: d3.Selection<any, unknown, any, any>,
rulers: {
rulerGroup: d3.Selection<SVGGElement, unknown, null, undefined>;
verticalRuler?: d3.Selection<SVGLineElement, unknown, null, undefined>;
horizontalRuler?: d3.Selection<SVGLineElement, unknown, null, undefined>;
},
xScale:
| d3.ScaleBand<string>
| d3.ScaleLinear<number, number, never>
| d3.ScaleLogarithmic<number, number, never>,
yScale?: d3.ScaleLinear<number, number, never> | d3.ScaleLogarithmic<number, number, never>,
svgRef?: React.RefObject<SVGSVGElement | null>,
zoomAxes?: 'x' | 'y' | 'both',
) => {
const { rulerGroup, verticalRuler, horizontalRuler } = rulers;
const effectiveZoomAxes = zoomAxes ?? 'both';
/** Apply zoom transform to scales, respecting which axes are zoomed. */
const getZoomedScales = () => {
let curX = xScale;
let curY = yScale;
if (svgRef?.current) {
const transform = d3.zoomTransform(svgRef.current);
if ((effectiveZoomAxes === 'x' || effectiveZoomAxes === 'both') && 'invert' in xScale) {
curX = transform.rescaleX(xScale as any);
}
if (
(effectiveZoomAxes === 'y' || effectiveZoomAxes === 'both') &&
yScale &&
'invert' in yScale
) {
curY = transform.rescaleY(yScale as any);
}
}
return { curX, curY };
};
selection
.on('mouseenter', function (_event, d) {
if (isPinned()) return;
// Apply custom hover state if provided
if (config.onHoverStart) {
config.onHoverStart(d3.select(this), d);
} else {
// Default: reduce opacity slightly
d3.select(this).attr('opacity', 0.8);
}
// Show tooltip
tooltipElement
.style('opacity', 1)
.style('display', 'block')
.style('pointer-events', 'none')
.html(config.generateTooltipContent(d, false));
// Position rulers
const { curX: currentXScale, curY: currentYScale } = getZoomedScales();
if (verticalRuler || horizontalRuler) {
rulerGroup.style('display', 'block');
}
if (verticalRuler && config.getRulerX) {
const x = config.getRulerX(d, currentXScale);
verticalRuler.attr('x1', x).attr('x2', x);
}
if (horizontalRuler && currentYScale && config.getRulerY) {
const y = config.getRulerY(d, currentYScale);
horizontalRuler.attr('y1', y).attr('y2', y);
}
})
.on('mousemove', function (event) {
if (isPinned()) return;
const rect = containerElement.getBoundingClientRect();
const mx = event.clientX - rect.left;
const my = event.clientY - rect.top;
const pos = computeTooltipPosition(mx, my, tooltipElement, containerElement);
tooltipElement.style('left', `${pos.left}px`).style('top', `${pos.top}px`);
})
.on('mouseleave', function (_event, d) {
if (isPinned()) return;
// Remove custom hover state if provided
if (config.onHoverEnd) {
config.onHoverEnd(d3.select(this), d);
} else {
// Default: restore full opacity
d3.select(this).attr('opacity', 1);
}
tooltipElement.style('opacity', 0).style('display', 'none');
rulerGroup.style('display', 'none');
})
.on('click', function (event, d) {
event.stopPropagation();
// Set content first so dimensions are available for position calc
const rect = containerElement.getBoundingClientRect();
const mx = event.clientX - rect.left;
const my = event.clientY - rect.top;
tooltipElement.html(config.generateTooltipContent(d, true));
const pos = computeTooltipPosition(mx, my, tooltipElement, containerElement);
tooltipElement
.style('left', `${pos.left}px`)
.style('top', `${pos.top}px`)
.style('opacity', 1)
.style('display', 'block')
.style('pointer-events', 'auto');
// Position rulers at the clicked point
const { curX: currentXScale, curY: currentYScale } = getZoomedScales();
if (verticalRuler || horizontalRuler) {
rulerGroup.style('display', 'block');
}
if (verticalRuler && config.getRulerX) {
const x = config.getRulerX(d, currentXScale);
verticalRuler.attr('x1', x).attr('x2', x);
}
if (horizontalRuler && currentYScale && config.getRulerY) {
const y = config.getRulerY(d, currentYScale);
horizontalRuler.attr('y1', y).attr('y2', y);
}
// Pin the tooltip
pinTooltip(d);
// Call optional analytics tracking
if (config.onPointClick) {
config.onPointClick(d);
}
});
},
[isPinned, pinTooltip],
);
return {
pinnedPoint,
pinnedPointIsOverlay,
pinTooltip,
dismissTooltip,
isPinned,
hideTooltipElements,
createRulers,
attachHandlers,
};
}