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): set line width per series (#125543)

Ben White7de4ea4a2ff1
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index d3cbd22fc700..3e669ea8a1f4 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -1344,6 +1344,10 @@ snapshots:
hash: v1.k794b7964.97f334a3fc014fd369a637380f1153670d4d322f07354f8efa8fd2f7d65f58f7.jCo_cDeo6ux668mBVSQCCEDMCD-Iz2q8iEX0crBWA_k
components-hogcharts-linechart--stacked-area--light:
hash: v1.k794b7964.5a0eba738430b9fb855d462426eefbfb23f2d367be42dcf0ad15126288fc4eba.wb6FcBkHysbLnPi0dyfor5gIcm_AgBo0lNzvt7Yfkjk
+ components-hogcharts-linechart--stroke-width--dark:
+ hash: v1.k794b7964.3eb02e8738b3a78ccf934aabe2577bcac87040cb7e7e8bcfe6b5f53314f21a2b.LPXAdaC8tgos-hhcV2JfMs9xBv8-CjqR3DZNvQ7ySME
+ components-hogcharts-linechart--stroke-width--light:
+ hash: v1.k794b7964.dc540d26e3f954167c985bfcce9e6d91b8f7640bd9c8c37509796bf1aa4290fa.Ucp2Mtgbra9ytiJIqVReB_Tdhs8_pYHcNmZ0yK5SnbU
components-hogcharts-linechart--triple-y-axis--dark:
hash: v1.k794b7964.5c0de8b56170087f74d30f6cd12b5563f647c92982e775bbb44ab7b3a3e7dfb0.ahREKNW7o2ambwyhfjIZBPW7X0cTz3D3MkHr2VeoGoU
components-hogcharts-linechart--triple-y-axis--light:
diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.md
index 7657249af681..236eebe31748 100644
--- a/packages/quill/packages/charts/AGENTS.md
+++ b/packages/quill/packages/charts/AGENTS.md
@@ -61,7 +61,7 @@ const series: Series[] = [
- `key`, `label`, `data` are required. `key` is the React key and the stacking identity.
- `labels` must be unique. The x-scale keys positions off the strings; a duplicate collapses onto the first occurrence and draws the series backwards. On time-series charts pass ISO dates and format ticks via `xAxis.timezone` / `interval`.
- `meta` carries arbitrary data into tooltips and click handlers; narrow it with `Series<MyMeta>`.
-- `visibility.{excluded,tooltip,total,valueLabel}` controls where a series appears. `overlay: true` marks a derived series (trend line, moving average). Line-only: `points`, `stroke.pattern`, `stroke.partial`, `fill`. Bar-only: `bars[]`, `trackData`. Full field docs on `Series` in `src/core/types.ts`.
+- `visibility.{excluded,tooltip,total,valueLabel}` controls where a series appears. `overlay: true` marks a derived series (trend line, moving average). Line-only: `points`, `stroke.width`, `stroke.pattern`, `stroke.partial`, `fill`. Bar-only: `bars[]`, `trackData`. Full field docs on `Series` in `src/core/types.ts`.
## Sizing
diff --git a/packages/quill/packages/charts/src/charts/LineChart/LineChart.stories.tsx b/packages/quill/packages/charts/src/charts/LineChart/LineChart.stories.tsx
index ed8c065f4c20..5c77fb4a61e6 100644
--- a/packages/quill/packages/charts/src/charts/LineChart/LineChart.stories.tsx
+++ b/packages/quill/packages/charts/src/charts/LineChart/LineChart.stories.tsx
@@ -63,6 +63,31 @@ export const StackedArea: Story = {
},
}
+/** `stroke.width` sets each line's width; `0` drops the stroke so only the fill or points draw. */
+export const StrokeWidth: Story = {
+ render: () => {
+ const theme = useReactiveTheme()
+ const series: Series[] = [
+ { key: 'thin', label: 'width 1', color: '', data: [20, 35, 28, 60, 45, 70, 52], stroke: { width: 1 } },
+ { key: 'default', label: 'default (2)', color: '', data: [30, 45, 38, 70, 55, 80, 62] },
+ { key: 'thick', label: 'width 5', color: '', data: [40, 55, 48, 80, 65, 90, 72], stroke: { width: 5 } },
+ {
+ key: 'fill-only',
+ label: 'width 0 + fill',
+ color: '',
+ data: [8, 14, 11, 22, 18, 26, 20],
+ stroke: { width: 0 },
+ fill: { opacity: 0.4 },
+ },
+ ]
+ return (
+ <Stage>
+ <LineChart series={series} labels={DAYS} config={BASIC} theme={theme} />
+ </Stage>
+ )
+ },
+}
+
export const PercentStacked: Story = {
render: () => {
const theme = useReactiveTheme()
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 d9121c396c34..026ce18ddf6e 100644
--- a/packages/quill/packages/charts/src/core/canvas-renderer.test.ts
+++ b/packages/quill/packages/charts/src/core/canvas-renderer.test.ts
@@ -193,6 +193,32 @@ describe('hog-charts canvas-renderer', () => {
})
})
+ describe('drawLine — stroke width', () => {
+ it.each([
+ { name: 'defaults to the standard width', stroke: undefined, lineWidth: 2, strokes: 1 },
+ { name: 'uses stroke.width', stroke: { width: 5 }, lineWidth: 5, strokes: 1 },
+ { name: 'draws nothing for a zero width', stroke: { width: 0 }, lineWidth: 0, strokes: 0 },
+ { name: 'draws nothing for a NaN width', stroke: { width: NaN }, lineWidth: 0, strokes: 0 },
+ ])('$name', ({ stroke, lineWidth, strokes }) => {
+ const ctx = mockCanvasContext()
+ drawLine(makeDrawContext(ctx, ['a', 'b']), makeSeries({ key: 's1', data: [10, 90], stroke }))
+ expect(ctx.lineWidth).toBe(lineWidth)
+ expect(ctx.stroke).toHaveBeenCalledTimes(strokes)
+ })
+ })
+
+ // Round caps close a dash gap narrower than the width, so the default gap grows with it.
+ it.each([
+ { name: 'the default width', width: undefined, custom: undefined, expected: [10, 10] },
+ { name: 'a 10px line', width: 10, custom: undefined, expected: [10, 18] },
+ { name: 'a custom pattern on a 10px line', width: 10, custom: [2, 8], expected: [2, 8] },
+ ])('keeps the incomplete-period dash open for $name', ({ width, custom, expected }) => {
+ const ctx = mockCanvasContext()
+ const stroke = { width, partial: { fromIndex: 1, pattern: custom } }
+ drawLine(makeDrawContext(ctx, ['a', 'b', 'c']), makeSeries({ key: 's1', data: [10, 50, 30], stroke }))
+ expect(dashCalls(ctx)).toContainEqual(expected)
+ })
+
describe('drawLine — monotone smoothing and yFloor', () => {
it('emits one bezier segment per point pair and no interior lineTo when smooth', () => {
const ctx = mockCanvasContext()
@@ -1270,6 +1296,30 @@ describe('hog-charts canvas-renderer', () => {
)
})
+ describe('drawLineSeriesLayer — yFloor', () => {
+ // A NaN width draws no line, so its fill rests on the baseline itself rather than vanishing.
+ it.each([
+ { name: 'the default stroke', stroke: undefined, fill: undefined, expectedY: 367 },
+ { name: 'a 10px stroke', stroke: { width: 10 }, fill: undefined, expectedY: 363 },
+ { name: 'the fill of a NaN-width stroke', stroke: { width: NaN }, fill: {}, expectedY: 368 },
+ ])('rests $name on the x-axis line at y=$expectedY', ({ stroke, fill, expectedY }) => {
+ const ctx = mockCanvasContext()
+ const labels = ['a', 'b']
+ const xScale = scalePoint<string>().domain(labels).range([48, 784]).padding(0)
+ const yScale = scaleLinear().domain([0, 100]).range([368, 16])
+ drawLineSeriesLayer({
+ ctx,
+ dimensions,
+ labels,
+ series: [makeSeries({ key: 's1', data: [0, 0], stroke, fill })],
+ xScale,
+ resolveYScale: () => yScale,
+ yFloor: 367,
+ })
+ expect(ctx.moveTo).toHaveBeenCalledWith(48, expectedY)
+ })
+ })
+
describe('traceScatterMarker', () => {
it.each<[ScatterMarkerShape, 'arc' | 'rect', number[]]>([
['circle', 'arc', [10, 20, 6, 0, Math.PI * 2]],
diff --git a/packages/quill/packages/charts/src/core/canvas-renderer.ts b/packages/quill/packages/charts/src/core/canvas-renderer.ts
index 5e3cf42f960c..01ed4a2bec44 100644
--- a/packages/quill/packages/charts/src/core/canvas-renderer.ts
+++ b/packages/quill/packages/charts/src/core/canvas-renderer.ts
@@ -22,8 +22,9 @@ export interface DrawContext {
/** Smooth the line/area with monotone-cubic interpolation instead of straight segments. */
smooth?: boolean
/** Largest drawable y (px) for line/area strokes. Charts drawing an x-axis line pass the
- * 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. */
+ * baseline minus half the default stroke width, and `drawLineSeriesLayer` raises it per series
+ * for a wider stroke, 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
@@ -33,22 +34,35 @@ function isDrawablePoint({ xScale, yScale, labels }: DrawContext, data: number[]
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
+/** Default stroke width of series lines. Half of it is the stroke's overhang past a point, which the
* `yFloor`/left-clip callers use to keep strokes flush against drawn axis lines. */
export const LINE_STROKE_WIDTH = 2
+/** The series' stroke width: the default when unset, 0 (no line) for a NaN or negative width. */
+function lineStrokeWidth(series: ResolvedSeries): number {
+ return Math.max(0, series.stroke?.width ?? LINE_STROKE_WIDTH) || 0
+}
+
+/** The incomplete-period dash: `stroke.partial.pattern`, else 10px dashes with an 8px visible gap.
+ * A round cap adds half the width to each end of a dash, so the gap grows with the width to stay
+ * open on a wide line. At the default width this is `[10, 10]`. */
+function partialDashPattern(series: ResolvedSeries): number[] {
+ return series.stroke?.partial?.pattern ?? [10, lineStrokeWidth(series) + 8]
+}
+
/** Dot radius for a point with no line through it, e.g. a series with one value. */
const ISOLATED_POINT_RADIUS = 3
export function drawLine(drawCtx: DrawContext, series: ResolvedSeries, yValues?: number[]): void {
const data = yValues ?? series.data
- if (data.length === 0) {
+ const width = lineStrokeWidth(series)
+ if (data.length === 0 || width === 0) {
return
}
const { ctx } = drawCtx
ctx.strokeStyle = series.color
- ctx.lineWidth = LINE_STROKE_WIDTH
+ ctx.lineWidth = width
ctx.lineJoin = 'round'
ctx.lineCap = 'round'
@@ -107,7 +121,7 @@ function drawFractionalTailDash(drawCtx: DrawContext, series: ResolvedSeries, da
// Dashed: the split pixel out to the final point.
ctx.beginPath()
- ctx.setLineDash(series.stroke?.partial?.pattern ?? [10, 10])
+ ctx.setLineDash(partialDashPattern(series))
ctx.moveTo(splitX, splitY)
ctx.lineTo(x1, y1)
ctx.stroke()
@@ -189,7 +203,7 @@ function drawSmoothFractionalTail(drawCtx: DrawContext, series: ResolvedSeries,
// Dashed: the split point out to the final point, along the same curve.
ctx.beginPath()
- ctx.setLineDash(series.stroke?.partial?.pattern ?? [10, 10])
+ ctx.setLineDash(partialDashPattern(series))
ctx.moveTo(split.x, split.y)
const end = tail[tail.length - 1]
ctx.bezierCurveTo(split.secondCp1x, split.secondCp1y, split.secondCp2x, split.secondCp2y, end.x, end.y)
@@ -210,7 +224,7 @@ interface 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 partialPattern = partialDashPattern(series)
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.
@@ -1071,13 +1085,16 @@ export function drawLineSeriesLayer(options: LineSeriesLayerOptions): void {
const shouldFill = options.shouldFill ?? ((s: ResolvedSeries) => !!s.fill)
const zOrder = options.zOrder ?? 'per-series'
const visible = series.filter((s) => !s.visibility?.excluded)
+ // `yFloor` is set for the default stroke; a wider stroke rests higher so it still sits on the axis.
+ const floorFor = (s: ResolvedSeries): number | undefined =>
+ yFloor == null ? undefined : yFloor - (lineStrokeWidth(s) - LINE_STROKE_WIDTH) / 2
const paintArea = (s: ResolvedSeries): void => {
if (!shouldFill(s)) {
return
}
drawArea(
- { ctx, dimensions, labels, xScale, yScale: resolveYScale(s), smooth, yFloor, connectNulls },
+ { ctx, dimensions, labels, xScale, yScale: resolveYScale(s), smooth, yFloor: floorFor(s), connectNulls },
s,
yValuesFor(s),
bottomFor(s)
@@ -1094,7 +1111,7 @@ export function drawLineSeriesLayer(options: LineSeriesLayerOptions): void {
xScale,
yScale: resolveYScale(s),
smooth,
- yFloor,
+ yFloor: floorFor(s),
connectNulls,
}
drawLine(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 e7c1e8e8db5c..6e2194f44456 100644
--- a/packages/quill/packages/charts/src/core/types.ts
+++ b/packages/quill/packages/charts/src/core/types.ts
@@ -88,6 +88,10 @@ export interface Series<Meta = unknown> {
}
/** Line stroke configuration. */
stroke?: {
+ /** Line width in CSS pixels. Defaults to 2. `0` draws no line, for an area fill or points
+ * without a stroke. Lines have round caps, which close a dash gap narrower than the width,
+ * so a wide line with a custom `pattern` needs wider gaps. */
+ width?: number
/** Canvas line dash pattern, e.g. [10, 10] for evenly dashed. Omit for solid. */
pattern?: number[]
/** A range of indices that should be drawn with a different (typically dashed) pattern. */
@@ -100,7 +104,8 @@ export interface Series<Meta = unknown> {
* beyond it — everything before stays solid. Lets a two-point line dash just its second
* half without a phantom interior point. Takes precedence over `fromIndex`/`toIndex`. */
fromFraction?: number
- /** Dash pattern for the partial range. Defaults to [10, 10]. */
+ /** Dash pattern for the partial range. Defaults to 10px dashes with an 8px visible gap,
+ * `[10, 10]` at the default width. */
pattern?: number[]
}
}