: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): keep duration axis ticks distinct below a minute (#85356)
Sam Penningtonec0d69443dc1
diff --git a/frontend/src/lib/utils/durations.ts b/frontend/src/lib/utils/durations.tsindex 2f5b314886eb..33686a202890 100644--- a/frontend/src/lib/utils/durations.ts+++ b/frontend/src/lib/utils/durations.ts@@ -1,5 +1,5 @@import { dayjs } from 'lib/dayjs'-import { zeroPad } from 'lib/utils/numbers'+import { humanFriendlyNumber, zeroPad } from 'lib/utils/numbers'import { TimeUnitType } from '~/types'@@ -64,6 +64,26 @@ export function humanFriendlyDuration(return units.slice(0, maxUnits ?? undefined).join(' ')}+const threeSignificantDigits = (value: number): number =>+ Math.min(Math.max(0, 2 - Math.floor(Math.log10(Math.abs(value)))), 4)++/** Below a minute `humanFriendlyDuration` rounds to whole milliseconds or seconds, which collapses+ * neighbouring axis ticks onto one label — 3.5ms and 4ms both render as "4ms". */+export function formatDurationMilliseconds(milliseconds: number): string {+ const absolute = Math.abs(milliseconds)+ if (absolute === 0) {+ return '0s'+ }+ if (absolute < 1_000) {+ return `${humanFriendlyNumber(milliseconds, threeSignificantDigits(milliseconds))}ms`+ }+ const seconds = milliseconds / 1_000+ if (absolute < 60_000) {+ return `${humanFriendlyNumber(seconds, threeSignificantDigits(seconds))}s`+ }+ return humanFriendlyDuration(seconds)+}+export function humanFriendlyDiff(from: dayjs.Dayjs | string, to: dayjs.Dayjs | string): string {const diff = dayjs(to).diff(dayjs(from), 'seconds')return humanFriendlyDuration(diff)diff --git a/frontend/src/scenes/insights/aggregationAxisFormat.ts b/frontend/src/scenes/insights/aggregationAxisFormat.tsindex 7bea773d1354..f63fa7d1c0b2 100644--- a/frontend/src/scenes/insights/aggregationAxisFormat.ts+++ b/frontend/src/scenes/insights/aggregationAxisFormat.ts@@ -2,7 +2,7 @@ import posthog from 'posthog-js'import { LemonSelectOptionLeaf } from 'lib/lemon-ui/LemonSelect'import { formatCurrency } from 'lib/utils/currency'-import { humanFriendlyDuration } from 'lib/utils/durations'+import { formatDurationMilliseconds } from 'lib/utils/durations'import {compactNumber,humanFriendlyCurrency,@@ -65,7 +65,7 @@ const formatNanoseconds = (value: number): string => {if (absoluteValue < 1_000_000) {return `${humanFriendlyNumber(value / 1_000)}µs`}- return humanFriendlyDuration(value / 1_000_000_000, { secondsFixed: 1 })+ return formatDurationMilliseconds(value / 1_000_000)}// this function needs to support a trendsFilter as part of an insight query and@@ -98,10 +98,10 @@ export const formatAggregationAxisValue = (if (aggregationAxisFormat) {switch (aggregationAxisFormat) {case 'duration':- formattedValue = humanFriendlyDuration(value)+ formattedValue = formatDurationMilliseconds(value * 1000)breakcase 'duration_ms':- formattedValue = humanFriendlyDuration(value / 1000, { secondsFixed: 1 })+ formattedValue = formatDurationMilliseconds(value)breakcase 'duration_ns':formattedValue = formatNanoseconds(value)diff --git a/frontend/src/scenes/insights/aggregationAxisFormats.test.ts b/frontend/src/scenes/insights/aggregationAxisFormats.test.tsindex b64ed0e84c07..0422a629516c 100644--- a/frontend/src/scenes/insights/aggregationAxisFormats.test.ts+++ b/frontend/src/scenes/insights/aggregationAxisFormats.test.ts@@ -17,6 +17,7 @@ describe('formatAggregationAxisValue', () => {{ candidate: 500_000, filters: { aggregation_axis_format: 'duration_ns' }, expected: '500µs' },{ candidate: 1_000_000, filters: { aggregation_axis_format: 'duration_ns' }, expected: '1ms' },{ candidate: 1_500_000_000, filters: { aggregation_axis_format: 'duration_ns' }, expected: '1.5s' },+ { candidate: 3.5, filters: { aggregation_axis_format: 'duration_ms' }, expected: '3.5ms' },{ candidate: 3.944, filters: { aggregation_axis_format: 'percentage' }, expected: '3.94%' },{ candidate: 3.956, filters: { aggregation_axis_format: 'percentage' }, expected: '3.96%' },{ candidate: 3940, filters: { aggregation_axis_format: 'percentage' }, expected: '3,940%' },diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.mdindex 6f27c2336ad3..89ecfffa175c 100644--- a/packages/quill/packages/charts/AGENTS.md+++ b/packages/quill/packages/charts/AGENTS.md@@ -99,7 +99,7 @@ Safari is the exception: it never fires `contextrestored`, so a context lost the- The composable form of `MetricCard` lives one layer up as `Metric` in `@posthog/quill-components` (it needs `Card`/`Badge` from primitives, which charts can't depend on). It's built on `Sparkline` plus the headless metric helpers this package exports — `resolveDelta` / `computeFallbackChangePercent` (delta math), `useAnimatedNumber` (count-up headline), `useHoverIntent` (hover dwell) — and the `MetricChange` / `ResolvedDelta` types. Reach for `Metric` when you need a custom layout; use the monolithic `MetricCard` here for the default self-contained tile.- `TimeSeriesComboChart` wraps `ComboChart` the way `TimeSeriesLineChart`/`TimeSeriesBarChart` wrap their base charts: `config.xAxis`/`config.yAxis` (date tick formatter, y-format, scale, grid), `config.goalLines` (off-scale targets stretch the value axis via `ComboChart`'s `valueDomain`), `config.legend` (click-to-toggle), and `config.valueLabels`. Per-series `type` (`'bar' | 'line' | 'area'`) drives rendering; `barLayout` (`'stacked' | 'grouped' | 'percent'`), `divergingStack` (stacked only — negative bar values stack below the zero baseline instead of being clamped to 0, same semantics as `BarsConfig.divergingStack`), and right-axis series (`yAxisId: 'right'`) carry over from `ComboChart`. `ComboChart` itself now also honors `valueDomain` (primary axis only) and `showAxisLines`.- `TimeSeriesBarChart` also takes `config.valueDomain` (same `ValueDomain` semantics as `BarChartConfig.bars.valueDomain`): setting both `min` and `max` skips `d3.nice()` and wins over the goal-line stretch — pin `{ min: 0, max: dataMax }` so the tallest bar reaches the plot top on an axis-less volume chart, where nice-rounded headroom is invisible waste; `include` merges with the goal-line stretch (`mergeValueDomains` in `utils/goal-lines.ts`).-- y-axis `format`: `numeric | short | percentage | percentage_scaled | currency | duration | duration_ms | duration_ns`, plus `prefix`/`suffix`. Numeric and percentage ticks default to two decimals but gain a decimal per leading zero below 0.1, so a small-valued axis (say latency in seconds over 0–0.012) doesn't render every tick as `0.01`; `decimalPlaces` pins the precision instead.+- y-axis `format`: `numeric | short | percentage | percentage_scaled | currency | duration | duration_ms | duration_ns`, plus `prefix`/`suffix`. Numeric and percentage ticks default to two decimals but gain a decimal per leading zero below 0.1, so a small-valued axis (say latency in seconds over 0–0.012) doesn't render every tick as `0.01`; `decimalPlaces` pins the precision instead. The duration formats keep three significant digits below a minute for the same reason, then switch to the `1m 30s` unit breakdown.- Fixed x-axis tick rotation: set `xAxis.tickLabelRotation` on a time-series chart (`-45` tilts labels left). Values are clamped to `-90..90`; omit it to keep horizontal labels and existing collision behavior.- y-axis baseline: defaults to clamping a non-negative axis down to 0 (matching the classic insight axis). To float the axis to its data range instead — zooming in on the variation — set `yAxis.startAtZero: false` on `TimeSeriesLineChart`, or `config.floatBaseline: true` on the lower-level `LineChart`. Ignored on a log scale (no zero baseline) and applied to the primary axis only. Bar charts always draw from a zero baseline, so the flag is a no-op there.- y-axis min/max: `yAxis.min` / `yAxis.max` pin either end of the primary value axis on `TimeSeriesLineChart` (`config.valueDomain: { min, max }` on the lower-level `LineChart` — the same option that carries `include`, since a capped axis still has to stretch to reach an off-scale goal line; `mergeValueDomains` combines them field by field). How many ends are set decides the behavior: **both** pins the axis (skips `nice()`, discards `include`, overrides percent layout), **one** clamps that end and leaves the other automatic. A single clamp composes with auto-scaling — it runs after `include` folding, the zero-baseline clamp and `nice()`, so only the extreme label is the raw bound while interior ticks stay round — and is ignored under a percent layout, where the domain is already 0..1. On a log scale a non-positive bound is dropped. A non-finite bound counts as unset, so `{ min: 0, max: Math.max(...[]) }` floors at zero rather than collapsing. An inverted `min >= max` pair falls back to the automatic domain rather than swapping. Overlays positioned off the value scale hide themselves outside the bounded window rather than painting over the chrome: a goal line drops out, and so does the value label of a clipped point (a point sitting exactly on the bound keeps its label). Bar and combo charts deliberately ignore `min`/`max`, even though they take the same `yAxis` config: a bar encodes magnitude as length from zero, so a bounded baseline misreads it — 10 vs 11 would draw as 1 vs 2. Pin `valueDomain` there instead, which rescales the bars rather than truncating them.diff --git a/packages/quill/packages/charts/src/utils/format.ts b/packages/quill/packages/charts/src/utils/format.tsindex 88f5710c5ec5..e155844cd8a5 100644--- a/packages/quill/packages/charts/src/utils/format.ts+++ b/packages/quill/packages/charts/src/utils/format.ts@@ -102,6 +102,26 @@ export function humanFriendlyDuration(return units.slice(0, maxUnits ?? undefined).join(' ')}+const threeSignificantDigits = (value: number): number =>+ Math.min(Math.max(0, 2 - Math.floor(Math.log10(Math.abs(value)))), 4)++/** Below a minute `humanFriendlyDuration` rounds to whole milliseconds or seconds, which collapses+ * neighbouring axis ticks onto one label — 3.5ms and 4ms both render as "4ms". */+export function formatDurationMilliseconds(milliseconds: number): string {+ const absolute = Math.abs(milliseconds)+ if (absolute === 0) {+ return '0s'+ }+ if (absolute < 1_000) {+ return `${humanFriendlyNumber(milliseconds, threeSignificantDigits(milliseconds))}ms`+ }+ const seconds = milliseconds / 1_000+ if (absolute < 60_000) {+ return `${humanFriendlyNumber(seconds, threeSignificantDigits(seconds))}s`+ }+ return humanFriendlyDuration(seconds)+}+export function percentage(division: number,maximumFractionDigits: number = DEFAULT_DECIMAL_PLACES,diff --git a/packages/quill/packages/charts/src/utils/y-formatters.test.ts b/packages/quill/packages/charts/src/utils/y-formatters.test.tsindex 1ffaeb30781d..6a44e152ddfa 100644--- a/packages/quill/packages/charts/src/utils/y-formatters.test.ts+++ b/packages/quill/packages/charts/src/utils/y-formatters.test.ts@@ -9,6 +9,8 @@ describe('buildYTickFormatter', () => {['percentage_scaled', { format: 'percentage_scaled' as const }, 0.5, '50%'],['duration', { format: 'duration' as const }, 90, `1m${NBSP}30s`],['duration_ms', { format: 'duration_ms' as const }, 1500, '1.5s'],+ ['duration_ms above a minute', { format: 'duration_ms' as const }, 90_000, `1m${NBSP}30s`],+ ['duration_ms keeps whole milliseconds whole', { format: 'duration_ms' as const }, 556, '556ms'],['duration_ns below one microsecond', { format: 'duration_ns' as const }, 500, '500ns'],['duration_ns below one millisecond', { format: 'duration_ns' as const }, 500_000, '500µs'],['duration_ns', { format: 'duration_ns' as const }, 1_500_000_000, '1.5s'],@@ -47,6 +49,13 @@ describe('buildYTickFormatter', () => {},{ format: 'percentage', ticks: [0.005, 0.01], expected: ['0.005%', '0.01%'] },{ format: 'percentage_scaled', ticks: [0.00005, 0.0001], expected: ['0.005%', '0.01%'] },+ {+ format: 'duration_ms',+ ticks: [3, 3.5, 4, 4.5, 5],+ expected: ['3ms', '3.5ms', '4ms', '4.5ms', '5ms'],+ },+ { format: 'duration', ticks: [1.2, 1.4, 1.6], expected: ['1.2s', '1.4s', '1.6s'] },+ { format: 'duration_ns', ticks: [1_200_000, 1_400_000], expected: ['1.2ms', '1.4ms'] },]it.each(smallTickCases)('keeps small $format ticks distinct', ({ format, ticks, expected }) => {diff --git a/packages/quill/packages/charts/src/utils/y-formatters.ts b/packages/quill/packages/charts/src/utils/y-formatters.tsindex 093f906e834c..134f7fa4a3ee 100644--- a/packages/quill/packages/charts/src/utils/y-formatters.ts+++ b/packages/quill/packages/charts/src/utils/y-formatters.ts@@ -1,8 +1,8 @@import {compactNumber,formatCurrency,+ formatDurationMilliseconds,humanFriendlyCurrency,- humanFriendlyDuration,humanFriendlyNumber,percentage,significantDecimalPlaces,@@ -40,7 +40,7 @@ function formatNanoseconds(value: number): string {if (absoluteValue < 1_000_000) {return `${humanFriendlyNumber(value / 1_000)}µs`}- return humanFriendlyDuration(value / 1_000_000_000, { secondsFixed: 1 })+ return formatDurationMilliseconds(value / 1_000_000)}export function buildYTickFormatter(config: YFormatterConfig): (value: number) => string {@@ -53,10 +53,10 @@ export function buildYTickFormatter(config: YFormatterConfig): (value: number) =)switch (format) {case 'duration':- formatted = humanFriendlyDuration(value)+ formatted = formatDurationMilliseconds(value * 1000)breakcase 'duration_ms':- formatted = humanFriendlyDuration(value / 1000, { secondsFixed: 1 })+ formatted = formatDurationMilliseconds(value)breakcase 'duration_ns':formatted = formatNanoseconds(value)