11import * as d3 from 'd3' ;
22import { useCallback } from 'react' ;
3+ import { computeTooltipPosition } from '@/lib/d3-chart/layers/scatter-points' ;
34import { useStickyTooltip } from './useStickyTooltip' ;
45
56export type RulerType = 'vertical' | 'horizontal' | 'crosshair' | 'none' ;
@@ -271,9 +272,10 @@ export function useChartTooltipHandlers<TData>(): ChartTooltipHandlers<TData> {
271272 if ( isPinned ( ) ) return ;
272273
273274 const rect = containerElement . getBoundingClientRect ( ) ;
274- tooltipElement
275- . style ( 'left' , `${ event . clientX - rect . left + 10 } px` )
276- . style ( 'top' , `${ event . clientY - rect . top + 10 } px` ) ;
275+ const mx = event . clientX - rect . left ;
276+ const my = event . clientY - rect . top ;
277+ const pos = computeTooltipPosition ( mx , my , tooltipElement , containerElement ) ;
278+ tooltipElement . style ( 'left' , `${ pos . left } px` ) . style ( 'top' , `${ pos . top } px` ) ;
277279 } )
278280 . on ( 'mouseleave' , function ( _event , d ) {
279281 if ( isPinned ( ) ) return ;
@@ -292,15 +294,18 @@ export function useChartTooltipHandlers<TData>(): ChartTooltipHandlers<TData> {
292294 . on ( 'click' , function ( event , d ) {
293295 event . stopPropagation ( ) ;
294296
295- // Position tooltip near the clicked point
297+ // Set content first so dimensions are available for position calc
296298 const rect = containerElement . getBoundingClientRect ( ) ;
299+ const mx = event . clientX - rect . left ;
300+ const my = event . clientY - rect . top ;
301+ tooltipElement . html ( config . generateTooltipContent ( d , true ) ) ;
302+ const pos = computeTooltipPosition ( mx , my , tooltipElement , containerElement ) ;
297303 tooltipElement
298- . style ( 'left' , `${ event . clientX - rect . left + 10 } px` )
299- . style ( 'top' , `${ event . clientY - rect . top + 10 } px` )
304+ . style ( 'left' , `${ pos . left } px` )
305+ . style ( 'top' , `${ pos . top } px` )
300306 . style ( 'opacity' , 1 )
301307 . style ( 'display' , 'block' )
302- . style ( 'pointer-events' , 'auto' )
303- . html ( config . generateTooltipContent ( d , true ) ) ;
308+ . style ( 'pointer-events' , 'auto' ) ;
304309
305310 // Position rulers at the clicked point
306311 const { curX : currentXScale , curY : currentYScale } = getZoomedScales ( ) ;
0 commit comments