From 4e7c8e0ab73995cf75f0eb0e942362610c7208ef Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Fri, 2 Oct 2026 07:36:51 -0400 Subject: [PATCH] fix(app): Match heatmap hover to the cell under the cursor --- .changeset/heatmap-hover-cell.md | 5 + .../components/DBHeatmapChart/HeatmapPlot.tsx | 28 ++++- .../DBHeatmapChart/HeatmapTooltip.tsx | 5 +- .../__tests__/HeatmapPlot.test.tsx | 53 ++++++++- .../__tests__/HeatmapTooltip.test.tsx | 16 +++ .../__tests__/highlightDataPlugin.test.ts | 68 +++++++---- .../DBHeatmapChart/highlightDataPlugin.ts | 107 +++++++++++------- 7 files changed, 216 insertions(+), 66 deletions(-) create mode 100644 .changeset/heatmap-hover-cell.md diff --git a/.changeset/heatmap-hover-cell.md b/.changeset/heatmap-hover-cell.md new file mode 100644 index 0000000000..51ee407866 --- /dev/null +++ b/.changeset/heatmap-hover-cell.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +fix: Match heatmap hover to the cell under the cursor diff --git a/packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx b/packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx index a1fadfb309..cb15aea043 100644 --- a/packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx +++ b/packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx @@ -19,6 +19,22 @@ import { HeatmapTooltip } from './HeatmapTooltip'; import { highlightDataPlugin, HighlightedPoint } from './highlightDataPlugin'; import { applySelectionToChart, SelectionBounds } from './selection'; +const isSameRenderedPoint = ( + a: HighlightedPoint | undefined, + b: HighlightedPoint | undefined, +) => + a === b || + (a != null && + b != null && + a.xVal === b.xVal && + a.yVal === b.yVal && + a.countVal === b.countVal && + a.closestIndex === b.closestIndex && + a.xCoord === b.xCoord && + a.yCoord === b.yCoord && + a.xSize === b.xSize && + a.ySize === b.ySize); + type HeatmapPlotProps = { className?: string; grid: HeatmapGrid; @@ -195,12 +211,18 @@ export function HeatmapPlot({ }, plugins: [ highlightDataPlugin({ - proximity: 20, + margin: 20, onPointHighlight: point => { // Only show tooltip after the user has actually hovered the chart. // uPlot fires setCursor on init which would trigger this on page load. if (!mouseInsideRef.current) return; - setHighlightedPoint(point); + // Keep the same object while the cursor stays on one cell, so + // moving within it doesn't re-render the tooltip. Compare what + // the tooltip renders, not just the index: after a data refresh + // or resize the same index can hold a different cell. + setHighlightedPoint(prev => + isSameRenderedPoint(prev, point) ? prev : point, + ); }, }), { @@ -269,7 +291,7 @@ export function HeatmapPlot({ ); onClearFilter?.(); }} - onMouseEnter={() => { + onMouseMoveCapture={() => { mouseInsideRef.current = true; }} onMouseLeave={() => { diff --git a/packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx b/packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx index 22db48df5d..0d7296d806 100644 --- a/packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx +++ b/packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx @@ -27,9 +27,8 @@ export function HeatmapTooltip({
({ }, })); +let onPointHighlight: (point: HighlightedPoint | undefined) => void; +jest.mock('@/components/DBHeatmapChart/highlightDataPlugin', () => ({ + highlightDataPlugin: (opts: { + onPointHighlight: typeof onPointHighlight; + }) => { + onPointHighlight = opts.onPointHighlight; + return {}; + }, +})); + const grid: HeatmapGrid = { times: [1000, 2000], stepMs: 1000, @@ -89,4 +100,44 @@ describe('HeatmapPlot', () => { fireEvent.click(screen.getByTestId('heatmap-plot')); expect(onClearFilter).toHaveBeenCalledTimes(1); }); + + const point: HighlightedPoint = { + xVal: 1000, + yVal: 0.5, + countVal: 1, + closestDistance: 0, + closestIndex: 0, + xCoord: 10, + yCoord: 20, + xSize: 10, + ySize: 10, + }; + + it('shows the tooltip only once the mouse moves over the plot', () => { + renderPlot(); + + // uPlot reports a point on init, before any hover + act(() => onPointHighlight(point)); + expect(screen.queryByText('Count Value:')).not.toBeInTheDocument(); + + // No mouseenter: the plot can mount under a cursor that never re-enters + fireEvent.mouseMove(screen.getByTestId('heatmap-plot')); + act(() => onPointHighlight(point)); + expect(screen.getByText('Count Value:')).toBeInTheDocument(); + }); + + it('updates the tooltip when the same cell index reports new values', () => { + renderPlot(); + fireEvent.mouseMove(screen.getByTestId('heatmap-plot')); + + act(() => onPointHighlight(point)); + expect(screen.getByText('Count Value:').parentElement).toHaveTextContent( + 'Count Value: 1', + ); + + act(() => onPointHighlight({ ...point, countVal: 7, xCoord: 30 })); + expect(screen.getByText('Count Value:').parentElement).toHaveTextContent( + 'Count Value: 7', + ); + }); }); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx b/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx index f86a300716..38da5974d2 100644 --- a/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx +++ b/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx @@ -48,6 +48,22 @@ describe('HeatmapTooltip', () => { expect(screen.getByText(/y=2 \(p95\.3\)/)).toBeInTheDocument(); }); + it('centers the cell highlight on the hovered cell', () => { + const { container } = renderTooltip({ + point: { ...point, xCoord: 100, yCoord: 50, xSize: 20, ySize: 40 }, + }); + + const highlight = container.querySelector( + 'div[style*="pointer-events: none"]', + ); + expect(highlight?.style).toMatchObject({ + top: '30px', + left: '90px', + width: '20px', + height: '40px', + }); + }); + it('shows the drag hint only when filtering is enabled', () => { const { unmount } = renderTooltip(); expect(screen.queryByText(/Drag to Compare/)).not.toBeInTheDocument(); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts b/packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts index 5ef88782a9..7edd85a16b 100644 --- a/packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts +++ b/packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts @@ -6,21 +6,24 @@ import { highlightDataPlugin } from '@/components/DBHeatmapChart/highlightDataPl // Identity x positions; y pixels grow downward (pixel = 100 - 10 * value). const valToPos = (v: number, axis: 'x' | 'y') => axis === 'x' ? v : 100 - 10 * v; +const posToVal = (p: number, axis: 'x' | 'y') => + axis === 'x' ? p : (100 - p) / 10; -function hover(cursor: { left: number; top: number }) { +function hover(cursor: { left: number; top: number }, cells = [2, 6]) { const onPointHighlight = jest.fn(); - const plugin = highlightDataPlugin({ proximity: 20, onPointHighlight }); + const plugin = highlightDataPlugin({ margin: 20, onPointHighlight }); const u = { cursor, over: { offsetLeft: 5, offsetTop: 7 }, valToPos, + posToVal, data: [ [], gridToPlotData({ times: [100], stepMs: 10, yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 3] }, - cells: [2, 6], + cells, cellKind: 'count', }), ], @@ -31,26 +34,49 @@ function hover(cursor: { left: number; top: number }) { return onPointHighlight; } +const reported = (onPointHighlight: jest.Mock) => + onPointHighlight.mock.calls.at(-1)?.[0]; + describe('highlightDataPlugin', () => { - it('reports the nearest non-empty cell with its own size', () => { - // Row 1 spans y 1..3, centered at y=2 -> pixel 80 - const onPointHighlight = hover({ left: 100, top: 80 }); - - expect(onPointHighlight).toHaveBeenCalledWith( - expect.objectContaining({ - xVal: 100, - yVal: 2, - countVal: 6, - closestIndex: 1, - xCoord: 105, - yCoord: 87, - xSize: 10, - ySize: 20, - }), - ); + it('reports the cell under the cursor with its own size', () => { + expect(reported(hover({ left: 100, top: 80 }))).toEqual({ + xVal: 100, + yVal: 2, + countVal: 6, + closestDistance: 0, + closestIndex: 1, + xCoord: 105, + yCoord: 87, + xSize: 10, + ySize: 20, + }); + }); + + it('prefers the cell under the cursor over a nearby one', () => { + // Just inside row 0, a pixel from row 1's edge + expect(reported(hover({ left: 100, top: 91 }))).toMatchObject({ + closestIndex: 0, + closestDistance: 0, + }); + }); + + it('reports a cell within the margin of its edge', () => { + expect(reported(hover({ left: 110, top: 80 }))).toMatchObject({ + closestIndex: 1, + closestDistance: 5, + }); + }); + + it('reaches past an empty cell to a neighbor within the margin', () => { + // Inside the empty row 0, 5px below row 1 + expect(reported(hover({ left: 100, top: 95 }, [0, 6]))).toMatchObject({ + closestIndex: 1, + closestDistance: 5, + }); }); - it('ignores the cursor when no cell is within the proximity', () => { - expect(hover({ left: 500, top: 80 })).not.toHaveBeenCalled(); + it('reports nothing when no cell is within the margin', () => { + const onPointHighlight = hover({ left: 130, top: 80 }); + expect(onPointHighlight).toHaveBeenCalledWith(undefined); }); }); diff --git a/packages/app/src/components/DBHeatmapChart/highlightDataPlugin.ts b/packages/app/src/components/DBHeatmapChart/highlightDataPlugin.ts index a26e2572c2..6cefe15641 100644 --- a/packages/app/src/components/DBHeatmapChart/highlightDataPlugin.ts +++ b/packages/app/src/components/DBHeatmapChart/highlightDataPlugin.ts @@ -18,14 +18,31 @@ export type HighlightedPoint = { ySize: number; }; +/** Pixel distance from a point to a rectangle; 0 when the point is inside. */ +function distanceToRect( + x: number, + y: number, + left: number, + right: number, + top: number, + bottom: number, +) { + const dx = Math.max(left - x, 0, x - right); + const dy = Math.max(top - y, 0, y - bottom); + return Math.hypot(dx, dy); +} + +/** + * Reports the non-empty cell under the cursor, or the closest one within + * `margin` pixels of its edge. Reports `undefined` when there is none. + */ export function highlightDataPlugin({ - proximity, + margin, onPointHighlight, }: { - proximity: number; - onPointHighlight: (point: HighlightedPoint) => void; + margin: number; + onPointHighlight: (point: HighlightedPoint | undefined) => void; }): Plugin { - let lastDataPointIndex = -1; // Used to prevent duplicate calls return { hooks: { setCursor: (u: uPlot) => { @@ -37,52 +54,66 @@ export function highlightDataPlugin({ const [xs, ys, counts, x0s, x1s, y0s, y1s] = u .data[1] as unknown as HeatmapPlotData; - let closestIndex = 0; + // Cells outside the margin box around the cursor can't be within + // `margin`, so skip them with data-space comparisons before paying + // for pixel conversions. + const xLo = u.posToVal(left - margin, 'x'); + const xHi = u.posToVal(left + margin, 'x'); + const yA = u.posToVal(top - margin, 'y'); + const yB = u.posToVal(top + margin, 'y'); + const yLo = Math.min(yA, yB); + const yHi = Math.max(yA, yB); + + let closestIndex = -1; let closestDistance = Infinity; + let xSize = 0; + let ySize = 0; - for (let i = 0; i < xs.length; i++) { - const xPx = u.valToPos(xs[i], 'x'); - const yPx = u.valToPos(ys[i], 'y'); + for (let i = 0; i < xs.length && closestDistance > 0; i++) { + if (!(counts[i] > 0)) continue; + if (x1s[i] < xLo || x0s[i] > xHi || y1s[i] < yLo || y0s[i] > yHi) { + continue; + } - const distance = Math.sqrt((xPx - left) ** 2 + (yPx - top) ** 2); + const x0 = u.valToPos(x0s[i], 'x'); + const x1 = u.valToPos(x1s[i], 'x'); + const y0 = u.valToPos(y0s[i], 'y'); + const y1 = u.valToPos(y1s[i], 'y'); + const distance = distanceToRect( + left, + top, + Math.min(x0, x1), + Math.max(x0, x1), + Math.min(y0, y1), + Math.max(y0, y1), + ); - if (distance < closestDistance && counts[i] > 0) { + if (distance < closestDistance) { closestIndex = i; closestDistance = distance; + xSize = Math.abs(x1 - x0); + ySize = Math.abs(y1 - y0); } } - const xSize = Math.abs( - u.valToPos(x1s[closestIndex], 'x') - - u.valToPos(x0s[closestIndex], 'x'), - ); - const ySize = Math.abs( - u.valToPos(y1s[closestIndex], 'y') - - u.valToPos(y0s[closestIndex], 'y'), - ); - - const xCoord = u.valToPos(xs[closestIndex], 'x'); - const yCoord = u.valToPos(ys[closestIndex], 'y'); + if (closestIndex < 0 || closestDistance > margin) { + onPointHighlight(undefined); + return; + } const { offsetLeft, offsetTop } = u.over; - if ( - closestDistance < proximity && - closestIndex !== lastDataPointIndex - ) { - lastDataPointIndex = closestIndex; - onPointHighlight({ - xVal: xs[closestIndex], - yVal: ys[closestIndex], - countVal: counts[closestIndex], - closestDistance, - closestIndex, - xCoord: xCoord + offsetLeft, - yCoord: yCoord + offsetTop, - xSize, - ySize, - }); - } + onPointHighlight({ + xVal: xs[closestIndex], + yVal: ys[closestIndex], + countVal: counts[closestIndex], + closestDistance, + closestIndex, + xCoord: u.valToPos(xs[closestIndex], 'x') + offsetLeft, + yCoord: u.valToPos(ys[closestIndex], 'y') + offsetTop, + xSize, + ySize, + }); }, }, };