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.

chore(quill-charts): extract bar band geometry into core/bar-layout (#61970)

Sam Pennington47fa5311bd80
diff --git a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx
index 03b104e526ec..98e2507f79db 100644
--- a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx
+++ b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx
@@ -1,7 +1,13 @@
import { color as d3Color } from 'd3-color'
import React, { useCallback, useMemo, useRef } from 'react'
-import { type BarChartPrivate, computeBarTrackRect, computeSeriesBars } from '../../core/bar-layout'
+import {
+ bandCenter,
+ type BarChartPrivate,
+ computeBarTrackRect,
+ computeSeriesBars,
+ groupedBarCenter,
+} from '../../core/bar-layout'
import {
BAR_TRACK_HOVER_ALPHA,
type BarRect,
@@ -27,7 +33,6 @@ import {
computePercentStackData,
computeStackData,
createBarScales,
- groupedBandSlot,
type StackedBand,
yTickCountForHeight,
} from '../../core/scales'
@@ -58,19 +63,6 @@ import {
iterBarsAtCursor,
} from './utils/bars-under-cursor'
-function bandCenter(scales: BarChartPrivate['__barChart'], label: string): number | undefined {
- const start = scales.band(label)
- return start == null ? undefined : start + scales.band.bandwidth() / 2
-}
-
-/** Center of a specific series's bar within a band. Used by overlays (e.g. annotations)
- * to anchor on the current-period bar in compare-against-previous grouped layouts.
- * Returns undefined when the layout isn't grouped or the series isn't in the group scale. */
-function groupedBarCenter(scales: BarChartPrivate['__barChart'], label: string, seriesKey: string): number | undefined {
- const slot = groupedBandSlot(scales, label, seriesKey)
- return slot && slot.x + slot.width / 2
-}
-
export interface BarChartProps<Meta = unknown> {
series: Series<Meta>[]
labels: string[]
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 c8796471ea07..e342bb9b5ecd 100644
--- a/packages/quill/packages/charts/src/core/bar-layout.test.ts
+++ b/packages/quill/packages/charts/src/core/bar-layout.test.ts
@@ -1,5 +1,12 @@
import { dimensions, makeSeries } from '../testing'
-import { type ComputeSeriesBarsOptions, computeBarTrackRect, computeSeriesBars, cornersFor } from './bar-layout'
+import {
+ bandCenter,
+ type ComputeSeriesBarsOptions,
+ computeBarTrackRect,
+ computeSeriesBars,
+ cornersFor,
+ groupedBarCenter,
+} from './bar-layout'
import type { BarRect } from './canvas-renderer'
import { computeStackData, createBarScales } from './scales'
import type { ChartDimensions } from './types'
@@ -469,4 +476,55 @@ describe('hog-charts bar-layout', () => {
})
})
})
+
+
+ describe('bandCenter', () => {
+ it.each([
+ { label: 'a', expected: 50 },
+ { label: 'b', expected: 150 },
+ ])('returns the pixel center $expected for label $label', ({ label, expected }) => {
+ const s = makeSeries({ key: 's', data: [10, 20] })
+ const scales = createBarScales([s], ['a', 'b'], PIXEL_TEST_DIMENSIONS, {
+ barLayout: 'grouped',
+ bandPadding: 0,
+ groupPadding: 0,
+ })
+ // Two bands across 200px → band width 100, centers at 50 and 150.
+ expect(bandCenter(scales, label)).toBeCloseTo(expected, 5)
+ })
+
+ it('returns undefined for an unknown band', () => {
+ const s = makeSeries({ key: 's', data: [10] })
+ const scales = createBarScales([s], ['a'], PIXEL_TEST_DIMENSIONS, { barLayout: 'grouped' })
+ expect(bandCenter(scales, 'missing')).toBeUndefined()
+ })
+ })
+
+
+ describe('groupedBarCenter', () => {
+ it.each([
+ { seriesKey: 'a', expected: 25 },
+ { seriesKey: 'b', expected: 75 },
+ ])("returns the center $expected for series $seriesKey's sub-band in a grouped layout", ({ seriesKey, expected }) => {
+ const a = makeSeries({ key: 'a', data: [10, 20] })
+ const b = makeSeries({ key: 'b', data: [20, 10] })
+ const scales = createBarScales([a, b], ['L1', 'L2'], PIXEL_TEST_DIMENSIONS, {
+ barLayout: 'grouped',
+ bandPadding: 0,
+ groupPadding: 0,
+ })
+ // a@L1 slot x:0 w:50 → center 25; b@L1 slot x:50 w:50 → center 75 (see grouped pixel case).
+ expect(groupedBarCenter(scales, 'L1', seriesKey)).toBeCloseTo(expected, 5)
+ })
+
+ it('returns undefined when the series is not in the group scale', () => {
+ const a = makeSeries({ key: 'a', data: [10] })
+ const scales = createBarScales([a], ['L1'], PIXEL_TEST_DIMENSIONS, {
+ barLayout: 'grouped',
+ bandPadding: 0,
+ groupPadding: 0,
+ })
+ expect(groupedBarCenter(scales, 'L1', 'missing')).toBeUndefined()
+ })
+ })
})
diff --git a/packages/quill/packages/charts/src/core/bar-layout.ts b/packages/quill/packages/charts/src/core/bar-layout.ts
index bc1bed6ff4b6..0490fad37843 100644
--- a/packages/quill/packages/charts/src/core/bar-layout.ts
+++ b/packages/quill/packages/charts/src/core/bar-layout.ts
@@ -205,3 +205,17 @@ export function computeBarTrackRect(
}
: { x: bar.x, y: valueMin, width: bar.width, height: valueSize, corners: bar.corners, dataIndex: bar.dataIndex }
}
+
+/** Pixel center of a band along the band axis — the anchor for band-level tooltips and grid ticks. */
+export function bandCenter(scales: BarScaleSet, label: string): number | undefined {
+ const start = scales.band(label)
+ return start == null ? undefined : start + scales.band.bandwidth() / 2
+}
+
+/** Center of a specific series's bar within a band. Used by overlays (e.g. annotations)
+ * to anchor on the current-period bar in compare-against-previous grouped layouts.
+ * Returns undefined when the layout isn't grouped or the series isn't in the group scale. */
+export function groupedBarCenter(scales: BarScaleSet, label: string, seriesKey: string): number | undefined {
+ const slot = groupedBandSlot(scales, label, seriesKey)
+ return slot && slot.x + slot.width / 2
+}