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(quill-charts): round stacked bar caps per band, geometrically (#67998)

Sam Pennington22da35a0b1c4
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 2db83cb24ec1..9dfa8e6fb96d 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -565,15 +565,15 @@ snapshots:
components-hogcharts-barchart--stacked-mixed-sign--light:
hash: v1.k794b7964.54ea51777cd1be48ada08afc6d9652348e58c55b4578259834125c1586df772a.puvvq3HL5w9hHEpev9dP2fRTzvd42bW7BZ-F7WiOIks
components-hogcharts-barchart--stacked-with-track-ceiling--dark:
- hash: v1.k794b7964.1f85d679741bd81ee3e7ab488ce82596c1e9f29f396295d863ed51a53f5a3a55.Q0JUJARVeJV8GdQs-EpptVEuOCgB0zYkwCApvNmEc7A
+ hash: v1.k794b7964.1ea8d87a1726a9a532850eb97fe6d93137d5dbb1aa960392741c9242778248b7.b88PGR3XtCLRXIoJ6hnH5yS7esXRS5Q2Ej3S6W7PO5g
components-hogcharts-barchart--stacked-with-track-ceiling--light:
- hash: v1.k794b7964.eb413728e33e4274883d8126d1634e408b221e6c9b3d880f6feaae458b891e09.55R7ZeVRnWxZjkWcMuLk_k8ZLPvrFXLxRqC8xOzYtAo
+ hash: v1.k794b7964.0d490f49e93f7af73d125716612fe4cb232d5476b6d8f29bd967dbe38fe5071c.ZPyhSX_pPV9Rg8G1VgMII5VYCH7ewyRl6ks4C0AGElg
components-hogcharts-barchart--with-bar-track--dark:
hash: v1.k794b7964.459993a07b1a8d239b43e5700fda3de963173c7c62792c3ca73ca7d26d3e967e.3i1wOTz8MkiBSViwUvFmF0H-bkd_5Yeu3YpGa4l5jP4
components-hogcharts-barchart--with-bar-track--light:
hash: v1.k794b7964.ba635da9134368b50adcd9ef7f0918d3c586f9282554e5395db438f04c1b9828.4CvwEAEAL4UT_K-Fs7S-laUq9wf3MMmOG2jnRIkffo4
components-hogcharts-barchart--with-bar-track-ceiling--dark:
- hash: v1.k794b7964.3326668de4a2d7d75de261b264de264a7a60113b703e67f687a8ef8a42f36109.2lqjmeix3iZwdrVjXi8ZpxMk9tuHmyV0LWVEIdaPJJ4
+ hash: v1.k794b7964.a59a7344c9cf9f18f144db6101af8e1484ba9fbf25d2e534ef7374b6ddc6349f.G1zekxfY0rzlPR7qvxJYOoNov8Oo6AQd-NSJ_3jCjUI
components-hogcharts-barchart--with-bar-track-ceiling--light:
hash: v1.k794b7964.e9accba1ecd78df7cf201f4eccdb1879ff90a450c7052c10940b1406892dcd65.fu8AiHQlPz_QXbMIH-RxgXyBXyhX9x365WS4pFWbKA4
components-hogcharts-barchart--with-reference-line--dark:
@@ -829,7 +829,7 @@ snapshots:
components-hogcharts-piechart--with-breakdown-labels--light:
hash: v1.k794b7964.0d2c1e4814a1414b2408dca7bcf8caab2c8ae39e313df9756d210e5dca04fba6.xQ4aCFRK6tR7nYCZNS7TKRDTD7qYqoZaUuY__-vfLBY
components-hogcharts-referenceline--fill-sides--dark:
- hash: v1.k794b7964.1de607eafc06356aced4ea0c94af0de2f0ce7e5568096ea9de1212946b19c1a6.EpByD_GpgernGyEPR5BsGG8sdXa3BxHXcjqgVwWbNS0
+ hash: v1.k794b7964.41d688d69cc669299398feee2e4f7986720e7d4aca69b3f6fa8c8e393b8b0c52.ykaqMNcqN3CyZHWIl_dxBhROh0rhUQ2tS2raPG1SgIs
components-hogcharts-referenceline--fill-sides--light:
hash: v1.k794b7964.e270bf77bc5b762f7a3ba24687202642ae8c33397dabbc4e76dd298acb92eba0.9P3ASMFV0Zbrb21E8Tux-2Cd7rGIWvgHAL7CDwwLpGI
components-hogcharts-referenceline--horizontal-goal--dark:
@@ -2489,7 +2489,7 @@ snapshots:
insights-funnelcorrelationtable--default--light:
hash: v1.k794b7964.3d6cf3fd19bf7fd2b7f9d822fcd66b7d10557a26bf4bd03b01f32fcc794f880f.jKBKmAVKsMdV2WrUXJtct3l-1dyvr6mOQP_ztF84bk0
insights-funnelhistogramchart--compare--dark:
- hash: v1.k794b7964.243c25013f1c49bd32748e1942907e66a5167645e28f2e4c0cc5afced3c5bba9.A3DQuQNSU2omhbPK9xJY5JUW-d6pbpWCul3DebkL_Ow
+ hash: v1.k794b7964.aa2dd12d7815abfdb02c975e4ba83d3d800ae0348cd2788fd915dd8a88526bd5.2qnsQFp61uQ5a8q50Qqh_IRszXwwfbfRWbY3HxOe1GM
insights-funnelhistogramchart--compare--light:
hash: v1.k794b7964.283dab157709f3a7857d3385fd5cbdb5c603a84adc73672c0f2d403793b10193.TkzfWpRiJQlFpynFeQM1CjcO1TBqsDQa1N1V6u5QJxQ
insights-funnelhistogramchart--default--dark:
@@ -6065,7 +6065,7 @@ snapshots:
scenes-app-marketing-analytics-cell-actions--source-cell-action-unmapped--light:
hash: v1.k794b7964.4ebf2f848b10c8b2859630215a9be1ff91fb825fc334005207556310261d3a5c.UcqJY3dqyJNbi3x7oYaZ2rEu95LAEdbGmMyEpI-Wlgc
scenes-app-mcp-analytics--dashboard--dark:
- hash: v1.k794b7964.0ea60752a844fdeb315a7097c3dbec54b88c65e83e0754a0493a3e298caa8798.l4Fy_Gv11GiegC6l-OJGVfUoSpP__Xhx41X5GRc8uWA
+ hash: v1.k794b7964.aa8074db9f16841b8436c40fc9124a0615a56a08edb0772bb88cf5377e4bf42d.1LP_nshkiTFuoA99D-QwEE4lc_56NFQZ9g3x-XQf9pc
scenes-app-mcp-analytics--dashboard--light:
hash: v1.k794b7964.9acc446340c5debc0fcc6de75280f0867883d1b14ddc2c18615998185de5965e.Vkvw6DwC5LolW8drPPax0uEP1K4LTvV-nQXzWbWmriU
scenes-app-mcp-analytics--intent-clustering--dark:
diff --git a/packages/quill/packages/charts/src/charts/BarChart/utils/bars-under-cursor.ts b/packages/quill/packages/charts/src/charts/BarChart/utils/bars-under-cursor.ts
index a9006a9dc3db..aee1094d04a2 100644
--- a/packages/quill/packages/charts/src/charts/BarChart/utils/bars-under-cursor.ts
+++ b/packages/quill/packages/charts/src/charts/BarChart/utils/bars-under-cursor.ts
@@ -1,4 +1,4 @@
-import { computeBarAtIndex, computeBarTrackRect } from '../../../core/bar-layout'
+import { applyOuterStackCaps, computeBarAtIndex, computeBarTrackRect } from '../../../core/bar-layout'
import type { BarRect } from '../../../core/canvas-renderer'
import { type BarScaleSet, groupedBandSlot, type StackedBand } from '../../../core/scales'
import type { BandSlot, Series } from '../../../core/types'
@@ -88,11 +88,14 @@ export interface BarAtCursor<S> {
/** Yields the renderable `{ series, bar }` for every visible series at `(label, dataIndex)`.
* Single source of truth shared by drawHover, tooltip narrowing, and click routing —
- * encapsulates visibility skip, stacked-band lookup, and `computeBarAtIndex`. */
+ * encapsulates visibility skip, stacked-band lookup, and `computeBarAtIndex`. Eager despite
+ * the generator shape: every bar is computed up front so stacked cap corners can be
+ * re-resolved across the whole band before the first yield. */
export function* barsAtCursor<S extends Pick<Series, 'key' | 'visibility' | 'yAxisId' | 'data'>>(
args: Omit<BarsAtCursorArgs, 'series'> & { series: readonly S[] }
): Generator<BarAtCursor<S>> {
const { series, label, dataIndex, scales, layout, isHorizontal, stackedData, topStackedKeyByAxis } = args
+ const results: BarAtCursor<S>[] = []
for (const s of series) {
if (s.visibility?.excluded) {
continue
@@ -111,9 +114,17 @@ export function* barsAtCursor<S extends Pick<Series, 'key' | 'visibility' | 'yAx
isTopOfStack,
})
if (bar) {
- yield { series: s, bar }
+ results.push({ series: s, bar })
}
}
+ // Match the static layer's per-band cap resolution so hover highlights round the same corners.
+ applyOuterStackCaps(
+ results.map((r) => ({ bar: r.bar, yAxisId: r.series.yAxisId })),
+ scales,
+ isHorizontal,
+ layout
+ )
+ yield* results
}
/** True when the cursor sits in a bar's inert volume gap — lined up on the band axis with a bar
diff --git a/packages/quill/packages/charts/src/charts/BarChart/utils/draw-bar-chart.ts b/packages/quill/packages/charts/src/charts/BarChart/utils/draw-bar-chart.ts
index 02dc33e36d46..3bf7b39bea75 100644
--- a/packages/quill/packages/charts/src/charts/BarChart/utils/draw-bar-chart.ts
+++ b/packages/quill/packages/charts/src/charts/BarChart/utils/draw-bar-chart.ts
@@ -1,6 +1,12 @@
import { color as d3Color } from 'd3-color'
-import { bandCenter, type BarChartPrivate, buildBarLayers, computeBarTrackRect } from '../../../core/bar-layout'
+import {
+ applyOuterStackCaps,
+ bandCenter,
+ type BarChartPrivate,
+ buildBarLayers,
+ computeBarTrackRect,
+} from '../../../core/bar-layout'
import {
BAR_HIGHLIGHT_DARKEN,
BAR_TRACK_HOVER_ALPHA,
@@ -143,6 +149,16 @@ export function drawBarChartStatic(
topStackedKeyByAxis,
})
+ // Stacked cap rounding is re-resolved per band from the laid-out rects, so breakdown and
+ // diverging stacks round their actual outer segments.
+ applyOuterStackCaps(
+ seriesBars.flatMap((sb) => sb.bars.map((bar) => ({ bar, yAxisId: sb.series.yAxisId }))),
+ d3Scales,
+ isHorizontal,
+ barLayout,
+ roundStackEnds
+ )
+
// `roundStackEnds`: round both outer ends of the whole stack into a pill by clipping
// the bar layer to a rounded rect spanning each band's full extent, then drawing the
// segments square. The clip rounds the outer corners at the full radius even when the
diff --git a/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsx b/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsx
index a5c545df6fd5..c79cbf029c33 100644
--- a/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsx
+++ b/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsx
@@ -1,7 +1,7 @@
import { color as d3Color } from 'd3-color'
import React, { useCallback, useMemo } from 'react'
-import { bandCenter, buildBarLayers, computeBarAtIndex, groupedBarCenter } from '../../core/bar-layout'
+import { applyOuterStackCaps, bandCenter, buildBarLayers, computeBarAtIndex, groupedBarCenter } from '../../core/bar-layout'
import {
BAR_HIGHLIGHT_DARKEN,
DEFAULT_BAR_CORNER_RADIUS,
@@ -13,6 +13,7 @@ import {
drawGrid,
drawLineHoverPoints,
drawLineSeriesLayer,
+ type BarRect,
type DrawContext,
} from '../../core/canvas-renderer'
import { Chart } from '../../core/Chart'
@@ -215,6 +216,14 @@ function ComboChartInner<Meta = unknown>({
stackedData: barStackedData,
topStackedKeyByAxis,
})
+ // Stacked cap rounding is re-resolved per band from the laid-out rects, so breakdown
+ // and diverging stacks round their actual outer segments.
+ applyOuterStackCaps(
+ barLayers.flatMap((layer) => layer.bars.map((bar) => ({ bar, yAxisId: layer.series.yAxisId }))),
+ comboScales,
+ false,
+ barLayout
+ )
for (const { series: s, bars } of barLayers) {
drawBars(baseDrawCtx, s, bars, barCornerRadius)
}
@@ -296,6 +305,7 @@ function ComboChartInner<Meta = unknown>({
}).hits
: null
+ const hoveredBars: { series: ResolvedSeries; bar: BarRect }[] = []
for (const s of barSeries) {
if (barHits && !barHits.has(s.key)) {
continue
@@ -314,6 +324,16 @@ function ComboChartInner<Meta = unknown>({
if (!bar) {
continue
}
+ hoveredBars.push({ series: s, bar })
+ }
+ // Match the static layer's per-band cap resolution so highlights round the same corners.
+ applyOuterStackCaps(
+ hoveredBars.map((h) => ({ bar: h.bar, yAxisId: h.series.yAxisId })),
+ comboScales,
+ false,
+ barLayout
+ )
+ for (const { series: s, bar } of hoveredBars) {
const barColor = barColorAt(s, bar.dataIndex)
const highlightColor = d3Color(barColor)?.darker(BAR_HIGHLIGHT_DARKEN).toString() ?? barColor
drawBarHighlight(ctx, bar, highlightColor, barCornerRadius)
diff --git a/packages/quill/packages/charts/src/core/bar-layout.test.ts b/packages/quill/packages/charts/src/core/bar-layout.test.ts
index 7d199eae3a38..9e9c744c726e 100644
--- a/packages/quill/packages/charts/src/core/bar-layout.test.ts
+++ b/packages/quill/packages/charts/src/core/bar-layout.test.ts
@@ -1,14 +1,18 @@
+import { scaleLinear } from 'd3-scale'
+
import { dimensions, makeSeries } from '../testing'
import {
+ applyOuterStackCaps,
bandCenter,
type ComputeSeriesBarsOptions,
computeBarTrackRect,
computeSeriesBars,
cornersFor,
groupedBarCenter,
+ roundOuterStackCaps,
} from './bar-layout'
import type { BarRect } from './canvas-renderer'
-import { computeStackData, createBarScales } from './scales'
+import { type BarScaleSet, computeStackData, createBarScales } from './scales'
import type { ChartDimensions } from './types'
// Compact plot area chosen so band/value scales produce round pixel values for snapshots.
@@ -578,4 +582,141 @@ describe('hog-charts bar-layout', () => {
expect(groupedBarCenter(scales, 'L1', 'missing')).toBeUndefined()
})
})
+
+ describe('roundOuterStackCaps', () => {
+ // Vertical pixel space with the baseline at y=100: positive segments sit above (smaller y),
+ // negative segments extend below.
+ const rect = (dataIndex: number, y: number, height: number): BarRect => ({
+ x: 0,
+ y,
+ width: 10,
+ height,
+ corners: {},
+ dataIndex,
+ })
+
+ it('rounds whichever segment tops each band, not a fixed series (breakdown stacks)', () => {
+ const bandA = [rect(0, 60, 40), rect(0, 30, 30)] // second segment on top
+ const bandB = [rect(1, 40, 60)] // only segment
+ roundOuterStackCaps([...bandA, ...bandB], false, 100)
+ expect(bandA[0].corners.topLeft).toBeUndefined()
+ expect(bandA[1].corners).toMatchObject({ topLeft: true, topRight: true })
+ expect(bandB[0].corners).toMatchObject({ topLeft: true, topRight: true })
+ })
+
+ it('rounds both directions of a diverging stack — positive top and negative bottom', () => {
+ const bars = [rect(0, 50, 50), rect(0, 20, 30), rect(0, 100, 40)] // last extends below baseline
+ roundOuterStackCaps(bars, false, 100)
+ expect(bars[1].corners).toMatchObject({ topLeft: true, topRight: true })
+ expect(bars[2].corners).toMatchObject({ bottomLeft: true, bottomRight: true })
+ expect(bars[0].corners.topLeft).toBeUndefined()
+ expect(bars[0].corners.bottomLeft).toBeUndefined()
+ })
+
+ it('squares a previously-rounded cap when a higher segment exists in the band', () => {
+ const lower = { ...rect(0, 60, 40), corners: cornersFor(false, true, true) }
+ const upper = rect(0, 30, 30)
+ roundOuterStackCaps([lower, upper], false, 100)
+ expect(lower.corners.topLeft).toBeUndefined()
+ expect(lower.corners.topRight).toBeUndefined()
+ expect(upper.corners).toMatchObject({ topLeft: true, topRight: true })
+ })
+
+ it('preserves baseline-side corners a caller applied when re-resolving the cap side', () => {
+ // Bottom-of-stack segment with both ends rounded (funnel pill), overtaken by a higher segment.
+ const lower = { ...rect(0, 60, 40), corners: cornersFor(false, true, true, true) }
+ const upper = rect(0, 30, 30)
+ roundOuterStackCaps([lower, upper], false, 100)
+ expect(lower.corners.topLeft).toBeUndefined()
+ expect(lower.corners.topRight).toBeUndefined()
+ expect(lower.corners).toMatchObject({ bottomLeft: true, bottomRight: true })
+ })
+
+ it('rounds the bottom cap of a bar that straddles the baseline within tolerance', () => {
+ // Top edge inside the ±0.5 tolerance band, bottom edge clearing it: the bar is
+ // classified as extending downward, so its rounding must land on the bottom side.
+ const straddler = rect(0, 99.6, 1)
+ roundOuterStackCaps([straddler], false, 100)
+ expect(straddler.corners).toMatchObject({ bottomLeft: true, bottomRight: true })
+ expect(straddler.corners.topLeft).toBeUndefined()
+ expect(straddler.corners.topRight).toBeUndefined()
+ })
+
+ it('skips invisible slivers so a zero-height segment cannot steal the cap', () => {
+ const visible = rect(0, 60, 40)
+ const sliver = rect(0, 59.9, 0.2)
+ roundOuterStackCaps([visible, sliver], false, 100)
+ expect(visible.corners).toMatchObject({ topLeft: true, topRight: true })
+ expect(sliver.corners.topLeft).toBeUndefined()
+ })
+
+ it('rounds the outer right/left caps in horizontal orientation', () => {
+ const hrect = (dataIndex: number, x: number, width: number): BarRect => ({
+ x,
+ y: 0,
+ width,
+ height: 10,
+ corners: {},
+ dataIndex,
+ })
+ const bars = [hrect(0, 0, 60), hrect(0, 60, 30)]
+ roundOuterStackCaps(bars, true, 0)
+ expect(bars[1].corners).toMatchObject({ topRight: true, bottomRight: true })
+ expect(bars[0].corners.topRight).toBeUndefined()
+ })
+ })
+
+ describe('applyOuterStackCaps', () => {
+ const rect = (dataIndex: number, y: number, height: number): BarRect => ({
+ x: 0,
+ y,
+ width: 10,
+ height,
+ corners: {},
+ dataIndex,
+ })
+ // Only `value` and `yAxes` are read by the cap pass; primary zero at y=100, right-axis zero at y=80.
+ const dualAxisScales = {
+ value: scaleLinear().domain([0, 100]).range([100, 0]),
+ yAxes: {
+ left: { scale: scaleLinear().domain([0, 100]).range([100, 0]), position: 'left' },
+ right: { scale: scaleLinear().domain([0, 100]).range([80, 0]), position: 'right' },
+ },
+ } as unknown as BarScaleSet
+
+ it('resolves each axis group against its own zero pixel (dual-axis combo stacks)', () => {
+ // Negative on the right axis (below y=80) yet above the primary zero (y=100): judged
+ // against the shared baseline it would read as positive and lose its bottom cap.
+ const rightBar = rect(0, 80, 15)
+ const leftBar = rect(0, 60, 40)
+ applyOuterStackCaps(
+ [
+ { bar: rightBar, yAxisId: 'right' },
+ { bar: leftBar, yAxisId: 'left' },
+ ],
+ dualAxisScales,
+ false,
+ 'stacked'
+ )
+ expect(leftBar.corners).toMatchObject({ topLeft: true, topRight: true })
+ expect(rightBar.corners).toMatchObject({ bottomLeft: true, bottomRight: true })
+ })
+
+ it('falls back to the primary value scale for entries without a yAxisId', () => {
+ const bar = rect(0, 60, 40)
+ applyOuterStackCaps([{ bar }], dualAxisScales, false, 'stacked')
+ expect(bar.corners).toMatchObject({ topLeft: true, topRight: true })
+ })
+
+ it.each([
+ { name: 'grouped layout', layout: 'grouped' as const, roundStackEnds: false },
+ { name: 'roundStackEnds', layout: 'stacked' as const, roundStackEnds: true },
+ ])('is a no-op under $name', ({ layout, roundStackEnds }) => {
+ const bar = { ...rect(0, 60, 40), corners: cornersFor(false, true, true) }
+ const other = rect(0, 30, 30)
+ applyOuterStackCaps([{ bar }, { bar: other }], dualAxisScales, false, layout, roundStackEnds)
+ expect(bar.corners).toMatchObject({ topLeft: true, topRight: true })
+ expect(other.corners).toEqual({})
+ })
+ })
})
diff --git a/packages/quill/packages/charts/src/core/bar-layout.ts b/packages/quill/packages/charts/src/core/bar-layout.ts
index 4970bf57abbe..6069c719a3f3 100644
--- a/packages/quill/packages/charts/src/core/bar-layout.ts
+++ b/packages/quill/packages/charts/src/core/bar-layout.ts
@@ -53,6 +53,120 @@ export function cornersFor(
return corners
}
+// A segment thinner than this can't visibly carry a rounded cap; skipping it stops an invisible
+// sliver (e.g. a zero-valued breakdown at the top of the stack order) from stealing the cap.
+const MIN_CAP_SEGMENT_PX = 0.5
+
+// How far a cap edge must clear the baseline to count as extending in that direction. Same value
+// as MIN_CAP_SEGMENT_PX but a distinct concept: an epsilon around the baseline, not a segment size.
+const BASELINE_TOLERANCE_PX = 0.5
+
+/** Re-resolve stacked cap rounding per band, geometrically, from the laid-out rects: within each
+ * `dataIndex`, only the segment reaching furthest away from the baseline in each direction keeps
+ * a rounded cap — everything else's cap is squared. Decided after layout so breakdown stacks
+ * (whose top layer varies band to band) and diverging stacks (negative bottoms) round the actual
+ * outer segment, not the last series in stack order. Mutates `corners` in place; baseline
+ * corners are untouched. */
+export function roundOuterStackCaps(bars: BarRect[], isHorizontal: boolean, baselinePx: number): void {
+ const outerPositive = new Map<number, BarRect>()
+ const outerNegative = new Map<number, BarRect>()
+ for (const bar of bars) {
+ const size = isHorizontal ? bar.width : bar.height
+ if (size < MIN_CAP_SEGMENT_PX) {
+ continue
+ }
+ // The cap edge, signed so "further from the baseline" compares uniformly per direction.
+ // Vertical: smaller y is further up; horizontal: larger x+width is further right.
+ if (isHorizontal) {
+ if (bar.x + bar.width > baselinePx + BASELINE_TOLERANCE_PX) {
+ const prev = outerPositive.get(bar.dataIndex)
+ if (!prev || bar.x + bar.width >= prev.x + prev.width) {
+ outerPositive.set(bar.dataIndex, bar)
+ }
+ }
+ if (bar.x < baselinePx - BASELINE_TOLERANCE_PX) {
+ const prev = outerNegative.get(bar.dataIndex)
+ if (!prev || bar.x <= prev.x) {
+ outerNegative.set(bar.dataIndex, bar)
+ }
+ }
+ } else {
+ if (bar.y < baselinePx - BASELINE_TOLERANCE_PX) {
+ const prev = outerPositive.get(bar.dataIndex)
+ if (!prev || bar.y <= prev.y) {
+ outerPositive.set(bar.dataIndex, bar)
+ }
+ }
+ if (bar.y + bar.height > baselinePx + BASELINE_TOLERANCE_PX) {
+ const prev = outerNegative.get(bar.dataIndex)
+ if (!prev || bar.y + bar.height >= prev.y + prev.height) {
+ outerNegative.set(bar.dataIndex, bar)
+ }
+ }
+ }
+ }
+ for (const bar of bars) {
+ const isOuterPositive = outerPositive.get(bar.dataIndex) === bar
+ const isOuterNegative = outerNegative.get(bar.dataIndex) === bar
+ // Rewrite only the bar's own cap side (away from the baseline), so baseline-side rounding
+ // a caller may have applied is preserved. Same tolerance as the outer-segment classification
+ // above — a bar straddling the baseline within tolerance must resolve to the same direction
+ // it was classified under, or its rounding lands on the wrong side.
+ const positive = isHorizontal
+ ? bar.x + bar.width > baselinePx + BASELINE_TOLERANCE_PX
+ : bar.y < baselinePx - BASELINE_TOLERANCE_PX
+ const cap = isOuterPositive || isOuterNegative || undefined
+ if (isHorizontal) {
+ if (positive) {
+ bar.corners.topRight = bar.corners.bottomRight = cap
+ } else {
+ bar.corners.topLeft = bar.corners.bottomLeft = cap
+ }
+ } else if (positive) {
+ bar.corners.topLeft = bar.corners.topRight = cap
+ } else {
+ bar.corners.bottomLeft = bar.corners.bottomRight = cap
+ }
+ }
+}
+
+/** A laid-out bar paired with the axis its series scales against (`Series.yAxisId`). */
+export interface AxisBarEntry {
+ bar: BarRect
+ yAxisId?: string
+}
+
+/** The shared cap-rounding pass over laid-out bars: groups them by value axis and runs
+ * {@link roundOuterStackCaps} per group against that axis's own zero pixel — a secondary-axis
+ * stack (ComboChart) must not be judged against the primary baseline. Owns the layout guard:
+ * no-op for grouped layouts (caps are per-bar) and under `roundStackEnds` (the pill clip owns
+ * the corners), so static, hover, and cursor paths can't diverge on when the pass applies. */
+export function applyOuterStackCaps(
+ entries: readonly AxisBarEntry[],
+ scales: BarScaleSet,
+ isHorizontal: boolean,
+ layout: 'stacked' | 'grouped' | 'percent',
+ roundStackEnds: boolean = false
+): void {
+ if (layout === 'grouped' || roundStackEnds) {
+ return
+ }
+ const barsByAxis = new Map<string, BarRect[]>()
+ for (const { bar, yAxisId } of entries) {
+ const axisId = yAxisId ?? DEFAULT_Y_AXIS_ID
+ const group = barsByAxis.get(axisId)
+ if (group) {
+ group.push(bar)
+ } else {
+ barsByAxis.set(axisId, [bar])
+ }
+ }
+ for (const [axisId, bars] of barsByAxis) {
+ const valueScale = scales.yAxes?.[axisId]?.scale ?? scales.value
+ roundOuterStackCaps(bars, isHorizontal, valueScale(0))
+ }
+}
+
function makeBarRect(
isHorizontal: boolean,
bandStart: number,