Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heatmap-hover-cell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

fix: Match heatmap hover to the cell under the cursor
28 changes: 25 additions & 3 deletions packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — Tooltip dedup keyed only on closestIndex keeps stale count/position after a data refresh or resize

When the cursor stays still and the data refreshes (live mode) or the chart resizes, uPlot runs setCursor again and the plugin reports a fresh point with the same closestIndex. Because the updater returns prev whenever the indices match, the tooltip keeps the old countVal/xVal/xCoord/xSize. After a live refresh the same index can even point to a different time bucket. Before this PR, a resize rebuilt the plugin and reset lastDataPointIndex; now the state survives that. Fix: keep prev only when the fields that are rendered also match (xVal, yVal, countVal, xCoord, yCoord, xSize, ySize), and add a HeatmapPlot test for it.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

isSameRenderedPoint(prev, point) ? prev : point,
);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
},
}),
{
Expand Down Expand Up @@ -269,7 +291,7 @@ export function HeatmapPlot({
);
onClearFilter?.();
}}
onMouseEnter={() => {
onMouseMoveCapture={() => {
mouseInsideRef.current = true;
}}
onMouseLeave={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ export function HeatmapTooltip({
<div
style={{
position: 'absolute',
top: point.yCoord,
// TODO: This seems to be off by a few pixels depending on scale
left: point.xCoord,
top: point.yCoord - point.ySize / 2,
left: point.xCoord - point.xSize / 2,
width: point.xSize,
height: point.ySize,
pointerEvents: 'none',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { fireEvent, screen } from '@testing-library/react';
import { act, fireEvent, screen } from '@testing-library/react';

import {
gridToPlotData,
HeatmapGrid,
} from '@/components/DBHeatmapChart/heatmapGrid';
import { HeatmapPlot } from '@/components/DBHeatmapChart/HeatmapPlot';
import type { HighlightedPoint } from '@/components/DBHeatmapChart/highlightDataPlugin';

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

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,
Expand Down Expand Up @@ -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',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLElement>(
'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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
],
Expand All @@ -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);
});
});
Loading
Loading