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): bridge missing values with connectNulls (#127293)

Sam Penningtonbb1e0c2b1048
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index f58720b189d6..51d38e2275e5 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -1565,9 +1565,9 @@ snapshots:
components-hogcharts-timeserieslinechart--start-at-zero--light:
hash: v1.k794b7964.c8d69212e98a1690b10aa0452f9addf1afbaa2c5d8430f29928905874542cbcf.2bGtOgJFy_0JdlDEo5wQj6jVRwzQDbPP-hRp5bR7vro
components-hogcharts-timeserieslinechart--time-scale--dark:
- hash: v1.k794b7964.00797dd065019207fce650c0094f08a735062e4cb3c8693b3d479f0f8d69617f.9cQxUXU3P6ThTL4tnvjBp8ccQj16c3M3hhBORK0FlEo
+ hash: v1.k794b7964.c46ad0c4046b940466189423a71e8a7beb2b47ce0c8b8f85c31dca846995debf.OaiYFvNRK8vM3R2Ko7Aw8VvFLVj0riFrNL07ZmojQiU
components-hogcharts-timeserieslinechart--time-scale--light:
- hash: v1.k794b7964.555c5ae439c1546c05f25839b6e7d19e5eca1a4e31a6ee2aaaee375ead906ee9.8Efnz1p05sBd3J7iRi-GQ1erSEq86cJJCpyHjIm0-sc
+ hash: v1.k794b7964.dc5250b707f9b13cfc18865894bfca34a62c3f10fe2ce2ab4cfd2bdcb68745a4._qkh-6RuC2H73_o9XWzCN5rnlmDuVhsjIv5olJWhRXg
components-hogcharts-timeserieslinechart--trend-lines--dark:
hash: v1.k794b7964.4d5a8d344fd57bb3d23edccf5bbcd73a293820083e5ed9f2c25f5bc439dba3ed.am33OiZY-lTm4WRE_NQKTyorQpZqkkA-f_sX1Hx46hA
components-hogcharts-timeserieslinechart--trend-lines--light:
diff --git a/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsx b/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsx
index a2e64286d238..f26e6d19bd4b 100644
--- a/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsx
+++ b/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsx
@@ -100,6 +100,7 @@ function LineChartInner<Meta = unknown>({
yAxes,
curve,
timeScale,
+ connectNulls = false,
} = config ?? {}
const smooth = curve === 'monotone'
// Resolve to primitives so an inline `{ x, y }` config object can't churn the draw callbacks.
@@ -228,6 +229,7 @@ function LineChartInner<Meta = unknown>({
// stroke at the y-axis, instead of straddling either axis line.
yFloor: axisLines.x ? dimensions.plotTop + dimensions.plotHeight - LINE_STROKE_WIDTH / 2 : undefined,
clipLeftEdge: axisLines.y,
+ connectNulls,
})
if (axisLineStyle) {
@@ -240,7 +242,7 @@ function LineChartInner<Meta = unknown>({
})
}
},
- [showGrid, axisLines, stackedData, smooth]
+ [showGrid, axisLines, stackedData, smooth, connectNulls]
)
const drawHover = useCallback(
diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.stories.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.stories.tsx
index 4a6404adbbe6..5ad12ef9c9de 100644
--- a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.stories.tsx
+++ b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.stories.tsx
@@ -384,7 +384,7 @@ const SPARSE_SERIES: Series[] = [
{
key: 'rps',
label: 'Requests',
- data: SPARSE_HOURS.map((h) => 200 + 60 * Math.sin(h / 3)),
+ data: SPARSE_HOURS.map((h) => (h === 15 ? NaN : 200 + 60 * Math.sin(h / 3))),
fill: { opacity: 0.2 },
},
]
@@ -397,10 +397,11 @@ export const TimeScale: Story = {
{ title: 'index (default)', config: {} },
{ title: "xScale: 'time'", config: { xScale: 'time' } },
{ title: "xScale: 'time', xDomain: whole day", config: { xScale: 'time', xDomain: WHOLE_DAY } },
+ { title: "xScale: 'time', connectNulls", config: { xScale: 'time', connectNulls: true } },
]
return (
// eslint-disable-next-line react/forbid-dom-props
- <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, auto)', gap: 24 }}>
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(2, auto)', gap: 24 }}>
{cells.map(({ title, config }) => (
<div key={title}>
<div>{title}</div>
diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx
index e13fe0f4c437..6ca663649088 100644
--- a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx
+++ b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx
@@ -65,6 +65,8 @@ export interface TimeSeriesLineChartConfig {
xScale?: 'index' | 'time'
/** With `xScale: 'time'`, the axis span as `[from, to]` epoch ms. Defaults to the label range. */
xDomain?: readonly [number, number]
+ /** Draw lines and areas across missing (non-finite) values instead of breaking them. */
+ connectNulls?: boolean
/** Tooltip behaviour and built-in content options. A `tooltip` render prop replaces the content. */
tooltip?: TooltipConfig
/** Built-in legend with click-to-toggle series visibility. Hidden by default. */
@@ -125,6 +127,7 @@ export function TimeSeriesLineChart<Meta = unknown>({
curve,
xScale,
xDomain,
+ connectNulls,
tooltip: tooltipConfig,
legend,
} = config ?? {}
@@ -179,6 +182,7 @@ export function TimeSeriesLineChart<Meta = unknown>({
showTickMarks,
curve,
timeScale,
+ connectNulls,
percentStackView,
showCrosshair,
tooltip: timeSeriesTooltipConfig,
diff --git a/packages/quill/packages/charts/src/core/canvas-renderer.test.ts b/packages/quill/packages/charts/src/core/canvas-renderer.test.ts
index 3d4c8b4b3b3e..d9121c396c34 100644
--- a/packages/quill/packages/charts/src/core/canvas-renderer.test.ts
+++ b/packages/quill/packages/charts/src/core/canvas-renderer.test.ts
@@ -121,12 +121,22 @@ describe('hog-charts canvas-renderer', () => {
expectedMoveTo: 0,
expectedLineTo: 0,
},
- ])('$name', ({ labels, data, gapValues, expectedBeginPath, expectedMoveTo, expectedLineTo }) => {
+ {
+ name: 'bridges the gap with connectNulls',
+ labels: ['a', 'b', 'c'],
+ data: [10, 50, 90],
+ gapValues: new Set([50]),
+ connectNulls: true,
+ expectedBeginPath: 1,
+ expectedMoveTo: 1,
+ expectedLineTo: 1,
+ },
+ ])('$name', ({ labels, data, gapValues, connectNulls, expectedBeginPath, expectedMoveTo, expectedLineTo }) => {
const ctx = mockCanvasContext()
const series = makeSeries({ key: 's1', data })
const drawCtx =
gapValues.size > 0 ? makeDrawContextWithGaps(ctx, labels, gapValues) : makeDrawContext(ctx, labels)
- drawLine(drawCtx, series)
+ drawLine({ ...drawCtx, connectNulls }, series)
expect(ctx.beginPath).toHaveBeenCalledTimes(expectedBeginPath)
expect(ctx.moveTo).toHaveBeenCalledTimes(expectedMoveTo)
expect(ctx.lineTo).toHaveBeenCalledTimes(expectedLineTo)
@@ -167,10 +177,18 @@ describe('hog-charts canvas-renderer', () => {
arcs: [5, 5],
},
{ name: 'draws no dot when the radius is explicitly zero', labels: ['a'], data: [10], radius: 0, arcs: [] },
- ])('$name', ({ labels, data, radius, arcs }) => {
+ {
+ name: 'draws no dots for points bridged by connectNulls',
+ labels: ['a', 'b', 'c'],
+ data: [10, 50, 90],
+ radius: undefined,
+ connectNulls: true,
+ arcs: [],
+ },
+ ])('$name', ({ labels, data, radius, connectNulls, arcs }) => {
const ctx = mockCanvasContext()
const series = makeSeries({ key: 's1', data, points: radius === undefined ? undefined : { radius } })
- drawPoints(makeDrawContextWithGaps(ctx, labels, new Set([50])), series)
+ drawPoints({ ...makeDrawContextWithGaps(ctx, labels, new Set([50])), connectNulls }, series)
expect(ctx.arc.mock.calls.map((call) => call[2])).toEqual(arcs)
})
})
@@ -408,6 +426,19 @@ describe('hog-charts canvas-renderer', () => {
expect(ctx.lineTo).toHaveBeenCalledTimes(4)
})
+ it.each([
+ { smooth: false, segment: 'lineTo' as const, partial: { fromIndex: 2 } },
+ { smooth: true, segment: 'bezierCurveTo' as const, partial: { fromIndex: 2 } },
+ { smooth: false, segment: 'lineTo' as const, partial: { toIndex: 1 } },
+ ])('dashes a bridged gap on the dash boundary ($partial, smooth: $smooth)', ({ smooth, segment, partial }) => {
+ const ctx = mockCanvasContext()
+ const series = makeSeries({ key: 's1', data: [10, 50, 30], stroke: { partial } })
+ const drawCtx = makeDrawContextWithGaps(ctx, ['a', 'b', 'c'], new Set([50]))
+ drawLine({ ...drawCtx, smooth, connectNulls: true }, series)
+ expect(ctx[segment]).toHaveBeenCalledTimes(1)
+ expect(dashCalls(ctx)).toEqual([[10, 10], []])
+ })
+
it('applies partial dashing against the yValues override length, not series.data', () => {
const ctx = mockCanvasContext()
const series = makeSeries({
@@ -732,12 +763,20 @@ describe('hog-charts canvas-renderer', () => {
gapValues: new Set([999, 998]),
expectedFillCount: 0,
},
- ])('$name', ({ labels, data, gapValues, expectedFillCount }) => {
+ {
+ name: 'fills one segment across a gap with connectNulls',
+ labels: ['a', 'b', 'c', 'd', 'e'],
+ data: [10, 20, 999, 80, 90],
+ gapValues: new Set([999]),
+ connectNulls: true,
+ expectedFillCount: 1,
+ },
+ ])('$name', ({ labels, data, gapValues, connectNulls, expectedFillCount }) => {
const ctx = mockCanvasContext()
const series = makeSeries({ key: 's1', data })
const drawCtx =
gapValues.size > 0 ? makeDrawContextWithGaps(ctx, labels, gapValues) : makeDrawContext(ctx, labels)
- drawArea(drawCtx, series)
+ drawArea({ ...drawCtx, connectNulls }, series)
expect(ctx.fill).toHaveBeenCalledTimes(expectedFillCount)
})
})
diff --git a/packages/quill/packages/charts/src/core/canvas-renderer.ts b/packages/quill/packages/charts/src/core/canvas-renderer.ts
index 80d5f04d21ca..5e3cf42f960c 100644
--- a/packages/quill/packages/charts/src/core/canvas-renderer.ts
+++ b/packages/quill/packages/charts/src/core/canvas-renderer.ts
@@ -25,6 +25,12 @@ export interface DrawContext {
* baseline minus half their stroke width, so a baseline-hugging stroke rests exactly on the
* axis line instead of straddling it. Pure draw-time clamp — scales and ticks are untouched. */
yFloor?: number
+ /** Draw across missing values instead of breaking the line and area at them. */
+ connectNulls?: boolean
+}
+
+function isDrawablePoint({ xScale, yScale, labels }: DrawContext, data: number[], i: number): boolean {
+ return xScale(labels[i]) != null && isFinite(yScale(data[i]))
}
/** Stroke width of series lines. Half of it is the stroke's overhang past a point, which the
@@ -51,7 +57,8 @@ export function drawLine(drawCtx: DrawContext, series: ResolvedSeries, yValues?:
return
}
- for (const { start, end, pattern } of planLineStrokes(series, data.length)) {
+ const isDrawable = drawCtx.connectNulls ? (i: number) => isDrawablePoint(drawCtx, data, i) : undefined
+ for (const { start, end, pattern } of planLineStrokes(series, data.length, isDrawable)) {
ctx.beginPath()
ctx.setLineDash(pattern)
tracePath(drawCtx, data, start, end)
@@ -201,11 +208,18 @@ interface Stroke {
* Each entry is a contiguous index range drawn with a single dash pattern; adjacent strokes
* share their boundary index so the visual seam between them is invisible.
*/
-function planLineStrokes(series: ResolvedSeries, length: number): Stroke[] {
+function planLineStrokes(series: ResolvedSeries, length: number, isDrawable?: (i: number) => boolean): Stroke[] {
const basePattern = series.stroke?.pattern ?? []
const partialPattern = series.stroke?.partial?.pattern ?? [10, 10]
- const from = resolvePartialIndex(series.stroke?.partial?.fromIndex, length)
- const to = resolvePartialIndex(series.stroke?.partial?.toIndex, length)
+ let from = resolvePartialIndex(series.stroke?.partial?.fromIndex, length)
+ let to = resolvePartialIndex(series.stroke?.partial?.toIndex, length)
+ // A boundary on a bridged missing value moves to the nearest drawn point, so the bridge is dashed like the area.
+ while (isDrawable && from !== null && from > 1 && !isDrawable(from - 1)) {
+ from--
+ }
+ while (isDrawable && to !== null && to < length - 1 && !isDrawable(to)) {
+ to++
+ }
// No partial dashing — one stroke covering the whole line.
if (from === null && to === null) {
@@ -239,7 +253,7 @@ function planLineStrokes(series: ResolvedSeries, length: number): Stroke[] {
* points since the tangents need the whole run). NaN/out-of-domain points break the line into
* separate subpaths. Caller owns beginPath/stroke. */
function tracePath(drawCtx: DrawContext, data: number[], start: number, end: number): void {
- const { ctx, xScale, yScale, labels, smooth, yFloor } = drawCtx
+ const { ctx, xScale, yScale, labels, smooth, yFloor, connectNulls } = drawCtx
if (smooth) {
traceSmoothPath(drawCtx, data, start, end)
return
@@ -249,9 +263,11 @@ function tracePath(drawCtx: DrawContext, data: number[], start: number, end: num
const x = xScale(labels[i])
const rawY = yScale(data[i])
if (x == null || !isFinite(rawY)) {
- // Reset so the next valid point starts a fresh subpath rather than
- // connecting straight across the NaN gap.
- started = false
+ if (!connectNulls) {
+ // Reset so the next valid point starts a fresh subpath rather than
+ // connecting straight across the NaN gap.
+ started = false
+ }
continue
}
const y = yFloor != null ? Math.min(rawY, yFloor) : rawY
@@ -272,14 +288,14 @@ interface Point {
/** Collects the drawable points of [start, end] into gap-delimited runs — a new run starts after
* every NaN/out-of-domain point, so each run is a contiguous subpath. `yFloor` clamps each point. */
function collectSmoothRuns(drawCtx: DrawContext, data: number[], start: number, end: number): Point[][] {
- const { xScale, yScale, labels, yFloor } = drawCtx
+ const { xScale, yScale, labels, yFloor, connectNulls } = drawCtx
const runs: Point[][] = []
let run: Point[] = []
for (let i = start; i <= end; i++) {
const x = xScale(labels[i])
const y = yScale(data[i])
if (x == null || !isFinite(y)) {
- if (run.length > 0) {
+ if (run.length > 0 && !connectNulls) {
runs.push(run)
run = []
}
@@ -458,7 +474,7 @@ export function drawArea(
yValues?: number[],
bottomValues?: number[]
): void {
- const { ctx, xScale, yScale, labels, dimensions, smooth, yFloor } = drawCtx
+ const { ctx, xScale, yScale, labels, dimensions, smooth, yFloor, connectNulls } = drawCtx
const data = yValues ?? series.data
const opacity = series.fill?.opacity ?? 0.5
const baseline = dimensions.plotTop + dimensions.plotHeight
@@ -479,7 +495,9 @@ export function drawArea(
const x = xScale(labels[i])
const rawTop = yScale(data[i])
if (x == null || !isFinite(rawTop)) {
- breakSegment()
+ if (!connectNulls) {
+ breakSegment()
+ }
continue
}
// Clamped like the line stroke so the area's top edge stays under it; the bottom edge
@@ -489,7 +507,9 @@ export function drawArea(
const rawBottom = bottomValues[i]
const yBot = rawBottom == null ? NaN : yScale(rawBottom)
if (!isFinite(yBot)) {
- breakSegment()
+ if (!connectNulls) {
+ breakSegment()
+ }
continue
}
currentTop.push({ x, y: yTop, dataIndex: i })
@@ -589,11 +609,13 @@ function fillAreaPath(ctx: CanvasRenderingContext2D, top: Point[], bottom: Point
}
export function drawPoints(drawCtx: DrawContext, series: ResolvedSeries, yValues?: number[]): void {
- const { ctx, xScale, yScale, labels } = drawCtx
+ const { ctx, xScale, yScale, labels, connectNulls } = drawCtx
const data = yValues ?? series.data
const radius = series.points?.radius
- const isDrawable = (i: number): boolean =>
- i >= 0 && i < data.length && xScale(labels[i]) != null && isFinite(yScale(data[i]))
+ const isDrawable = (i: number): boolean => i >= 0 && i < data.length && isDrawablePoint(drawCtx, data, i)
+ const drawableCount = connectNulls ? data.filter((_, i) => isDrawable(i)).length : 0
+ const isIsolated = (i: number): boolean =>
+ connectNulls ? drawableCount === 1 : !isDrawable(i - 1) && !isDrawable(i + 1)
ctx.fillStyle = series.color
@@ -604,7 +626,7 @@ export function drawPoints(drawCtx: DrawContext, series: ResolvedSeries, yValues
continue
}
// No line passes through a point without drawable neighbors, so it needs a dot to be visible.
- const r = radius ?? (!isDrawable(i - 1) && !isDrawable(i + 1) ? ISOLATED_POINT_RADIUS : 0)
+ const r = radius ?? (isIsolated(i) ? ISOLATED_POINT_RADIUS : 0)
if (r <= 0) {
continue
}
@@ -1035,13 +1057,15 @@ export interface LineSeriesLayerOptions {
* past it. Pass the chart's `showAxisLines` — without an axis line the overhang is invisible
* and edge line caps are left whole. */
clipLeftEdge?: boolean
+ connectNulls?: boolean
}
/** Draw a line/area layer (clipped vertically) — the per-series area/line/points orchestration shared
* by LineChart and ComboChart. Callers supply the y-value source (raw vs stacked tops), the fill
* predicate, and the z-order; the loop, clip, and primitive calls live here. */
export function drawLineSeriesLayer(options: LineSeriesLayerOptions): void {
- const { ctx, dimensions, labels, series, xScale, resolveYScale, smooth, yFloor, clipLeftEdge } = options
+ const { ctx, dimensions, labels, series, xScale, resolveYScale, smooth, yFloor, clipLeftEdge, connectNulls } =
+ options
const yValuesFor = options.yValuesFor ?? (() => undefined)
const bottomFor = options.bottomFor ?? ((s: ResolvedSeries) => s.fill?.lowerData)
const shouldFill = options.shouldFill ?? ((s: ResolvedSeries) => !!s.fill)
@@ -1053,7 +1077,7 @@ export function drawLineSeriesLayer(options: LineSeriesLayerOptions): void {
return
}
drawArea(
- { ctx, dimensions, labels, xScale, yScale: resolveYScale(s), smooth, yFloor },
+ { ctx, dimensions, labels, xScale, yScale: resolveYScale(s), smooth, yFloor, connectNulls },
s,
yValuesFor(s),
bottomFor(s)
@@ -1063,7 +1087,16 @@ export function drawLineSeriesLayer(options: LineSeriesLayerOptions): void {
if (s.fill?.lowerData) {
return
}
- const drawCtx: DrawContext = { ctx, dimensions, labels, xScale, yScale: resolveYScale(s), smooth, yFloor }
+ const drawCtx: DrawContext = {
+ ctx,
+ dimensions,
+ labels,
+ xScale,
+ yScale: resolveYScale(s),
+ smooth,
+ yFloor,
+ connectNulls,
+ }
drawLine(drawCtx, s, yValuesFor(s))
drawPoints(drawCtx, s, yValuesFor(s))
}
diff --git a/packages/quill/packages/charts/src/core/types.ts b/packages/quill/packages/charts/src/core/types.ts
index 6fc632d3e31a..9ce79b768072 100644
--- a/packages/quill/packages/charts/src/core/types.ts
+++ b/packages/quill/packages/charts/src/core/types.ts
@@ -522,6 +522,8 @@ export interface LineChartConfig extends ChartConfig {
percentStackView?: boolean
/** Place each label at its time instead of evenly by index. Labels must be ascending. Memoize it. */
timeScale?: TimeScale
+ /** Draw lines and areas across missing (non-finite) values instead of breaking them. */
+ connectNulls?: boolean
/** Value-axis domain control — omit for data-derived auto-scaling. See {@link ValueDomain}. */
valueDomain?: ValueDomain
/** Float the value axis to its data range instead of clamping the baseline to 0 (a y-axis "start
diff --git a/packages/quill/packages/charts/src/docs/axes.md b/packages/quill/packages/charts/src/docs/axes.md
index 06137c736ef9..31084887b9ed 100644
--- a/packages/quill/packages/charts/src/docs/axes.md
+++ b/packages/quill/packages/charts/src/docs/axes.md
@@ -31,6 +31,7 @@ Theme-side restyle knobs, all optional: `theme.axisLineColor` strokes the baseli
- `maxCategoryLabelWidth` truncates category labels with an ellipsis and reveals the full value on hover. It also clamps the axis margin, so a long label cannot push the plot off screen. On horizontal charts the cap grows to 30% of the chart width when that is wider, so a wider chart shows more of each label. `MAX_CATEGORY_LABEL_WIDTH` is the exported default.
- `TimeSeriesLineChart` `xScale: 'time'` places each point at its time, so a stretch with no samples keeps its width. Labels parse in `xAxis.timezone` and must be ascending. `xDomain: [from, to]` in epoch ms spans the axis past the data. Ticks stay on the data labels.
- `LineChart` `timeScale: { parse, domain }` places each label at its time instead of evenly by index. `parse` returns epoch milliseconds, and labels outside `domain` are not drawn. Memoize the object.
+- A missing (non-finite) value breaks the line and area. `connectNulls: true` draws across it instead.
## Y-axis format