: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(charts): keep trend charts rendering when a dimension goes non-finite (#71511)
posthog[bot]af05a028080a
diff --git a/packages/quill/packages/charts/src/core/bar-scales.test.ts b/packages/quill/packages/charts/src/core/bar-scales.test.tsindex 9341fea5f4f6..6e56604c5215 100644--- a/packages/quill/packages/charts/src/core/bar-scales.test.ts+++ b/packages/quill/packages/charts/src/core/bar-scales.test.ts@@ -234,6 +234,22 @@ describe('hog-charts bar scales', () => {const { value } = createBarScales(series, ['a', 'b', 'c'], dimensions, { barLayout, valueDomain })expect(value.domain()).toEqual(valueDomain)})++ // A non-finite or collapsed fixed domain maps every bar (and axis tick) to NaN, so the chart+ // paints nothing while x-only tooltips keep working — the same failure the line path guards+ // against via sanitizeFixedDomain. The bar value scale must stay well-formed too.+ it.each([+ { name: 'NaN bounds', valueDomain: [NaN, NaN] as [number, number] },+ { name: 'a NaN max (e.g. Math.max of empty data)', valueDomain: [0, NaN] as [number, number] },+ { name: 'an infinite max', valueDomain: [0, Infinity] as [number, number] },+ { name: 'collapsed bounds', valueDomain: [50, 50] as [number, number] },+ ])('keeps a finite, non-degenerate domain for $name', ({ valueDomain }) => {+ const series = [makeSeries({ key: 's1', data: [10, 20, 30] })]+ const [min, max] = createBarScales(series, ['a', 'b', 'c'], dimensions, { valueDomain }).value.domain()+ expect(isFinite(min)).toBe(true)+ expect(isFinite(max)).toBe(true)+ expect(min).toBeLessThan(max)+ })})describe('createBarScales — log scale', () => {diff --git a/packages/quill/packages/charts/src/core/canvas-size.test.ts b/packages/quill/packages/charts/src/core/canvas-size.test.tsindex 7d022e75ee36..0808937336c3 100644--- a/packages/quill/packages/charts/src/core/canvas-size.test.ts+++ b/packages/quill/packages/charts/src/core/canvas-size.test.ts@@ -89,6 +89,24 @@ describe('canvas-size', () => {plotHeight,})})++ // `Math.max(0, NaN)` is NaN, so a non-finite rect/margin used to poison the plot size — a NaN+ // pixel range maps every point and axis tick to NaN, blanking the chart while x-only tooltips+ // still fire. Only the non-finite dimension floors to 0; the finite one keeps its real size —+ // pinning both catches a blanket floor-to-0 that would clobber a valid dimension yet still be+ // finite and non-negative. DEFAULT_MARGINS is left 48 / right 16 / top 16 / bottom 32.+ it.each([+ { name: 'a NaN height', rect: { width: 600, height: NaN }, plotWidth: 536, plotHeight: 0 },+ { name: 'an infinite height', rect: { width: 600, height: Infinity }, plotWidth: 536, plotHeight: 0 },+ { name: 'a NaN width', rect: { width: NaN, height: 400 }, plotWidth: 0, plotHeight: 352 },+ ])(+ 'floors only the non-finite dimension, keeping the finite one, for $name',+ ({ rect, plotWidth, plotHeight }) => {+ const d = buildDimensions(rect, DEFAULT_MARGINS)+ expect(d.plotWidth).toBe(plotWidth)+ expect(d.plotHeight).toBe(plotHeight)+ }+ )})describe('sameDimensions', () => {diff --git a/packages/quill/packages/charts/src/core/canvas-size.ts b/packages/quill/packages/charts/src/core/canvas-size.tsindex 31114b5d8dc8..c133c6d848f7 100644--- a/packages/quill/packages/charts/src/core/canvas-size.ts+++ b/packages/quill/packages/charts/src/core/canvas-size.ts@@ -48,14 +48,22 @@ export function syncCanvasSize(canvas: HTMLCanvasElement, rect: SizeRect, dpr: nreturn resized}+// `Math.max(0, NaN)` is `NaN`, not `0` — so a single non-finite input (an unmeasured container, a+// NaN margin) would silently poison the plot size. A NaN `plotWidth`/`plotHeight` flows straight+// into the value-scale pixel range, mapping every point and axis tick to NaN: the chart paints+// nothing while x-only pointer hit-testing still fires tooltips. Floor to a finite, non-negative size.+function finitePlotSize(size: number): number {+ return isFinite(size) && size > 0 ? size : 0+}+export function buildDimensions(rect: SizeRect, margins: ChartMargins): ChartDimensions {return {width: rect.width,height: rect.height,plotLeft: margins.left,plotTop: margins.top,- plotWidth: Math.max(0, rect.width - margins.left - margins.right),- plotHeight: Math.max(0, rect.height - margins.top - margins.bottom),+ plotWidth: finitePlotSize(rect.width - margins.left - margins.right),+ plotHeight: finitePlotSize(rect.height - margins.top - margins.bottom),}}diff --git a/packages/quill/packages/charts/src/core/scales.test.ts b/packages/quill/packages/charts/src/core/scales.test.tsindex 973724bcc479..4dd1a6096a29 100644--- a/packages/quill/packages/charts/src/core/scales.test.ts+++ b/packages/quill/packages/charts/src/core/scales.test.ts@@ -11,6 +11,7 @@ import {createXScale,createYScale,niceLogDomain,+ sanitizeFixedDomain,yTickCountForHeight,} from './scales'import type { StackedBand } from './scales'@@ -246,6 +247,21 @@ describe('hog-charts scales', () => {const scale = createYScale(series, dimensions, { percentStack: true, valueDomain: [0, 200] })expect(scale.domain()).toEqual([0, 200])})++ // A non-finite or collapsed fixed domain maps every value (and axis tick) to NaN, so the+ // chart paints nothing while x-only tooltips keep working. The domain must stay well-formed.+ it.each([+ { name: 'NaN bounds', valueDomain: [NaN, NaN] as [number, number] },+ { name: 'a NaN max (e.g. Math.max of empty data)', valueDomain: [0, NaN] as [number, number] },+ { name: 'an infinite max', valueDomain: [0, Infinity] as [number, number] },+ { name: 'collapsed bounds', valueDomain: [50, 50] as [number, number] },+ ])('keeps a finite, non-degenerate domain for $name', ({ valueDomain }) => {+ const series = [makeSeries({ key: 's1', data: [10, 20, 30] })]+ const [min, max] = createYScale(series, dimensions, { valueDomain }).domain()+ expect(isFinite(min)).toBe(true)+ expect(isFinite(max)).toBe(true)+ expect(min).toBeLessThan(max)+ })})describe('createYScale — log mode', () => {@@ -664,11 +680,29 @@ describe('hog-charts scales', () => {{ plotHeight: 400, expected: 8 },{ plotHeight: 550, expected: 11 },{ plotHeight: 1600, expected: 11 },+ // A non-finite/non-positive height must floor to the minimum: `ticks(NaN)` returns [],+ // which would strip every tick off the axis.+ { plotHeight: NaN, expected: 2 },+ { plotHeight: -Infinity, expected: 2 },+ { plotHeight: -10, expected: 2 },])('plotHeight $plotHeight → $expected ticks', ({ plotHeight, expected }) => {expect(yTickCountForHeight(plotHeight)).toBe(expected)})})+ describe('sanitizeFixedDomain', () => {+ it.each([+ { name: 'NaN bounds fall back to [0, 1]', input: [NaN, NaN], expected: [0, 1] },+ { name: 'a NaN bound falls back to [0, 1]', input: [0, NaN], expected: [0, 1] },+ { name: 'an infinite bound falls back to [0, 1]', input: [0, Infinity], expected: [0, 1] },+ { name: 'collapsed bounds get a unit span', input: [5, 5], expected: [5, 6] },+ { name: 'an inverted order is normalized', input: [40, 0], expected: [0, 40] },+ { name: 'a well-formed domain is preserved', input: [0, 40], expected: [0, 40] },+ ])('$name', ({ input, expected }) => {+ expect(sanitizeFixedDomain(input as [number, number])).toEqual(expected)+ })+ })+describe('autoFormatYTick', () => {it.each([{ domainMax: 0, value: 0.5, expected: '0.50', label: 'two decimal places when domainMax < 2' },diff --git a/packages/quill/packages/charts/src/core/scales.ts b/packages/quill/packages/charts/src/core/scales.tsindex 587101e88020..64a7707a794d 100644--- a/packages/quill/packages/charts/src/core/scales.ts+++ b/packages/quill/packages/charts/src/core/scales.ts@@ -126,8 +126,41 @@ export function createXScale(labels: string[], dimensions: ChartDimensions): Sca.padding(0)}+/** Min/max y-axis tick counts. `MIN_Y_TICKS` doubles as the floor for a non-finite/non-positive+ * plot height, where d3's `ticks()` would otherwise return an empty array. */+const MIN_Y_TICKS = 2+const MAX_Y_TICKS = 11+export function yTickCountForHeight(plotHeight: number): number {- return Math.max(2, Math.min(11, Math.floor(plotHeight / 50)))+ // A non-finite or non-positive plot height (e.g. an unmeasured container) would make d3's+ // `ticks(count)` return `[]` — the axis then renders no ticks at all. Floor at the minimum.+ if (!isFinite(plotHeight) || plotHeight <= 0) {+ return MIN_Y_TICKS+ }+ return Math.max(MIN_Y_TICKS, Math.min(MAX_Y_TICKS, Math.floor(plotHeight / 50)))+}++/** Repair a fixed, caller-supplied `[min, max]` domain so it can't map every value to NaN. A+ * non-finite bound (e.g. a `Math.max(...[])` that yielded `-Infinity`, or a `0/0`) falls back to+ * `[0, 1]`; a collapsed `min === max` domain gets a unit span. Keeps the caller's finite bounds+ * otherwise, only normalizing an inverted order. */+export function sanitizeFixedDomain([min, max]: readonly [number, number]): [number, number] {+ if (!isFinite(min) || !isFinite(max)) {+ return [0, 1]+ }+ if (min === max) {+ return [min, min + 1]+ }+ return min < max ? [min, max] : [max, min]+}++/** Repair a degenerate (`min === max`) or non-finite value-scale extent so a linear domain can't+ * map every value to NaN: coerce non-finite bounds to 0, bracket zero, and guarantee a unit span.+ * Callers apply it only inside the degenerate/non-finite guard — bracketing a well-formed extent+ * toward zero would move the axis. */+function repairDegenerateExtent(min: number, max: number): [number, number] {+ const lo = Math.min(0, isFinite(min) ? min : 0)+ return [lo, Math.max(0, isFinite(max) ? max : 0, lo + 1)]}export function createYScale(@@ -148,7 +181,7 @@ export function createYScale(if (fixed) {return scaleLinear()- .domain([fixed[0], fixed[1]])+ .domain(sanitizeFixedDomain(fixed)).range([dimensions.plotTop + dimensions.plotHeight, dimensions.plotTop])}@@ -220,9 +253,10 @@ export function buildValueScale(options: {// bracket a degenerate `min === max` domain so it doesn't collapse to NaN.let logMin = minlet logMax = max- if (logMin === logMax) {- logMin = Math.min(0, logMin)- logMax = Math.max(0, logMax, logMin + 1)+ if (!isFinite(logMin) || !isFinite(logMax) || logMin === logMax) {+ const [lo, hi] = repairDegenerateExtent(logMin, logMax)+ logMin = lo+ logMax = hi}return scaleLinear().domain([logMin, logMax]).nice(tickCount).range(valueRange)}@@ -238,11 +272,14 @@ export function buildValueScale(options: {}// The range can collapse to a single point (e.g. all-equal data, or `include`-only goal values- // like `[100, 100]`) — a degenerate domain that maps everything to NaN. Bracket zero, then- // guarantee a unit span, so the axis stays well-formed.- if (min === max) {- min = Math.min(0, min)- max = Math.max(0, max, min + 1)+ // like `[100, 100]`) — a degenerate domain that maps everything to NaN. A non-finite extent+ // (defensive: `count > 0` should keep both bounds finite, but a stray NaN here blanks the whole+ // chart) is treated the same. Bracket zero, then guarantee a unit span, so the axis stays+ // well-formed.+ if (!isFinite(min) || !isFinite(max) || min === max) {+ const [lo, hi] = repairDegenerateExtent(min, max)+ min = lo+ max = hi}return scaleLinear().domain([min, max]).nice(tickCount).range(valueRange)@@ -685,7 +722,7 @@ function buildBarValueScale(): D3YScale {const { fixed, include } = resolveValueDomain(valueDomain)if (fixed) {- return scaleLinear().domain([fixed[0], fixed[1]]).range(valueRange)+ return scaleLinear().domain(sanitizeFixedDomain(fixed)).range(valueRange)}if (barLayout === 'percent') {return scaleLinear().domain([0, 1]).nice(tickCount).range(valueRange)@@ -696,14 +733,17 @@ function buildBarValueScale(if (range.count === 0) {return scaleLinear().domain([0, 1]).range(valueRange)}- const min = range.min > 0 ? 0 : range.min+ let min = range.min > 0 ? 0 : range.minlet max = range.max < 0 ? 0 : range.maxif (scaleType === 'log' && isFinite(range.minPositive)) {return scaleLog().domain(niceLogDomain(range.minPositive, max)).range(valueRange).clamp(true)}- // Guard the degenerate single-point domain (e.g. empty data with a single goal value at 0).- if (min === max) {- max = min + 1+ // Guard the degenerate single-point domain (e.g. empty data with a single goal value at 0), and+ // any non-finite extent, so the value scale never maps every bar to NaN.+ if (!isFinite(min) || !isFinite(max) || min === max) {+ const [lo, hi] = repairDegenerateExtent(min, max)+ min = lo+ max = hi}const scale = scaleLinear().domain([min, max]).nice(tickCount)return scale.range(padValueRange(valueRange, scale.domain() as [number, number], valuePadding))