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(charts): round only the outer cap of a diverging stack's negative segment (#101746)

Sam Penningtona426d347dca6
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 7cfaf3675a01..3d0af26b6ce1 100644
--- a/packages/quill/packages/charts/src/core/bar-layout.test.ts
+++ b/packages/quill/packages/charts/src/core/bar-layout.test.ts
@@ -12,7 +12,7 @@ import {
roundOuterStackCaps,
} from './bar-layout'
import type { BarRect } from './canvas-renderer'
-import { type BarScaleSet, computeStackData, createBarScales } from './scales'
+import { type BarScaleSet, computeDivergingStackData, computeStackData, createBarScales } from './scales'
import type { ChartDimensions } from './types'
// Compact plot area chosen so band/value scales produce round pixel values for snapshots.
@@ -163,6 +163,56 @@ describe('hog-charts bar-layout', () => {
expect(bars[0]?.corners).toEqual(expectedCorners)
})
+ it.each([
+ { desc: 'positive', key: 'pos', expectedCorners: { topLeft: true, topRight: true } },
+ { desc: 'negative', key: 'neg', expectedCorners: { bottomLeft: true, bottomRight: true } },
+ ])('rounds the cap away from the baseline for a $desc diverging segment', ({ key, expectedCorners }) => {
+ const pos = makeSeries({ key: 'pos', data: [10] })
+ const neg = makeSeries({ key: 'neg', data: [-5] })
+ const stacks = computeDivergingStackData([pos, neg], ['a'])
+ const stackedSeries = [pos, neg].flatMap((s) => [
+ { ...s, data: stacks.get(s.key)!.top },
+ { ...s, key: `${s.key}__bottom`, data: stacks.get(s.key)!.bottom },
+ ])
+ const scales = createBarScales([pos, neg], ['a'], dimensions, { barLayout: 'stacked', stackedSeries })
+ const series = key === 'pos' ? pos : neg
+ const bars = layoutOf({
+ series,
+ scales,
+ layout: 'stacked',
+ stackedBand: stacks.get(key),
+ isTopOfStack: true,
+ })
+ expect(bars[0]?.corners).toEqual(expectedCorners)
+ })
+
+ it.each([
+ { desc: 'vertical', isHorizontal: false, expectedCorners: { topLeft: true, topRight: true } },
+ { desc: 'horizontal', isHorizontal: true, expectedCorners: { topRight: true, bottomRight: true } },
+ ])('rounds the cap away from a clamped log baseline ($desc)', ({ isHorizontal, expectedCorners }) => {
+ // A log domain excludes 0, so d3 clamps valueScale(0) to the domain minimum — which is the
+ // baseline end of the pixel range, so a positive segment still reads positive.
+ const a = makeSeries({ key: 'a', data: [10] })
+ const b = makeSeries({ key: 'b', data: [100] })
+ const labels = ['a']
+ const stacks = computeStackData([a, b], labels)
+ const scales = createBarScales([a, b], labels, dimensions, {
+ barLayout: 'stacked',
+ scaleType: 'log',
+ axisOrientation: isHorizontal ? 'horizontal' : 'vertical',
+ stackedSeries: [a, b].map((s) => ({ ...s, data: stacks.get(s.key)!.top })),
+ })
+ const bars = layoutOf({
+ series: b,
+ scales,
+ isHorizontal,
+ layout: 'stacked',
+ stackedBand: stacks.get('b'),
+ isTopOfStack: true,
+ })
+ 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).
diff --git a/packages/quill/packages/charts/src/core/bar-layout.ts b/packages/quill/packages/charts/src/core/bar-layout.ts
index 4f9b72bdc4cd..047b837e3684 100644
--- a/packages/quill/packages/charts/src/core/bar-layout.ts
+++ b/packages/quill/packages/charts/src/core/bar-layout.ts
@@ -392,16 +392,19 @@ export function computeBarAtIndex({
const valueRangeMax = Math.max(valueRangeA, valueRangeB)
topPixel = Math.min(Math.max(flooredTopPixel, valueRangeMin), valueRangeMax)
}
- // 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
+ // A diverging stack emits a negative segment as [bottom = cumulative, top = towards zero] — the
+ // same pixel ordering as a positive segment — so direction comes from which side of the value
+ // baseline the segment sits on, not from which edge is further along the axis.
+ const midPixel = (topPixel + bottomPixel) / 2
+ const basePixel = valueScale(0)
+ const isPositive = isHorizontal ? midPixel >= basePixel : midPixel <= basePixel
const corners = cornersFor(isHorizontal, isPositive, shouldRoundCap, shouldRoundBaseline)
// Extend an interior segment a sub-pixel toward the baseline so it overlaps its lower neighbour,
// hiding the faint anti-aliased seam where two adjacent fills meet on a fractional device pixel.
// The bottom-of-stack segment sits on the value-axis baseline, so it's left exact — extending it
// would only overpaint the axis. The cap (away-from-baseline) side is always exact so cap
// rounding and the stack's outer edge stay put.
- const sitsOnBaseline = Math.abs(bottomPixel - valueScale(0)) < 0.001
+ const sitsOnBaseline = Math.abs(bottomPixel - basePixel) < 0.001
const overlappedBottom = sitsOnBaseline
? bottomPixel
: bottomPixel + STACK_SEGMENT_OVERLAP_PX * Math.sign(bottomPixel - topPixel)