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): add a horizontal box plot (#101230)

Julian Bez6f44af502170
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index ccfdf30538af..acc7dfe9ef13 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -704,6 +704,14 @@ snapshots:
hash: v1.k794b7964.8130eafe275f6eb471a60a4eec4f54f07f243c664aba0a64e5431f7728b22fe0.5-iHw88oelBcXyENYl4mnYzsVNsQR7qDfpYyz4GPdWw
components-hogcharts-barchart--with-value-labels--light:
hash: v1.k794b7964.6aff50b1c147df21230d149c147deb023f6308cd2e01983b58e65461dd4c7207.vLZ6XqXFhSTA3hr10RUEv9vzOgxKWYtUzWj0OvoCTU0
+ components-hogcharts-boxplot--horizontal--dark:
+ hash: v1.k794b7964.30cbeacc1f375b50732dd7c20765bbedfc0dc626d4896c8fa9687aa5f9bfc001.FTUzztXbfM9Du-zf-HPWGeBiY0cPJRqb3Ybj7dYNln8
+ components-hogcharts-boxplot--horizontal--light:
+ hash: v1.k794b7964.aec9e9c2b045f52ff9c7b439bc22260f45ca59a8965a8086ba9150aea221edd4.JkVfjROZzhUJfai3iKOmQmvmcOSY92m5tKg24b65qpA
+ components-hogcharts-boxplot--horizontal-log-scale--dark:
+ hash: v1.k794b7964.3ac079cae9db4bd2deb288342d8954ccd11e58a34e8e0cb8fc6ec92dfb82b1b1.Gm5cGG6ZEsju02uwqNvHF62mtJuI7Poo7cA_VCxmjHg
+ components-hogcharts-boxplot--horizontal-log-scale--light:
+ hash: v1.k794b7964.f4b58820e4955ee8d26de3cb2f9e743c3ff5f7cbfe94253ed95d7fce11b0fe51.47EN0L76m4Tszagv7IimkYbuVmbTOj3OHAkbertIvuA
components-hogcharts-boxplot--multi-series-grouped--dark:
hash: v1.k794b7964.fd8a8c11e35ef420a663807a21a6349eba0ae2658c6c3d114763f4629dd45934.d23KBypemM2QY55g8_QWVUc9diJoAL1oNZjK3d3Ebs4
components-hogcharts-boxplot--multi-series-grouped--light:
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.stories.tsx b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.stories.tsx
index ffdac25973aa..9176be152823 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.stories.tsx
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.stories.tsx
@@ -103,6 +103,47 @@ export const MultiSeriesGrouped: Story = {
},
}
+const WAIT_HOURS: BoxPlotSeries[] = [
+ {
+ key: 'wait',
+ label: 'Wait (hours)',
+ color: '',
+ data: [d(0.2, 1.5, 4, 9, 12, 60), d(0.1, 0.8, 2.5, 6, 8, 96)],
+ },
+]
+
+export const Horizontal: Story = {
+ render: () => {
+ const theme = useReactiveTheme()
+ return (
+ <Stage>
+ <BoxPlot
+ series={WAIT_HOURS}
+ labels={['This author', 'Repo']}
+ theme={theme}
+ config={{ showGrid: true, axisOrientation: 'horizontal' }}
+ />
+ </Stage>
+ )
+ },
+}
+
+export const HorizontalLogScale: Story = {
+ render: () => {
+ const theme = useReactiveTheme()
+ return (
+ <Stage>
+ <BoxPlot
+ series={WAIT_HOURS}
+ labels={['This author', 'Repo']}
+ theme={theme}
+ config={{ showGrid: true, axisOrientation: 'horizontal', yScaleType: 'log' }}
+ />
+ </Stage>
+ )
+ },
+}
+
export const NoGrid: Story = {
render: () => {
const theme = useReactiveTheme()
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.test.tsx b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.test.tsx
index c7b8ba156ad1..03e0a7edd479 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.test.tsx
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.test.tsx
@@ -86,6 +86,20 @@ describe('BoxPlot', () => {
expect(maxTickNumeric).toBeGreaterThanOrEqual(250)
})
+ it('lists the labels down the y-axis and the values along x when horizontal', () => {
+ const { chart } = renderHogChart(
+ <BoxPlot
+ series={TWO_SERIES}
+ labels={LABELS}
+ theme={THEME}
+ config={{ axisOrientation: 'horizontal' }}
+ />
+ )
+ expect(chart.yTicks()).toEqual(LABELS)
+ const values = chart.xTicks().map((tick) => parseFloat(tick.replace(/[^\d.-]/g, '')))
+ expect(Math.max(...values)).toBeGreaterThanOrEqual(100)
+ })
+
it('forwards `dataAttr` to the chart wrapper', () => {
const { chart } = renderHogChart(
<BoxPlot series={TWO_SERIES} labels={LABELS} theme={THEME} dataAttr="boxplot-instance" />
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx
index 8549d50d15bf..58605f9e9114 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx
@@ -57,10 +57,9 @@ export interface BoxPlotAdaptedMeta<Meta = unknown> {
* don't have to redeclare it. */
export type BoxPlotTooltipContext<Meta = unknown> = TooltipContext<BoxPlotAdaptedMeta<Meta>>
-/** Chart-level config. `axisOrientation` is always vertical for BoxPlot — there's no
- * horizontal mode — so it's omitted from the consumer-visible config to avoid silently
- * ignored values. */
-export interface BoxPlotConfig extends Omit<ChartConfig, 'axisOrientation'> {
+/** Chart-level config. `axisOrientation: 'horizontal'` lists the labels down the y-axis and draws
+ * the value axis along x, which suits a few long boxes better than a row of narrow columns. */
+export interface BoxPlotConfig extends ChartConfig {
legend?: ChartLegendConfig
/** Mean marker radius in CSS pixels. Defaults to 3. */
meanRadius?: number
@@ -131,7 +130,9 @@ function BoxPlotInner<Meta = unknown>({
meanRadius = 3,
whiskerCapRatio = 0.6,
boxStrokeWidth = 1.5,
+ axisOrientation = 'vertical',
} = config ?? {}
+ const horizontal = axisOrientation === 'horizontal'
const { x: xAxisLine, y: yAxisLine } = resolveAxisLines(showAxisLines)
const axisLines = useMemo(() => ({ x: xAxisLine, y: yAxisLine }), [xAxisLine, yAxisLine])
@@ -203,11 +204,11 @@ function BoxPlotInner<Meta = unknown>({
const d3Scales = createBarScales(coloredSeries, scaleLabels, dimensions, {
scaleType: yScaleType,
barLayout,
- axisOrientation: 'vertical',
+ axisOrientation,
stackedSeries: valueRangeSeries.length > 0 ? valueRangeSeries : undefined,
})
- const yTickCount = yTickCountForHeight(dimensions.plotHeight)
+ const yTickCount = yTickCountForHeight(horizontal ? dimensions.plotWidth : dimensions.plotHeight)
const priv: BoxPlotPrivate = {
__boxPlot: { scales: d3Scales, datumsByKey, grouped },
}
@@ -232,7 +233,7 @@ function BoxPlotInner<Meta = unknown>({
_private: priv,
}
},
- [grouped, yScaleType, valueRangeSeries, datumsByKey]
+ [grouped, yScaleType, axisOrientation, horizontal, valueRangeSeries, datumsByKey]
)
const drawStatic = useCallback(
@@ -258,7 +259,7 @@ function BoxPlotInner<Meta = unknown>({
drawGrid(baseDrawCtx, {
gridColor: theme.gridColor,
gridDash: theme.gridDashPattern,
- orientation: 'vertical',
+ orientation: axisOrientation,
frame: !axisLineStyle,
})
}
@@ -285,6 +286,7 @@ function BoxPlotInner<Meta = unknown>({
meanRadius,
whiskerCapRatio,
lineWidth: boxStrokeWidth,
+ horizontal,
})
}
@@ -296,7 +298,7 @@ function BoxPlotInner<Meta = unknown>({
})
}
},
- [showGrid, axisLines, meanRadius, whiskerCapRatio, boxStrokeWidth]
+ [showGrid, axisLines, meanRadius, whiskerCapRatio, boxStrokeWidth, axisOrientation, horizontal]
)
const drawHover = useCallback(
@@ -324,6 +326,7 @@ function BoxPlotInner<Meta = unknown>({
cursor: hoverPosition,
scales: priv.scales,
grouped: priv.grouped,
+ horizontal,
})
if (hits.size === 0) {
return false
@@ -349,12 +352,12 @@ function BoxPlotInner<Meta = unknown>({
if (!box) {
continue
}
- drawBoxHighlight(ctx, box, dimColor(s.color, 0.25))
+ drawBoxHighlight(ctx, box, dimColor(s.color, 0.25), horizontal)
drewAny = true
}
return drewAny
},
- [series]
+ [series, horizontal]
)
const renderTooltip = useCallback(
@@ -401,7 +404,7 @@ function BoxPlotInner<Meta = unknown>({
<Chart<BoxPlotAdaptedMeta<Meta>>
series={visibleSeries}
labels={labels}
- config={{ ...config, axisOrientation: 'vertical' }}
+ config={{ ...config, axisOrientation }}
theme={theme}
createScales={createScales}
drawStatic={drawStatic}
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.test.ts b/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.test.ts
index 47b357a8266c..9d67879f7a1f 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.test.ts
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.test.ts
@@ -41,13 +41,15 @@ function makeScales(
describe('cursorInsideBoxBand', () => {
const box = { x: 10, width: 40 }
it.each([
- ['cursor left of box', { x: 5 }, false],
- ['cursor on left edge', { x: 10 }, true],
- ['cursor inside box', { x: 30 }, true],
- ['cursor on right edge', { x: 50 }, true],
- ['cursor right of box', { x: 60 }, false],
- ])('%s', (_name, cursor, expected) => {
- expect(cursorInsideBoxBand(box, cursor)).toBe(expected)
+ ['cursor left of box', { x: 5, y: 30 }, false, false],
+ ['cursor on left edge', { x: 10, y: 0 }, false, true],
+ ['cursor inside box', { x: 30, y: 0 }, false, true],
+ ['cursor on right edge', { x: 50, y: 0 }, false, true],
+ ['cursor right of box', { x: 60, y: 30 }, false, false],
+ ['horizontal: cursor inside the band on y, outside on x', { x: 90, y: 30 }, true, true],
+ ['horizontal: cursor outside the band on y, inside on x', { x: 30, y: 90 }, true, false],
+ ])('%s', (_name, cursor, horizontal, expected) => {
+ expect(cursorInsideBoxBand(box, cursor, horizontal)).toBe(expected)
})
})
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.ts b/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.ts
index 03426cd8df4b..792e7e4dca7b 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.ts
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/utils/boxes-under-cursor.ts
@@ -6,8 +6,13 @@ import type { BoxPlotSeries } from '../types'
* bar-chart pattern of resolving "which sub-band is under the cursor" with a
* value-axis-agnostic check, so hovering above (or below) a tall whisker still
* selects the right box. */
-export function cursorInsideBoxBand(box: { x: number; width: number }, cursor: { x: number }): boolean {
- return cursor.x >= box.x && cursor.x <= box.x + box.width
+export function cursorInsideBoxBand(
+ box: { x: number; width: number },
+ cursor: { x: number; y: number },
+ horizontal = false
+): boolean {
+ const bandCoord = horizontal ? cursor.y : cursor.x
+ return bandCoord >= box.x && bandCoord <= box.x + box.width
}
export interface SeriesKeysAtCursorArgs<Meta> {
@@ -17,6 +22,8 @@ export interface SeriesKeysAtCursorArgs<Meta> {
cursor: { x: number; y: number }
scales: BarScaleSet
grouped: boolean
+ /** Bands run along y instead of x. */
+ horizontal?: boolean
}
/** Returns the set of series keys whose box (at the given x-label) contains the cursor
@@ -25,7 +32,7 @@ export interface SeriesKeysAtCursorArgs<Meta> {
* series. Uses `computeBoxBand` (band-axis only) — value-axis math is for the highlight
* pass, not the hit-test. */
export function seriesKeysAtCursor<Meta>(args: SeriesKeysAtCursorArgs<Meta>): Set<string> {
- const { series, label, dataIndex, cursor, scales, grouped } = args
+ const { series, label, dataIndex, cursor, scales, grouped, horizontal = false } = args
const hits = new Set<string>()
for (const s of series) {
if (s.visibility?.excluded) {
@@ -36,7 +43,7 @@ export function seriesKeysAtCursor<Meta>(args: SeriesKeysAtCursorArgs<Meta>): Se
continue
}
const band = computeBoxBand(s.key, label, scales, grouped)
- if (band && cursorInsideBoxBand(band, cursor)) {
+ if (band && cursorInsideBoxBand(band, cursor, horizontal)) {
hits.add(s.key)
}
}
diff --git a/packages/quill/packages/charts/src/core/canvas-renderer.ts b/packages/quill/packages/charts/src/core/canvas-renderer.ts
index 0701232a06be..fa877ebef56d 100644
--- a/packages/quill/packages/charts/src/core/canvas-renderer.ts
+++ b/packages/quill/packages/charts/src/core/canvas-renderer.ts
@@ -1323,6 +1323,15 @@ export interface DrawBoxOptions {
lineWidth?: number
/** Width of the whisker caps (as a fraction of the box width). Defaults to 0.6. */
whiskerCapRatio?: number
+ /** The boxes lie along a horizontal value axis: `x`/`width` are band pixels on y, and the value
+ * fields are pixels on x. Defaults to false. */
+ horizontal?: boolean
+}
+
+/** Swap the canvas x and y axes, so geometry laid out for vertical boxes draws horizontally. The
+ * transpose is a reflection, so line widths and marker radii keep their size. */
+function transposeAxes(ctx: CanvasRenderingContext2D): void {
+ ctx.transform(0, 1, 1, 0, 0, 0)
}
/** Paint a whole series of box-and-whiskers, batching path operations so the number of
@@ -1341,8 +1350,13 @@ export function drawBoxes(ctx: CanvasRenderingContext2D, boxes: BoxRect[], optio
meanRadius = 3,
lineWidth = 1.5,
whiskerCapRatio = 0.6,
+ horizontal = false,
} = options
+ ctx.save()
+ if (horizontal) {
+ transposeAxes(ctx)
+ }
ctx.lineWidth = lineWidth
ctx.strokeStyle = color
ctx.setLineDash([])
@@ -1411,17 +1425,28 @@ export function drawBoxes(ctx: CanvasRenderingContext2D, boxes: BoxRect[], optio
ctx.fill()
ctx.stroke()
}
+ ctx.restore()
}
/** Translucent highlight overlay for a hovered box. Drawn on the overlay canvas so it
* composites over the static box without disturbing it — mirrors {@link drawBarHighlight}. */
-export function drawBoxHighlight(ctx: CanvasRenderingContext2D, box: BoxRect, overlayColor: string): void {
+export function drawBoxHighlight(
+ ctx: CanvasRenderingContext2D,
+ box: BoxRect,
+ overlayColor: string,
+ horizontal = false
+): void {
const boxHeight = Math.max(0, box.bottom - box.top)
if (box.width <= 0 || boxHeight <= 0) {
return
}
+ ctx.save()
+ if (horizontal) {
+ transposeAxes(ctx)
+ }
ctx.fillStyle = overlayColor
ctx.fillRect(box.x, box.top, box.width, boxHeight)
+ ctx.restore()
}
type DrawHoverFn = (args: ChartDrawArgs) => DrawHoverResult
diff --git a/packages/quill/packages/charts/src/docs/chart-types.md b/packages/quill/packages/charts/src/docs/chart-types.md
index 9da021901004..74c39472f4cc 100644
--- a/packages/quill/packages/charts/src/docs/chart-types.md
+++ b/packages/quill/packages/charts/src/docs/chart-types.md
@@ -65,6 +65,7 @@ Part of whole, one value per series (`data[0]`).
Distribution summaries: `{ min, p25, median, mean, p75, max }` per label.
Supports `config.legend` for grouped series and `onBoxClick`.
+`axisOrientation: 'horizontal'` lists the labels down the y-axis and draws values along x; `yScaleType: 'log'` then applies to that x value axis, and `yTickFormatter` formats its ticks.
The tooltip receives a `BoxPlotTooltipContext`; `BoxPlotTooltip` is the default.
## Heatmap