Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(quill): stop heatmap brush edges snapping into neighboring rows (#71569)

Jon McCallumbf03b65d7ea6
diff --git a/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx b/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx
index 155a11990654..a9e52ba1647c 100644
--- a/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx
+++ b/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx
@@ -92,6 +92,9 @@ export interface HeatmapBrushData {
// pinning the brush to one sliver of a bucket.
const FULL_HEIGHT_MIN_Y_DRAG_PX = 8
+// Half-pixel inset applied to each brush edge before snapping it to a row (see handleAreaSelect).
+const BRUSH_EDGE_EPSILON_PX = 0.5
+
export interface HeatmapCellDatum {
/** Column index into `xLabels`. */
xIndex: number
@@ -320,8 +323,12 @@ function HeatmapInner({
let endRow = layout.rows - 1
if (Math.abs(data.yPixel1 - data.yPixel0) >= FULL_HEIGHT_MIN_Y_DRAG_PX) {
// yPixel0 is the top of the drag → the higher row index (rows count bottom-up).
- const top = rowAtYClamped(layout, data.yPixel0)
- const bottom = rowAtYClamped(layout, data.yPixel1)
+ // Shrink the span half a pixel on each edge before snapping to rows: an edge landing
+ // exactly on a row boundary otherwise floors into the neighbouring row and reports a
+ // row the rect only touches. The FULL_HEIGHT_MIN_Y_DRAG_PX guard keeps the span far
+ // thicker than the nudge, so it can't invert.
+ const top = rowAtYClamped(layout, data.yPixel0 + BRUSH_EDGE_EPSILON_PX)
+ const bottom = rowAtYClamped(layout, data.yPixel1 - BRUSH_EDGE_EPSILON_PX)
startRow = Math.min(top, bottom)
endRow = Math.max(top, bottom)
}
diff --git a/packages/quill/packages/charts/src/charts/Heatmap/heatmap-layout.ts b/packages/quill/packages/charts/src/charts/Heatmap/heatmap-layout.ts
index ae276bfb74c5..c4d0c9ab6040 100644
--- a/packages/quill/packages/charts/src/charts/Heatmap/heatmap-layout.ts
+++ b/packages/quill/packages/charts/src/charts/Heatmap/heatmap-layout.ts
@@ -53,7 +53,7 @@ export function rowAtY(layout: HeatmapLayout, y: number): number {
if (layout.rows === 0 || layout.rowHeight <= 0) {
return -1
}
- const row = Math.floor((layout.plotTop + layout.plotHeight - y) / layout.rowHeight)
+ const row = rawRowAtY(layout, y)
return row >= 0 && row < layout.rows ? row : -1
}
@@ -63,8 +63,12 @@ export function rowAtYClamped(layout: HeatmapLayout, y: number): number {
if (layout.rows === 0 || layout.rowHeight <= 0) {
return -1
}
- const row = Math.floor((layout.plotTop + layout.plotHeight - y) / layout.rowHeight)
- return Math.max(0, Math.min(layout.rows - 1, row))
+ return Math.max(0, Math.min(layout.rows - 1, rawRowAtY(layout, y)))
+}
+
+// Bottom-up row index for a canvas y pixel, unbounded — callers apply their own edge policy.
+function rawRowAtY(layout: HeatmapLayout, y: number): number {
+ return Math.floor((layout.plotTop + layout.plotHeight - y) / layout.rowHeight)
}
export function maxCellValue(cells: number[][]): number {
diff --git a/packages/quill/packages/charts/src/core/canvas-renderer.ts b/packages/quill/packages/charts/src/core/canvas-renderer.ts
index 6535d5a1d7b0..7e51fbefbeb3 100644
--- a/packages/quill/packages/charts/src/core/canvas-renderer.ts
+++ b/packages/quill/packages/charts/src/core/canvas-renderer.ts
@@ -1405,11 +1405,10 @@ export function composeDrawHoverWithSelection(baseDrawHover: DrawHoverFn): DrawH
const x0 = Math.max(args.dimensions.plotLeft, Math.min(dragRect.x0, dragRect.x1))
const x1 = Math.min(args.dimensions.plotLeft + args.dimensions.plotWidth, Math.max(dragRect.x0, dragRect.x1))
const plotBottom = args.dimensions.plotTop + args.dimensions.plotHeight
- const hasY = dragRect.y0 != null && dragRect.y1 != null
- const y0 = hasY
- ? Math.max(args.dimensions.plotTop, Math.min(dragRect.y0 ?? 0, dragRect.y1 ?? 0))
- : args.dimensions.plotTop
- const y1 = hasY ? Math.min(plotBottom, Math.max(dragRect.y0 ?? 0, dragRect.y1 ?? 0)) : plotBottom
+ const { y0: rectY0, y1: rectY1 } = dragRect
+ const hasY = rectY0 != null && rectY1 != null
+ const y0 = hasY ? Math.max(args.dimensions.plotTop, Math.min(rectY0, rectY1)) : args.dimensions.plotTop
+ const y1 = hasY ? Math.min(plotBottom, Math.max(rectY0, rectY1)) : plotBottom
if (x1 <= x0 || y1 <= y0) {
return result
}
diff --git a/packages/quill/packages/charts/src/core/hooks/useDragToZoom.ts b/packages/quill/packages/charts/src/core/hooks/useDragToZoom.ts
index 3f5170f88cff..855a9556749f 100644
--- a/packages/quill/packages/charts/src/core/hooks/useDragToZoom.ts
+++ b/packages/quill/packages/charts/src/core/hooks/useDragToZoom.ts
@@ -70,24 +70,25 @@ export function useDragToZoom({
}
if (origin.active) {
const range = dragRectToLabelRange({ x0: origin.x, x1: mouseX }, labelPositionsRef.current)
- if (range && onAreaSelectRef.current) {
+ if (range) {
const currentLabels = labelsRef.current
- onAreaSelectRef.current({
+ const labelRange = {
startLabel: currentLabels[range.startIndex],
endLabel: currentLabels[range.endIndex],
startIndex: range.startIndex,
endIndex: range.endIndex,
- yPixel0: Math.min(origin.y, mouseY),
- yPixel1: Math.max(origin.y, mouseY),
- })
- } else if (range && onDateRangeZoomRef.current) {
- const currentLabels = labelsRef.current
- onDateRangeZoomRef.current({
- startLabel: currentLabels[range.startIndex],
- endLabel: currentLabels[range.endIndex],
- startIndex: range.startIndex,
- endIndex: range.endIndex,
- })
+ }
+ // Area-select wins over date-range zoom when both are set (a consumer wiring
+ // both would otherwise double-apply the x range).
+ if (onAreaSelectRef.current) {
+ onAreaSelectRef.current({
+ ...labelRange,
+ yPixel0: Math.min(origin.y, mouseY),
+ yPixel1: Math.max(origin.y, mouseY),
+ })
+ } else if (onDateRangeZoomRef.current) {
+ onDateRangeZoomRef.current(labelRange)
+ }
}
// Swallow the trailing `click` that the browser fires after the gesture's mouseup.
// shouldSwallowClick() clears this synchronously on that click; the timer is the
diff --git a/packages/quill/packages/charts/src/core/types.ts b/packages/quill/packages/charts/src/core/types.ts
index 717169219f3b..13cd992ca673 100644
--- a/packages/quill/packages/charts/src/core/types.ts
+++ b/packages/quill/packages/charts/src/core/types.ts
@@ -504,8 +504,9 @@ export interface ChartDrawArgs {
/** Restart the hover-fade at progress 0; returns the new value to use this frame.
* Call when the chart type detects a visible-state change at the same hoverIndex. */
resetHoverFade: () => number
- /** Live pixel range of an in-progress drag-to-zoom selection, x-axis only. Null when
- * no drag is active. Only the hover overlay reads this — the static layer ignores it. */
+ /** Live pixel range of an in-progress selection: x-axis drag-to-zoom, plus the vertical
+ * range on a 2D (`onAreaSelect`) brush. Null when no drag is active. Only the hover
+ * overlay reads this — the static layer ignores it. */
dragRect?: DragRect | null
}
@@ -519,22 +520,21 @@ export interface DragRect {
y1?: number
}
-export interface DateRangeZoomData {
+/** An x-axis range resolved to labels — the shared shape of the drag-selection payloads. */
+export interface LabelRange {
startLabel: string
endLabel: string
startIndex: number
endIndex: number
}
+export type DateRangeZoomData = LabelRange
+
/** Payload of a completed 2D brush ({@link ChartProps.onAreaSelect}). The x axis resolves to
* labels like `onDateRangeZoom`; the y axis stays in canvas pixels — the core is label-generic
* and has no y-band concept, so chart-type adapters map the pixel range onto their own scales
* (e.g. the Heatmap converts it to row indices). */
-export interface AreaSelectData {
- startLabel: string
- endLabel: string
- startIndex: number
- endIndex: number
+export interface AreaSelectData extends LabelRange {
/** Top of the dragged range in canvas pixels (always <= yPixel1). */
yPixel0: number
/** Bottom of the dragged range in canvas pixels. */