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.

feat(hog-charts): add bar pill-rounding/track primitives and cursor tooltip (#60821)

Sam Penningtonf4a99e5d9718
diff --git a/frontend/src/lib/hog-charts/core/bar-layout.test.ts b/frontend/src/lib/hog-charts/core/bar-layout.test.ts
index fda671861b0e..c8796471ea07 100644
--- a/frontend/src/lib/hog-charts/core/bar-layout.test.ts
+++ b/frontend/src/lib/hog-charts/core/bar-layout.test.ts
@@ -37,6 +37,24 @@ describe('hog-charts bar-layout', () => {
])('returns no rounding when shouldRoundCap is false', ({ isHorizontal, isPositive }) => {
expect(cornersFor(isHorizontal, isPositive, false)).toEqual({})
})
+
+ it.each([
+ { isHorizontal: false, isPositive: true, expected: { bottomLeft: true, bottomRight: true } },
+ { isHorizontal: false, isPositive: false, expected: { topLeft: true, topRight: true } },
+ { isHorizontal: true, isPositive: true, expected: { topLeft: true, bottomLeft: true } },
+ { isHorizontal: true, isPositive: false, expected: { topRight: true, bottomRight: true } },
+ ])('rounds the baseline end (h=$isHorizontal, +=$isPositive)', ({ isHorizontal, isPositive, expected }) => {
+ expect(cornersFor(isHorizontal, isPositive, false, true)).toEqual(expected)
+ })
+
+ it('rounds both ends as a pill when cap and baseline are both set', () => {
+ expect(cornersFor(true, true, true, true)).toEqual({
+ topLeft: true,
+ bottomLeft: true,
+ topRight: true,
+ bottomRight: true,
+ })
+ })
})
describe('computeSeriesBars — grouped', () => {
@@ -119,6 +137,54 @@ describe('hog-charts bar-layout', () => {
expect(bars[0]?.corners).toEqual(expectedCorners)
})
+ it('rounds both ends per band when capRoundedAtIndex/baseRoundedAtIndex are funnel-style', () => {
+ // Funnel: step 0 is 100% (no filler), step 1 splits value + filler. The value segment
+ // is the bottom of every band; it is also the visible top at step 0 (filler is zero).
+ const labels = ['0', '1']
+ const value = makeSeries({ key: 'value', data: [100, 55] })
+ const filler = makeSeries({ key: 'filler', data: [0, 45] })
+ const scales = createBarScales([value, filler], labels, dimensions, {
+ barLayout: 'stacked',
+ axisOrientation: 'horizontal',
+ })
+ const stacks = computeStackData([value, filler], labels)
+ // Per-band non-zero edges: value is bottom at both; top at step 0, filler is top at step 1.
+ const topKeyAtIndex = ['value', 'filler']
+ const bottomKeyAtIndex = ['value', 'value']
+ const valueBars = computeSeriesBars({
+ series: value,
+ labels,
+ scales,
+ layout: 'stacked',
+ isHorizontal: true,
+ stackedBand: stacks.get('value'),
+ isTopOfStack: false,
+ capRoundedAtIndex: (i) => topKeyAtIndex[i] === 'value',
+ baseRoundedAtIndex: (i) => bottomKeyAtIndex[i] === 'value',
+ })
+ const fillerBars = computeSeriesBars({
+ series: filler,
+ labels,
+ scales,
+ layout: 'stacked',
+ isHorizontal: true,
+ stackedBand: stacks.get('filler'),
+ isTopOfStack: true,
+ capRoundedAtIndex: (i) => topKeyAtIndex[i] === 'filler',
+ baseRoundedAtIndex: (i) => bottomKeyAtIndex[i] === 'filler',
+ })
+ // Step 0: value is the only segment — rounded pill on both ends.
+ expect(valueBars[0]?.corners).toEqual({
+ topLeft: true,
+ bottomLeft: true,
+ topRight: true,
+ bottomRight: true,
+ })
+ // Step 1: value rounds the baseline (left) only; filler rounds the cap (right) only.
+ expect(valueBars[1]?.corners).toEqual({ topLeft: true, bottomLeft: true })
+ expect(fillerBars[1]?.corners).toEqual({ topRight: true, bottomRight: true })
+ })
+
it('returns nulls when stackedBand is omitted for non-grouped layouts', () => {
const s = makeSeries({ key: 's', data: [10] })
const scales = createBarScales([s], ['a'], dimensions, { barLayout: 'stacked' })
diff --git a/frontend/src/lib/hog-charts/core/bar-layout.ts b/frontend/src/lib/hog-charts/core/bar-layout.ts
index f9018a991b4b..1337910d3e87 100644
--- a/frontend/src/lib/hog-charts/core/bar-layout.ts
+++ b/frontend/src/lib/hog-charts/core/bar-layout.ts
@@ -11,15 +11,42 @@ export interface BarChartPrivate {
export type SeriesBarLayout = (BarRect | null)[]
/** Cap is the side away from the value-axis baseline; pass `shouldRoundCap: false` for stacked
- * layers below the topmost. */
-export function cornersFor(isHorizontal: boolean, isPositive: boolean, shouldRoundCap: boolean): BarRoundedCorners {
- if (!shouldRoundCap) {
- return {}
+ * layers below the topmost. `shouldRoundBaseline` rounds the side *towards* the baseline — used
+ * for the bottom-of-stack layer so a funnel-style bar reads as one rounded pill on both ends. */
+export function cornersFor(
+ isHorizontal: boolean,
+ isPositive: boolean,
+ shouldRoundCap: boolean,
+ shouldRoundBaseline: boolean = false
+): BarRoundedCorners {
+ const corners: BarRoundedCorners = {}
+ if (shouldRoundCap) {
+ if (isHorizontal) {
+ if (isPositive) {
+ corners.topRight = corners.bottomRight = true
+ } else {
+ corners.topLeft = corners.bottomLeft = true
+ }
+ } else if (isPositive) {
+ corners.topLeft = corners.topRight = true
+ } else {
+ corners.bottomLeft = corners.bottomRight = true
+ }
}
- if (isHorizontal) {
- return isPositive ? { topRight: true, bottomRight: true } : { topLeft: true, bottomLeft: true }
+ if (shouldRoundBaseline) {
+ if (isHorizontal) {
+ if (isPositive) {
+ corners.topLeft = corners.bottomLeft = true
+ } else {
+ corners.topRight = corners.bottomRight = true
+ }
+ } else if (isPositive) {
+ corners.bottomLeft = corners.bottomRight = true
+ } else {
+ corners.topLeft = corners.topRight = true
+ }
}
- return isPositive ? { topLeft: true, topRight: true } : { bottomLeft: true, bottomRight: true }
+ return corners
}
function makeBarRect(
@@ -47,6 +74,13 @@ export interface ComputeSeriesBarsOptions {
/** Required for `stacked` and `percent` layouts. Must be omitted for `grouped`. */
stackedBand?: StackedBand
isTopOfStack: boolean
+ /** Per-index override for cap rounding — funnels round whichever segment is the topmost
+ * *non-zero* one at each band, which varies by band (e.g. a 100% first step has no
+ * filler). When omitted, falls back to the per-series `isTopOfStack`. */
+ capRoundedAtIndex?: (dataIndex: number) => boolean
+ /** Per-index override for baseline rounding — rounds the side towards the value-axis
+ * baseline for the bottom-of-stack segment. When omitted, the baseline is never rounded. */
+ baseRoundedAtIndex?: (dataIndex: number) => boolean
}
export function computeSeriesBars({
@@ -57,6 +91,8 @@ export function computeSeriesBars({
isHorizontal,
stackedBand,
isTopOfStack,
+ capRoundedAtIndex,
+ baseRoundedAtIndex,
}: ComputeSeriesBarsOptions): SeriesBarLayout {
const result: SeriesBarLayout = new Array(labels.length)
for (let i = 0; i < labels.length; i++) {
@@ -65,10 +101,12 @@ export function computeSeriesBars({
label: labels[i],
dataIndex: i,
scales,
- layout,
isHorizontal,
+ layout,
stackedBand,
isTopOfStack,
+ capRounded: capRoundedAtIndex?.(i),
+ baseRounded: baseRoundedAtIndex?.(i),
})
}
return result
@@ -84,6 +122,10 @@ export interface ComputeBarAtIndexOptions {
/** Required for `stacked` and `percent` layouts. Must be omitted for `grouped`. */
stackedBand?: StackedBand
isTopOfStack: boolean
+ /** Resolved cap-rounding for this bar. Overrides the `isGrouped || isTopOfStack` default. */
+ capRounded?: boolean
+ /** Resolved baseline-rounding for this bar. Defaults to `false`. */
+ baseRounded?: boolean
}
/** Single-bar fast path for `drawHover` so the overlay redraw doesn't recompute every bar
@@ -97,6 +139,8 @@ export function computeBarAtIndex({
isHorizontal,
stackedBand,
isTopOfStack,
+ capRounded,
+ baseRounded,
}: ComputeBarAtIndexOptions): BarRect | null {
const isGrouped = layout === 'grouped'
if (!isGrouped && !stackedBand) {
@@ -112,7 +156,8 @@ export function computeBarAtIndex({
return null
}
- const shouldRoundCap = isGrouped || isTopOfStack
+ const shouldRoundCap = capRounded ?? (isGrouped || isTopOfStack)
+ const shouldRoundBaseline = isGrouped ? false : (baseRounded ?? false)
const bandWidth = scales.band.bandwidth()
if (isGrouped) {
@@ -135,7 +180,7 @@ export function computeBarAtIndex({
// For stacked/percent the bar's "positive direction" depends on which pixel is further from baseline,
// which differs by orientation: horizontal = larger x-pixel, vertical = smaller y-pixel (axis is inverted).
const isPositive = isHorizontal ? topPixel >= bottomPixel : topPixel <= bottomPixel
- const corners = cornersFor(isHorizontal, isPositive, shouldRoundCap)
+ const corners = cornersFor(isHorizontal, isPositive, shouldRoundCap, shouldRoundBaseline)
return makeBarRect(isHorizontal, bandStart, bandWidth, topPixel, bottomPixel, corners, dataIndex)
}
diff --git a/frontend/src/lib/hog-charts/core/canvas-renderer.ts b/frontend/src/lib/hog-charts/core/canvas-renderer.ts
index e9f0f5e23615..1bfd74196f15 100644
--- a/frontend/src/lib/hog-charts/core/canvas-renderer.ts
+++ b/frontend/src/lib/hog-charts/core/canvas-renderer.ts
@@ -534,6 +534,40 @@ function fillTrackRects(ctx: CanvasRenderingContext2D, tracks: BarRect[], corner
}
}
+/** Clips subsequent drawing to the union of the given rounded rects — used to mask the bar
+ * layer to the funnel pill so a stack's outer corners round even when the edge segment is too
+ * thin to round on its own. Caller owns save/restore. */
+export function clipToRoundedRects(ctx: CanvasRenderingContext2D, rects: BarRect[], cornerRadius: number): void {
+ const renderableRects = rects.filter((r) => r.width > 0 && r.height > 0)
+ if (renderableRects.length === 0) {
+ return
+ }
+ ctx.beginPath()
+ for (const r of renderableRects) {
+ traceRoundedBarPath(ctx, r.x, r.y, r.width, r.height, cornerRadius, r.corners)
+ }
+ ctx.clip()
+}
+
+/** Paints each track rect as a single solid colour — the neutral "remainder of the whole"
+ * backdrop for funnel-style stacked bars (one track per band behind the stack), as opposed
+ * to {@link drawBarTracks}'s per-series tinted+hatched treatment for grouped layouts. */
+export function drawSolidBarTracks(
+ ctx: CanvasRenderingContext2D,
+ tracks: BarRect[],
+ color: string,
+ cornerRadius: number
+): void {
+ const renderableTracks = tracks.filter((t) => t.width > 0 && t.height > 0)
+ if (renderableTracks.length === 0) {
+ return
+ }
+ ctx.save()
+ ctx.fillStyle = color
+ fillTrackRects(ctx, renderableTracks, cornerRadius)
+ ctx.restore()
+}
+
/** Paints each track rect as a tinted base under hatched stripes. Takes laid-out rects
* from `computeBarTrackRect`, mirroring `drawBars`. */
export function drawBarTracks(
diff --git a/frontend/src/lib/hog-charts/core/types.ts b/frontend/src/lib/hog-charts/core/types.ts
index d7ec7c7376ca..e1d44557e0ec 100644
--- a/frontend/src/lib/hog-charts/core/types.ts
+++ b/frontend/src/lib/hog-charts/core/types.ts
@@ -199,10 +199,11 @@ export interface TooltipConfig {
enabled?: boolean
/** When true, clicking a data point with multiple series pins the tooltip in place. */
pinnable?: boolean
- /** Where the tooltip anchors vertically. `follow-data` (default) tracks the highest data point
- * at the hovered x; `top` fixes the tooltip to the top of the chart so it doesn't jump
- * vertically as the cursor moves between data points. */
- placement?: 'follow-data' | 'top'
+ /** Where the tooltip anchors. `follow-data` (default) tracks the highest data point at the
+ * hovered x; `top` fixes the tooltip to the top of the chart so it doesn't jump vertically
+ * as the cursor moves between data points; `cursor` tracks the mouse, so the tooltip sits
+ * beside the cursor and the hovered bar (chart.js-style) rather than at a fixed anchor. */
+ placement?: 'follow-data' | 'top' | 'cursor'
}
/** Bar appearance + band-layout details. Grouped under {@link BarChartConfig.bars} to keep the
diff --git a/frontend/src/lib/hog-charts/overlays/Tooltip.tsx b/frontend/src/lib/hog-charts/overlays/Tooltip.tsx
index 572c768dea3e..594bc5bfd9d8 100644
--- a/frontend/src/lib/hog-charts/overlays/Tooltip.tsx
+++ b/frontend/src/lib/hog-charts/overlays/Tooltip.tsx
@@ -7,7 +7,7 @@ import type { TooltipContext } from '../core/types'
interface TooltipProps<Meta> {
context: TooltipContext<Meta>
renderTooltip: (ctx: TooltipContext<Meta>) => React.ReactNode
- placement?: 'follow-data' | 'top'
+ placement?: 'follow-data' | 'top' | 'cursor'
}
const TOOLTIP_MIDDLEWARE = [offset(12), flip(), shift({ padding: 8 })]
@@ -20,34 +20,51 @@ export function Tooltip<Meta = unknown>({
}: TooltipProps<Meta>): React.ReactElement {
const { theme } = useChartLayout()
const zIndex = theme.tooltipZIndex ?? DEFAULT_TOOLTIP_Z_INDEX
- // `top` placement pins y to the canvas top, ignoring position.y (no per-mousemove reposition).
- const y = placement === 'top' ? context.canvasBounds.top : context.canvasBounds.top + context.position.y
- // Anchor at band's right edge via `right-start` so the tooltip lands beside the bar,
- // not over it. Only used for `top` placement; `flip()` handles overflow.
- const referenceWidth = placement === 'top' ? (context.position.width ?? 0) : 0
+ const { left: canvasLeft, top: canvasTop } = context.canvasBounds
+ // `cursor` follows the mouse (falling back to the data anchor on non-mousemove rebuilds);
+ // `top` pins y to the canvas top; `follow-data` anchors at the hovered data point.
+ const cursor = placement === 'cursor' ? context.hoverPosition : null
+ let anchorX: number
+ let anchorY: number
+ let anchorWidth: number
+ if (cursor) {
+ anchorX = canvasLeft + cursor.x
+ anchorY = canvasTop + cursor.y
+ anchorWidth = 0
+ } else if (placement === 'top') {
+ anchorX = canvasLeft + context.position.x
+ anchorY = canvasTop
+ // Anchor at the band's right edge via the reference width + `right-start` so the tooltip
+ // lands beside the bar, not over it. `flip()` handles overflow.
+ anchorWidth = context.position.width ?? 0
+ } else {
+ anchorX = canvasLeft + context.position.x
+ anchorY = canvasTop + context.position.y
+ anchorWidth = 0
+ }
+
const virtualReference = useMemo<VirtualElement>(
() => ({
getBoundingClientRect() {
- const centerX = context.canvasBounds.left + context.position.x
- const left = centerX - referenceWidth / 2
- const right = centerX + referenceWidth / 2
+ const left = anchorX - anchorWidth / 2
+ const right = anchorX + anchorWidth / 2
return {
x: left,
- y,
- width: referenceWidth,
+ y: anchorY,
+ width: anchorWidth,
height: 0,
- top: y,
+ top: anchorY,
right,
- bottom: y,
+ bottom: anchorY,
left,
}
},
}),
- [context.position.x, y, referenceWidth, context.canvasBounds]
+ [anchorX, anchorY, anchorWidth]
)
const { refs, floatingStyles } = useFloating({
- placement: placement === 'top' ? 'right-start' : 'right',
+ placement: placement === 'follow-data' ? 'right' : 'right-start',
strategy: 'fixed',
middleware: TOOLTIP_MIDDLEWARE,
})