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(insights): show true min and max in box plot tooltip (#121109)

Paul D'Ambra52f94b44d52e
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 03e0a7edd479..b533fea0451c 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.test.tsx
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.test.tsx
@@ -62,6 +62,24 @@ describe('BoxPlot', () => {
expect(maxTick).toBeGreaterThanOrEqual(200)
})
+ it('ends the y-axis at the whisker, not at a clipped outlier max', () => {
+ const series: BoxPlotSeries[] = [
+ {
+ key: 'a',
+ label: 'A',
+ data: [
+ datum({ min: 0, max: 1_000_000, whiskerMax: 120 }),
+ datum({ min: 10, max: 900_000, whiskerMax: 130 }),
+ ],
+ },
+ ]
+ const { chart } = renderHogChart(<BoxPlot series={series} labels={['Mon', 'Tue']} theme={THEME} />)
+ const nums = chart.yTicks().map((t) => parseFloat(t.replace(/[^\d.-]/g, '')))
+ const maxTick = Math.max(...nums.filter((n) => Number.isFinite(n)))
+ expect(maxTick).toBeGreaterThanOrEqual(130)
+ expect(maxTick).toBeLessThan(1000)
+ })
+
it('allocates left margin wide enough for the whisker-derived y ticks (not just medians)', () => {
// Regression: clipping of the y-tick column. `useChartMargins` reads
// `seriesValueRange(adaptedSeries)` to size the left margin, so the adapted series'
@@ -88,12 +106,7 @@ describe('BoxPlot', () => {
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' }}
- />
+ <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, '')))
@@ -182,6 +195,60 @@ describe('BoxPlot', () => {
expect(rowLabels).toEqual(['Max', '75th percentile', 'Median', 'Mean', '25th percentile', 'Min'])
})
+ it.each([
+ {
+ name: 'lists the clipped whiskers between the true extremes and the box',
+ override: { min: 1, p25: 20, median: 30, mean: 40, p75: 50, max: 900, whiskerMin: 10, whiskerMax: 80 },
+ rows: [
+ ['Max', '900'],
+ ['Upper whisker', '80'],
+ ['75th percentile', '50'],
+ ['Median', '30'],
+ ['Mean', '40'],
+ ['25th percentile', '20'],
+ ['Lower whisker', '10'],
+ ['Min', '1'],
+ ],
+ },
+ {
+ name: 'lists only the whisker that is clipped',
+ override: { min: 1, p25: 20, median: 30, mean: 40, p75: 50, max: 900, whiskerMin: 1, whiskerMax: 80 },
+ rows: [
+ ['Max', '900'],
+ ['Upper whisker', '80'],
+ ['75th percentile', '50'],
+ ['Median', '30'],
+ ['Mean', '40'],
+ ['25th percentile', '20'],
+ ['Min', '1'],
+ ],
+ },
+ {
+ name: 'omits whisker rows that equal the true extremes',
+ override: { min: 1, p25: 20, median: 30, mean: 40, p75: 50, max: 90, whiskerMin: 1, whiskerMax: 90 },
+ rows: [
+ ['Max', '90'],
+ ['75th percentile', '50'],
+ ['Median', '30'],
+ ['Mean', '40'],
+ ['25th percentile', '20'],
+ ['Min', '1'],
+ ],
+ },
+ ])('$name', async ({ override, rows }) => {
+ const series: BoxPlotSeries[] = [{ key: 'a', label: 'A-label', data: [datum(override), datum(), datum()] }]
+ const { chart } = renderHogChart(<BoxPlot series={series} labels={LABELS} theme={THEME} />, {
+ nativeTooltip: true,
+ })
+ chart.hoverAtIndex(0)
+ const tooltipEl = await waitForHogChartTooltip()
+ const rendered = Array.from(tooltipEl.querySelectorAll('tr')).map((tr) => [
+ (tr.firstElementChild as HTMLElement).textContent!.trim(),
+ (tr.lastElementChild as HTMLElement).textContent!.trim(),
+ ])
+ expect(rendered).toEqual(rows)
+ })
+
it('renders the values in the canonical row order', async () => {
const { chart } = renderHogChart(<BoxPlot series={SINGLE_SERIES} labels={LABELS} theme={THEME} />, {
nativeTooltip: true,
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx
index c757915a8bc2..1096e70f3ae7 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx
@@ -174,11 +174,13 @@ function BoxPlotInner<Meta = unknown>({
if (!datum) {
continue
}
- if (Number.isFinite(datum.min)) {
- mins.push(datum.min)
+ const whiskerMin = datum.whiskerMin ?? datum.min
+ const whiskerMax = datum.whiskerMax ?? datum.max
+ if (Number.isFinite(whiskerMin)) {
+ mins.push(whiskerMin)
}
- if (Number.isFinite(datum.max)) {
- maxs.push(datum.max)
+ if (Number.isFinite(whiskerMax)) {
+ maxs.push(whiskerMax)
}
}
if (mins.length > 0) {
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlotTooltip.tsx b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlotTooltip.tsx
index 750ee1e50449..6cfd69230bb3 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlotTooltip.tsx
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlotTooltip.tsx
@@ -5,15 +5,26 @@ import { TooltipSurface, TooltipSwatch } from '../../overlays/TooltipSurface'
import type { BoxPlotAdaptedMeta } from './BoxPlot'
import type { BoxPlotDatum } from './types'
-/** Rows shown per box, in the same order the legacy BoxPlotChart used. */
-const ROWS: { label: string; key: keyof BoxPlotDatum }[] = [
- { label: 'Max', key: 'max' },
- { label: '75th percentile', key: 'p75' },
- { label: 'Median', key: 'median' },
- { label: 'Mean', key: 'mean' },
- { label: '25th percentile', key: 'p25' },
- { label: 'Min', key: 'min' },
-]
+interface TooltipRow {
+ label: string
+ value: number
+}
+
+/** Whisker rows only appear when a whisker is clipped, so it differs from the true bound. */
+function tooltipRows(datum: BoxPlotDatum): TooltipRow[] {
+ const { whiskerMin, whiskerMax } = datum
+ const rows: (TooltipRow | null)[] = [
+ { label: 'Max', value: datum.max },
+ whiskerMax !== undefined && whiskerMax !== datum.max ? { label: 'Upper whisker', value: whiskerMax } : null,
+ { label: '75th percentile', value: datum.p75 },
+ { label: 'Median', value: datum.median },
+ { label: 'Mean', value: datum.mean },
+ { label: '25th percentile', value: datum.p25 },
+ whiskerMin !== undefined && whiskerMin !== datum.min ? { label: 'Lower whisker', value: whiskerMin } : null,
+ { label: 'Min', value: datum.min },
+ ]
+ return rows.filter((row): row is TooltipRow => row !== null)
+}
export interface BoxPlotTooltipProps<Meta = unknown> {
ctx: TooltipContext<BoxPlotAdaptedMeta<Meta>>
@@ -72,12 +83,10 @@ export function BoxPlotTooltip<Meta = unknown>({
)}
<table className="border-collapse">
<tbody>
- {ROWS.map((row) => (
- <tr key={row.key}>
+ {tooltipRows(entry.datum).map((row) => (
+ <tr key={row.label}>
<td className="pr-3 opacity-70">{row.label}</td>
- <td className="font-medium">
- {defaultFormatValue(entry.datum[row.key] as number)}
- </td>
+ <td className="font-medium">{defaultFormatValue(row.value)}</td>
</tr>
))}
</tbody>
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.test.ts b/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.test.ts
index a650655371a2..d06fee741316 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.test.ts
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.test.ts
@@ -90,6 +90,23 @@ describe('computeBoxRect', () => {
expect(box.whiskerBottom).toBeGreaterThan(box.bottom)
})
+ it('draws whiskers at whiskerMin and whiskerMax instead of min and max when set', () => {
+ const data = [
+ datum({ min: 0, p25: 20, median: 50, mean: 55, p75: 80, max: 100, whiskerMin: 5, whiskerMax: 95 }),
+ ]
+ const scales = makeScales(['Mon'], [{ key: 'a', data }], 'stacked')
+ const box = computeBoxRect({
+ seriesKey: 'a',
+ label: 'Mon',
+ dataIndex: 0,
+ datum: data[0]!,
+ scales,
+ grouped: false,
+ })!
+ expect(box.whiskerTop).toBeCloseTo(scales.value(95), 5)
+ expect(box.whiskerBottom).toBeCloseTo(scales.value(5), 5)
+ })
+
it('uses the full band width when not grouped', () => {
const data = [datum()]
const scales = makeScales(['Mon'], [{ key: 'a', data }], 'stacked')
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.ts b/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.ts
index c1707bc8c46b..bea430c521ad 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.ts
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/computeBoxLayout.ts
@@ -62,8 +62,8 @@ export function computeBoxRect({
const p75Y = scales.value(datum.p75)
const medianY = scales.value(datum.median)
const meanY = scales.value(datum.mean)
- const maxY = scales.value(datum.max)
- const minY = scales.value(datum.min)
+ const maxY = scales.value(datum.whiskerMax ?? datum.max)
+ const minY = scales.value(datum.whiskerMin ?? datum.min)
if (!Number.isFinite(p25Y) || !Number.isFinite(p75Y) || !Number.isFinite(medianY) || !Number.isFinite(meanY)) {
return null
}
diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/types.ts b/packages/quill/packages/charts/src/charts/BoxPlot/types.ts
index edf56e05497f..f1ee7691360b 100644
--- a/packages/quill/packages/charts/src/charts/BoxPlot/types.ts
+++ b/packages/quill/packages/charts/src/charts/BoxPlot/types.ts
@@ -7,6 +7,9 @@ type BoxPlotSummary = { min: number; p25: number; median: number; mean: number;
export type BoxPlotDatum = BoxPlotSummary & {
/** Optional identifier for this x position (e.g. ISO date). The chart treats it as opaque. */
day?: string
+ /** Clipped whisker ends, for example at 1.5 × IQR. They default to `min` and `max`. */
+ whiskerMin?: number
+ whiskerMax?: number
}
/** Series shape for the BoxPlot chart. Unlike the generic `Series` (whose `data` is
diff --git a/packages/quill/packages/charts/src/docs/chart-types.md b/packages/quill/packages/charts/src/docs/chart-types.md
index cc2f3a4af236..d90d40ae1777 100644
--- a/packages/quill/packages/charts/src/docs/chart-types.md
+++ b/packages/quill/packages/charts/src/docs/chart-types.md
@@ -98,6 +98,7 @@ It is a preset over a one-series stacked `BarChart` with `axisOrientation: 'hori
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.
+Whiskers are drawn at `min` and `max`. Set `whiskerMin` and `whiskerMax` on a datum to clip them (for example to 1.5 × IQR) while `min` and `max` stay the true extremes; the y-axis then spans the whiskers, and the default tooltip lists both.
The tooltip receives a `BoxPlotTooltipContext`; `BoxPlotTooltip` is the default.
Boxes have a light tint of the series color, a thick median line, and whiskers in the axis color. Set `config.boxCornerRadius: 0` for square corners.
diff --git a/products/product_analytics/frontend/insights/trends/BoxPlot/BoxPlotChart.tsx b/products/product_analytics/frontend/insights/trends/BoxPlot/BoxPlotChart.tsx
index 758296a425d2..7f973786e6c9 100644
--- a/products/product_analytics/frontend/insights/trends/BoxPlot/BoxPlotChart.tsx
+++ b/products/product_analytics/frontend/insights/trends/BoxPlot/BoxPlotChart.tsx
@@ -32,15 +32,16 @@ export function BoxPlotChart({ showPersonsModal = true, context }: ChartParams):
label: group.seriesLabel,
// Explicit color keeps boxes in sync with BoxPlotLegend / BoxPlotResultsTable.
color: getSeriesColor(group.seriesIndex),
- // Feed whisker bounds as min/max — quill draws whiskers at min/max, so this
- // preserves the exclude-outliers (±1.5·IQR) behaviour computed in the logic.
+ // min/max stay the true extremes; the whiskers are clipped to ±1.5·IQR when outliers are excluded.
data: group.rawData.map((raw, i) => ({
- min: group.data[i].whiskerMin,
+ min: raw.min,
p25: raw.p25,
median: raw.median,
mean: raw.mean,
p75: raw.p75,
- max: group.data[i].whiskerMax,
+ max: raw.max,
+ whiskerMin: group.data[i].whiskerMin,
+ whiskerMax: group.data[i].whiskerMax,
day: raw.day,
})),
})),