: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): prevent log y-axis label overlap in quill charts (#65107)
Sam Pennington6be9a6c19b8d
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 4d51abe5d83d..b61132d6b582 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -917,9 +917,9 @@ snapshots:components-hogcharts-timeserieslinechart--dual-y-axis--light:hash: v1.k794b7964.1f138ad789223e20e6ce5a44a606eef88a656cc6e6f1a9b97509d41097924487.ahlyjBbCBI_xNK_wpzuGHu7ggQpeg9TEWbuK2JfankMcomponents-hogcharts-timeserieslinechart--dual-y-axis-mixed-scales--dark:- hash: v1.k794b7964.f707dca453892b302d4fb7d9de170e8fc06724d15b279058e7abb8ca2da86223.njpHnoZg1z2SZ9s-p-V14hb6U_GmOGvjJYjEqsAHVwg+ hash: v1.k794b7964.f71a4c1f6dda91d3288b84ca5898f1dd6989ef59232b2ab0c750fec183fb3ed1.xwJ30vBMNqFMIelTDoEcVniPPFuogpQDZabgIgBzstMcomponents-hogcharts-timeserieslinechart--dual-y-axis-mixed-scales--light:- hash: v1.k794b7964.beaba679099c68a2741f10fd75af0ce911c82fbb7bd6facd4991fabf21d0f70d.PCyv0DEoo5ZDgud3iDg7VCZlyy1-3P7KVM6YIuqGkdw+ hash: v1.k794b7964.f8686c7d995901bfa19f6a6bc90eff91470a6a7d01a2605b1cdec3e63e5430db.Onr7JhLfsgT3GChJBP11H_cF_IvqZ2bJJ5TtcKySxOIcomponents-hogcharts-timeserieslinechart--moving-average--dark:hash: v1.k794b7964.e332e9beb6b72bee51ab22b8078273883dabe8cd97163f6005a44367378fa8f7.H_JLql_NFDi4zX-Gn0svc0N_NewsyAhqaII4UGIXykgcomponents-hogcharts-timeserieslinechart--moving-average--light:diff --git a/packages/quill/packages/charts/src/overlays/AxisLabels.test.ts b/packages/quill/packages/charts/src/overlays/AxisLabels.test.tsindex 6d05219fdca6..4a1436e791a0 100644--- a/packages/quill/packages/charts/src/overlays/AxisLabels.test.ts+++ b/packages/quill/packages/charts/src/overlays/AxisLabels.test.ts@@ -1,5 +1,7 @@+import { scaleLog } from 'd3-scale'+import { measureLabelWidth } from '../utils/text-measure'-import { computeVisibleValueTicks, computeVisibleXLabels } from './AxisLabels'+import { computeVisibleValueTicks, computeVisibleXLabels, computeVisibleYTicks } from './AxisLabels'describe('computeVisibleXLabels', () => {const longUrl = 'https://app.posthog.com/project/1/insights/abc123/edit?with=a&very=long&query=string'@@ -93,3 +95,46 @@ describe('computeVisibleValueTicks', () => {expect(visible.map((v) => v.tick)).toEqual(expected)})})++describe('computeVisibleYTicks', () => {+ it('keeps every linear tick when comfortably spaced', () => {+ const ticks = [0, 25, 50, 75, 100]+ // 50px apart — far beyond the ~16px overlap threshold.+ const valueToCoord = (v: number): number => 250 - (v / 100) * 200++ expect(computeVisibleYTicks(ticks, valueToCoord)).toEqual(ticks)+ })++ it('thins an overcrowded log axis down to non-overlapping labels, preferring round values', () => {+ // d3 log ticks for a 1→1000 domain: 1..9, 10..90, 100..900, 1000 — far too many to label+ // in a 300px gutter without overlap.+ const scale = scaleLog().domain([1, 1000]).range([300, 0])+ const ticks = scale.ticks() as number[]++ const visible = computeVisibleYTicks(ticks, (v) => scale(v))++ // No two surviving labels sit closer than the overlap threshold.+ const coords = visible.map((t) => scale(t)).sort((a, b) => a - b)+ for (let i = 1; i < coords.length; i++) {+ expect(coords[i] - coords[i - 1]).toBeGreaterThanOrEqual(16)+ }+ // The powers of ten — the roundest values — all survive.+ expect(visible).toEqual(expect.arrayContaining([1, 10, 100, 1000]))+ // Ascending order is preserved.+ expect([...visible].sort((a, b) => a - b)).toEqual(visible)+ })++ it('prefers a power of ten over an adjacent sub-decade tick when only one fits', () => {+ // 90 and 100 map ~5px apart — too close to both label; the rounder 100 must win.+ const valueToCoord = (v: number): number => (v === 90 ? 105 : v === 100 ? 100 : 300 - v)+ const visible = computeVisibleYTicks([90, 100], valueToCoord)++ expect(visible).toEqual([100])+ })++ it('drops ticks whose coordinate is not finite', () => {+ const valueToCoord = (v: number): number => (v === 50 ? NaN : 200 - v)++ expect(computeVisibleYTicks([0, 50, 100], valueToCoord)).toEqual([0, 100])+ })+})diff --git a/packages/quill/packages/charts/src/overlays/AxisLabels.tsx b/packages/quill/packages/charts/src/overlays/AxisLabels.tsxindex c7d5b42f0212..7f1ee397edf8 100644--- a/packages/quill/packages/charts/src/overlays/AxisLabels.tsx+++ b/packages/quill/packages/charts/src/overlays/AxisLabels.tsx@@ -25,6 +25,12 @@ interface AxisLabelsProps {const CATEGORY_LABEL_PADDING = 20const VALUE_TICK_LABEL_PADDING = 8+// Minimum center-to-center spacing (px) between stacked y-axis tick labels. A 12px font renders a+// box ~14px tall, so anything closer than this overlaps. Used to thin a crowded value axis — most+// notably a log scale, whose `.ticks()` emits sub-decade values (…8, 9, 20, 30…) that pack far+// tighter than a linear axis's `.nice()`-bounded ticks ever do.+const Y_TICK_LABEL_MIN_GAP = 16+interface XLabelCandidate {index: number/** Display text — truncated to `maxCategoryLabelWidth` when one is set. */@@ -121,6 +127,55 @@ export function computeVisibleValueTicks(return dropOverlappingLabels(candidates, VALUE_TICK_LABEL_PADDING)}+// Roundness rank for a value, lowest = most preferred to keep as a label: a power of ten beats a+// 5×10ⁿ, which beats a 2×10ⁿ, which beats everything else. Drives which labels survive when a log+// axis is too crowded to show them all — yielding the classic 1-2-5 decade labelling.+function tickRoundness(value: number): number {+ if (value === 0) {+ return 0+ }+ const abs = Math.abs(value)+ const mantissa = abs / Math.pow(10, Math.floor(Math.log10(abs)))+ const rounded = Math.round(mantissa)+ if (rounded === 1 || rounded === 10) {+ return 0+ }+ if (rounded === 5) {+ return 1+ }+ if (rounded === 2) {+ return 2+ }+ return 3+}++/** Thin value-axis ticks whose stacked labels would overlap vertically, keeping the roundest values+ * (powers of ten, then 5s, then 2s) so a crowded axis — chiefly a log scale — reads as clean 1-2-5+ * decade labels instead of an unreadable smear. Linear axes are `.nice()`-bounded to comfortably+ * spaced ticks, so every one clears the gap and all are kept unchanged. Returns ticks in ascending+ * value order, matching the input. */+export function computeVisibleYTicks(+ ticks: number[],+ valueToCoord: (value: number) => number,+ minGap = Y_TICK_LABEL_MIN_GAP+): number[] {+ const candidates = ticks.map((tick) => ({ tick, y: valueToCoord(tick) })).filter(({ y }) => isFinite(y))+ if (candidates.length <= 1) {+ return candidates.map((c) => c.tick)+ }++ // Offer the roundest ticks first; ties broken by position so the greedy fills the axis evenly.+ const ordered = [...candidates].sort((a, b) => tickRoundness(a.tick) - tickRoundness(b.tick) || a.y - b.y)+ const kept: { tick: number; y: number }[] = []+ for (const candidate of ordered) {+ if (kept.every((k) => Math.abs(k.y - candidate.y) >= minGap)) {+ kept.push(candidate)+ }+ }++ return kept.sort((a, b) => a.tick - b.tick).map((c) => c.tick)+}+const TICK_STYLE_BASE: React.CSSProperties = {position: 'absolute',fontSize: 12,@@ -286,7 +341,7 @@ export const AxisLabels = React.memo(function AxisLabels({return (<>{yGutters.flatMap((gutter) =>- gutter.ticks.map((tick: number) => {+ computeVisibleYTicks(gutter.ticks, gutter.scale).map((tick: number) => {const y = gutter.scale(tick)if (!isFinite(y)) {return null