Skip to content

Commit 4e7c8e0

Browse files
committed
fix(app): Match heatmap hover to the cell under the cursor
1 parent 0b85553 commit 4e7c8e0

7 files changed

Lines changed: 216 additions & 66 deletions

File tree

‎.changeset/heatmap-hover-cell.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hyperdx/app': patch
3+
---
4+
5+
fix: Match heatmap hover to the cell under the cursor

‎packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ import { HeatmapTooltip } from './HeatmapTooltip';
1919
import { highlightDataPlugin, HighlightedPoint } from './highlightDataPlugin';
2020
import { applySelectionToChart, SelectionBounds } from './selection';
2121

22+
const isSameRenderedPoint = (
23+
a: HighlightedPoint | undefined,
24+
b: HighlightedPoint | undefined,
25+
) =>
26+
a === b ||
27+
(a != null &&
28+
b != null &&
29+
a.xVal === b.xVal &&
30+
a.yVal === b.yVal &&
31+
a.countVal === b.countVal &&
32+
a.closestIndex === b.closestIndex &&
33+
a.xCoord === b.xCoord &&
34+
a.yCoord === b.yCoord &&
35+
a.xSize === b.xSize &&
36+
a.ySize === b.ySize);
37+
2238
type HeatmapPlotProps = {
2339
className?: string;
2440
grid: HeatmapGrid;
@@ -195,12 +211,18 @@ export function HeatmapPlot({
195211
},
196212
plugins: [
197213
highlightDataPlugin({
198-
proximity: 20,
214+
margin: 20,
199215
onPointHighlight: point => {
200216
// Only show tooltip after the user has actually hovered the chart.
201217
// uPlot fires setCursor on init which would trigger this on page load.
202218
if (!mouseInsideRef.current) return;
203-
setHighlightedPoint(point);
219+
// Keep the same object while the cursor stays on one cell, so
220+
// moving within it doesn't re-render the tooltip. Compare what
221+
// the tooltip renders, not just the index: after a data refresh
222+
// or resize the same index can hold a different cell.
223+
setHighlightedPoint(prev =>
224+
isSameRenderedPoint(prev, point) ? prev : point,
225+
);
204226
},
205227
}),
206228
{
@@ -269,7 +291,7 @@ export function HeatmapPlot({
269291
);
270292
onClearFilter?.();
271293
}}
272-
onMouseEnter={() => {
294+
onMouseMoveCapture={() => {
273295
mouseInsideRef.current = true;
274296
}}
275297
onMouseLeave={() => {

‎packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ export function HeatmapTooltip({
2727
<div
2828
style={{
2929
position: 'absolute',
30-
top: point.yCoord,
31-
// TODO: This seems to be off by a few pixels depending on scale
32-
left: point.xCoord,
30+
top: point.yCoord - point.ySize / 2,
31+
left: point.xCoord - point.xSize / 2,
3332
width: point.xSize,
3433
height: point.ySize,
3534
pointerEvents: 'none',

‎packages/app/src/components/DBHeatmapChart/__tests__/HeatmapPlot.test.tsx‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { fireEvent, screen } from '@testing-library/react';
1+
import { act, fireEvent, screen } from '@testing-library/react';
22

33
import {
44
gridToPlotData,
55
HeatmapGrid,
66
} from '@/components/DBHeatmapChart/heatmapGrid';
77
import { HeatmapPlot } from '@/components/DBHeatmapChart/HeatmapPlot';
8+
import type { HighlightedPoint } from '@/components/DBHeatmapChart/highlightDataPlugin';
89

910
type PlotOptions = {
1011
cursor?: { drag?: { x?: boolean; y?: boolean } };
@@ -26,6 +27,16 @@ jest.mock('uplot-react', () => ({
2627
},
2728
}));
2829

30+
let onPointHighlight: (point: HighlightedPoint | undefined) => void;
31+
jest.mock('@/components/DBHeatmapChart/highlightDataPlugin', () => ({
32+
highlightDataPlugin: (opts: {
33+
onPointHighlight: typeof onPointHighlight;
34+
}) => {
35+
onPointHighlight = opts.onPointHighlight;
36+
return {};
37+
},
38+
}));
39+
2940
const grid: HeatmapGrid = {
3041
times: [1000, 2000],
3142
stepMs: 1000,
@@ -89,4 +100,44 @@ describe('HeatmapPlot', () => {
89100
fireEvent.click(screen.getByTestId('heatmap-plot'));
90101
expect(onClearFilter).toHaveBeenCalledTimes(1);
91102
});
103+
104+
const point: HighlightedPoint = {
105+
xVal: 1000,
106+
yVal: 0.5,
107+
countVal: 1,
108+
closestDistance: 0,
109+
closestIndex: 0,
110+
xCoord: 10,
111+
yCoord: 20,
112+
xSize: 10,
113+
ySize: 10,
114+
};
115+
116+
it('shows the tooltip only once the mouse moves over the plot', () => {
117+
renderPlot();
118+
119+
// uPlot reports a point on init, before any hover
120+
act(() => onPointHighlight(point));
121+
expect(screen.queryByText('Count Value:')).not.toBeInTheDocument();
122+
123+
// No mouseenter: the plot can mount under a cursor that never re-enters
124+
fireEvent.mouseMove(screen.getByTestId('heatmap-plot'));
125+
act(() => onPointHighlight(point));
126+
expect(screen.getByText('Count Value:')).toBeInTheDocument();
127+
});
128+
129+
it('updates the tooltip when the same cell index reports new values', () => {
130+
renderPlot();
131+
fireEvent.mouseMove(screen.getByTestId('heatmap-plot'));
132+
133+
act(() => onPointHighlight(point));
134+
expect(screen.getByText('Count Value:').parentElement).toHaveTextContent(
135+
'Count Value: 1',
136+
);
137+
138+
act(() => onPointHighlight({ ...point, countVal: 7, xCoord: 30 }));
139+
expect(screen.getByText('Count Value:').parentElement).toHaveTextContent(
140+
'Count Value: 7',
141+
);
142+
});
92143
});

‎packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ describe('HeatmapTooltip', () => {
4848
expect(screen.getByText(/y=2 \(p95\.3\)/)).toBeInTheDocument();
4949
});
5050

51+
it('centers the cell highlight on the hovered cell', () => {
52+
const { container } = renderTooltip({
53+
point: { ...point, xCoord: 100, yCoord: 50, xSize: 20, ySize: 40 },
54+
});
55+
56+
const highlight = container.querySelector<HTMLElement>(
57+
'div[style*="pointer-events: none"]',
58+
);
59+
expect(highlight?.style).toMatchObject({
60+
top: '30px',
61+
left: '90px',
62+
width: '20px',
63+
height: '40px',
64+
});
65+
});
66+
5167
it('shows the drag hint only when filtering is enabled', () => {
5268
const { unmount } = renderTooltip();
5369
expect(screen.queryByText(/Drag to Compare/)).not.toBeInTheDocument();

‎packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts‎

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ import { highlightDataPlugin } from '@/components/DBHeatmapChart/highlightDataPl
66
// Identity x positions; y pixels grow downward (pixel = 100 - 10 * value).
77
const valToPos = (v: number, axis: 'x' | 'y') =>
88
axis === 'x' ? v : 100 - 10 * v;
9+
const posToVal = (p: number, axis: 'x' | 'y') =>
10+
axis === 'x' ? p : (100 - p) / 10;
911

10-
function hover(cursor: { left: number; top: number }) {
12+
function hover(cursor: { left: number; top: number }, cells = [2, 6]) {
1113
const onPointHighlight = jest.fn();
12-
const plugin = highlightDataPlugin({ proximity: 20, onPointHighlight });
14+
const plugin = highlightDataPlugin({ margin: 20, onPointHighlight });
1315
const u = {
1416
cursor,
1517
over: { offsetLeft: 5, offsetTop: 7 },
1618
valToPos,
19+
posToVal,
1720
data: [
1821
[],
1922
gridToPlotData({
2023
times: [100],
2124
stepMs: 10,
2225
yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 3] },
23-
cells: [2, 6],
26+
cells,
2427
cellKind: 'count',
2528
}),
2629
],
@@ -31,26 +34,49 @@ function hover(cursor: { left: number; top: number }) {
3134
return onPointHighlight;
3235
}
3336

37+
const reported = (onPointHighlight: jest.Mock) =>
38+
onPointHighlight.mock.calls.at(-1)?.[0];
39+
3440
describe('highlightDataPlugin', () => {
35-
it('reports the nearest non-empty cell with its own size', () => {
36-
// Row 1 spans y 1..3, centered at y=2 -> pixel 80
37-
const onPointHighlight = hover({ left: 100, top: 80 });
38-
39-
expect(onPointHighlight).toHaveBeenCalledWith(
40-
expect.objectContaining({
41-
xVal: 100,
42-
yVal: 2,
43-
countVal: 6,
44-
closestIndex: 1,
45-
xCoord: 105,
46-
yCoord: 87,
47-
xSize: 10,
48-
ySize: 20,
49-
}),
50-
);
41+
it('reports the cell under the cursor with its own size', () => {
42+
expect(reported(hover({ left: 100, top: 80 }))).toEqual({
43+
xVal: 100,
44+
yVal: 2,
45+
countVal: 6,
46+
closestDistance: 0,
47+
closestIndex: 1,
48+
xCoord: 105,
49+
yCoord: 87,
50+
xSize: 10,
51+
ySize: 20,
52+
});
53+
});
54+
55+
it('prefers the cell under the cursor over a nearby one', () => {
56+
// Just inside row 0, a pixel from row 1's edge
57+
expect(reported(hover({ left: 100, top: 91 }))).toMatchObject({
58+
closestIndex: 0,
59+
closestDistance: 0,
60+
});
61+
});
62+
63+
it('reports a cell within the margin of its edge', () => {
64+
expect(reported(hover({ left: 110, top: 80 }))).toMatchObject({
65+
closestIndex: 1,
66+
closestDistance: 5,
67+
});
68+
});
69+
70+
it('reaches past an empty cell to a neighbor within the margin', () => {
71+
// Inside the empty row 0, 5px below row 1
72+
expect(reported(hover({ left: 100, top: 95 }, [0, 6]))).toMatchObject({
73+
closestIndex: 1,
74+
closestDistance: 5,
75+
});
5176
});
5277

53-
it('ignores the cursor when no cell is within the proximity', () => {
54-
expect(hover({ left: 500, top: 80 })).not.toHaveBeenCalled();
78+
it('reports nothing when no cell is within the margin', () => {
79+
const onPointHighlight = hover({ left: 130, top: 80 });
80+
expect(onPointHighlight).toHaveBeenCalledWith(undefined);
5581
});
5682
});

0 commit comments

Comments
 (0)