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): add bar, series, and tooltip support to charts Sparkline (#70236)

Sam Penningtonfeabcf46f0c6
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 7b271f6fa23c..663aedb5e0a3 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -900,6 +900,10 @@ snapshots:
hash: v1.k794b7964.298a8b5041dd7d331feb2af9f05f88f46c3a7d9d09ff3518931bab2635105bac.riN9w_kEdcqhVqOTuZT46yyaIU2V9zSeTCjuCZ3D3hs
components-hogcharts-slopechart--without-series-labels--light:
hash: v1.k794b7964.03ec4e12d62a1f40c27d1c408c3f99fe35c895b4594b58e1c7732a4e5b1db38e.YofX1C439e9BBPGc_hkrYO2MgMPzWdrA4hk_XrYym_k
+ components-hogcharts-sparkline--bars--dark:
+ hash: v1.k794b7964.07530240026e946bc6e25333117d90c59940ab7e9f781ba4f4067a37f3870a79.FEXCKn2z4UQG-NJNzcNhVBIzZ0iJss7TgLSWYh0BK_E
+ components-hogcharts-sparkline--bars--light:
+ hash: v1.k794b7964.48157b40b3db233d512be726442898c4fb91228847c4f2a5aaab0cecb4e35616.7BH5SHAcN-xWcpncjUIM_0JXrPcN_xlDyJ5DwbE4SRE
components-hogcharts-sparkline--custom-color--dark:
hash: v1.k794b7964.fe78efe136cddcd6eb3b35c161f9ea1ab15383c31a7406f12fd66726d4ec37e9.1CoDmn7MkL3aQvQLXIWLG6CE4jgEm6T3uHEFOJzeAhg
components-hogcharts-sparkline--custom-color--light:
@@ -916,10 +920,18 @@ snapshots:
hash: v1.k794b7964.53fdc0d3c494d1ed89587f0c1867c77a2793f4e5bd02775451e3fa50080c3223.1q18RKBU3X_MI1FXD9koGx5irEnkztXov4yG-jFzNOw
components-hogcharts-sparkline--flat--light:
hash: v1.k794b7964.d786a4e2f7f586efb844c69f4c8469218df01fc8cb8fcf4f062f91cd901901e5.SktIyoMHAw_PGyJT-MFUaQ7wN_3QKUedaAU9mt9LUTM
+ components-hogcharts-sparkline--stacked-bars--dark:
+ hash: v1.k794b7964.54e69305ddd513385068777be8fed68d51762555e0e44802262603ef7cec26bc.-HTfmwGZaCaOA8HYSzz5Coeht6NG5rgkSDG1OxAZQzY
+ components-hogcharts-sparkline--stacked-bars--light:
+ hash: v1.k794b7964.66f3f5ff463287dd88ee1ecaf0a9e22893ed7236fbc2aa621d1a5274a680b61e.mfTQ4d8B896ZNHIsBv_OY6aMv8R4WbP6Z3eQQnZVwGY
components-hogcharts-sparkline--volatile--dark:
hash: v1.k794b7964.78c5852a35d0712e541e43742c092734fae98c4211006e5cd32de0008485092c.JRPUzMhsTjR1kj7t_k6dmc3J3afFEnp5MhYokgKnwok
components-hogcharts-sparkline--volatile--light:
hash: v1.k794b7964.43ae770e9ade45be47b529127a9836a97f508a6157f08ec81c81dd38bfbf1120.ZTO1dJnuVbYebO8PRQhm2TFf4Nwpdx5Vf--u-kHMDv4
+ components-hogcharts-sparkline--with-tooltip--dark:
+ hash: v1.k794b7964.111f64658c7df79aea9f2214d2e8d03dddeda94d29c45c4658fdd9adaaed6499.7UFbDNHh1CVeVxVeS2nF3yfgl4NHx5esZP-VQW9vih8
+ components-hogcharts-sparkline--with-tooltip--light:
+ hash: v1.k794b7964.b620c4de95000d73bfae09022e7da415f67615d3a66c15d99d301ce15e29fa12.ttyPKDDw4PQikkuHgGg8uvhvabRMh-L46Aydjae728g
components-hogcharts-timeseriesbarchart--basic--dark:
hash: v1.k794b7964.f31a9b390355debe67ab4c295f06cab7f858ec7e6e86f1bd45353000fadff5d7.i5bWMbjTSMyU8lL2xRmonD5JGtF8KDALH8Fo8xfg-f0
components-hogcharts-timeseriesbarchart--basic--light:
diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.md
index 93ae54a58432..46439f28d4d4 100644
--- a/packages/quill/packages/charts/AGENTS.md
+++ b/packages/quill/packages/charts/AGENTS.md
@@ -14,7 +14,7 @@ Quick-reference for AI agents using `@posthog/quill-charts`. Canvas-rendered cha
| TimeSeriesComboChart | Mixed bar + line/area on a time x-axis — `ComboChart` plus the time-series chrome (date x-axis, goal lines, legend, value labels) |
| PieChart | Part-of-whole, one value per series; `innerRadiusRatio` for donut + `centerLabel`; on-slice labels via `showLabelOnSlice`/`showValueOnSlice`, positioned with `labelRadiusRatio` (0=center, 1=rim) and gated by `minSlicePercentForLabel` |
| BoxPlot | Distribution summaries — `{ min, p25, median, mean, p75, max }` per label |
-| Sparkline | Tiny inline trend from a flat `number[]` — no axes, no series structure |
+| Sparkline | Tiny inline trend, no axes — a gradient-filled line (default) or stacked bars via `type: 'bar'`; a flat `number[]` + `color`, or full `series` for multi-series; tooltip off by default, opt in with a `tooltip` render prop |
| MetricCard | Headline number + sparkline + change pill (dashboard stat tiles) |
| SlopeChart | Change between two points (slopegraph) — one line per series, left "before" → right "after"; `data: [start, end]` |
diff --git a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.stories.tsx b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.stories.tsx
index 9a38880399a9..616325fff219 100644
--- a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.stories.tsx
+++ b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.stories.tsx
@@ -1,5 +1,6 @@
import { Meta, StoryObj } from '@storybook/react'
+import { DefaultTooltip } from '../../overlays/DefaultTooltip'
import { Stage, useReactiveTheme } from '../../story-helpers'
import { Sparkline } from './Sparkline'
@@ -69,3 +70,48 @@ export const FillOpacityVariants: Story = {
)
},
}
+
+export const Bars: Story = {
+ render: () => {
+ const theme = useReactiveTheme()
+ return (
+ <Stage width={360} height={120}>
+ <Sparkline data={VOLATILE} theme={theme} type="bar" />
+ </Stage>
+ )
+ },
+}
+
+export const StackedBars: Story = {
+ render: () => {
+ const theme = useReactiveTheme()
+ return (
+ <Stage width={360} height={120}>
+ <Sparkline
+ type="bar"
+ series={[
+ { key: 'success', label: 'Success', data: RISING },
+ { key: 'failure', label: 'Failure', data: VOLATILE.map((v) => v * 20) },
+ ]}
+ theme={theme}
+ />
+ </Stage>
+ )
+ },
+}
+
+export const WithTooltip: Story = {
+ render: () => {
+ const theme = useReactiveTheme()
+ return (
+ <Stage width={360} height={120}>
+ <Sparkline
+ data={RISING}
+ labels={RISING.map((_, i) => `Week ${i + 1}`)}
+ theme={theme}
+ tooltip={(ctx) => <DefaultTooltip {...ctx} />}
+ />
+ </Stage>
+ )
+ },
+}
diff --git a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.test.tsx b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.test.tsx
index 4efb06ae17e6..aa9ae5b46a84 100644
--- a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.test.tsx
+++ b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.test.tsx
@@ -1,10 +1,11 @@
import { fireEvent } from '@testing-library/react'
-import type { ChartTheme } from '../../core/types'
-import { renderHogChart } from '../../testing'
+import type { ChartTheme, TooltipContext } from '../../core/types'
+import { DefaultTooltip } from '../../overlays/DefaultTooltip'
+import { createDefaultTooltipAccessor, hoverUntilTooltip, renderHogChart } from '../../testing'
import { Sparkline } from './Sparkline'
-const THEME: ChartTheme = { colors: ['#22d3ee'], backgroundColor: '#ffffff' }
+const THEME: ChartTheme = { colors: ['#22d3ee', '#f14f58'], backgroundColor: '#ffffff' }
const LABELS = ['Jan', 'Feb', 'Mar', 'Apr']
describe('Sparkline', () => {
@@ -45,4 +46,32 @@ describe('Sparkline', () => {
const { container } = renderHogChart(<Sparkline data={[10, 20, 30]} theme={THEME} />)
expect(container.querySelector('canvas')).not.toBeNull()
})
+
+ it('renders multi-series stacked bars via the series prop', () => {
+ const { chart } = renderHogChart(
+ <Sparkline
+ type="bar"
+ series={[
+ { key: 'success', label: 'Success', data: [10, 20, 30, 40] },
+ { key: 'failure', label: 'Failure', data: [1, 2, 3, 4] },
+ ]}
+ labels={LABELS}
+ theme={THEME}
+ />
+ )
+ expect(chart.seriesCount).toBe(2)
+ })
+
+ it('enables the tooltip when a tooltip render prop is supplied', async () => {
+ const { chart } = renderHogChart(
+ <Sparkline
+ data={[100, 200, 300, 400]}
+ labels={LABELS}
+ theme={THEME}
+ tooltip={(ctx: TooltipContext) => <DefaultTooltip {...ctx} />}
+ />
+ )
+ const tooltip = createDefaultTooltipAccessor(await hoverUntilTooltip(chart.element, 2, LABELS.length))
+ expect(tooltip.label()).toBe('Mar')
+ })
})
diff --git a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsx b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsx
index 851bab7fd5a5..7f5d0b5ae790 100644
--- a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsx
+++ b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsx
@@ -3,16 +3,24 @@ import React, { useEffect, useMemo } from 'react'
import { useChartHover } from '../../core/chart-context'
import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'
import { useLatest } from '../../core/hooks/useLatest'
-import type { ChartTheme, LineChartConfig, Series } from '../../core/types'
+import type { BarChartConfig, ChartTheme, LineChartConfig, Series, TooltipContext } from '../../core/types'
+import { BarChart } from '../BarChart/BarChart'
import { LineChart } from '../LineChart/LineChart'
export interface SparklineProps {
- data: number[]
+ /** Single-series values. Ignored when `series` is provided. */
+ data?: number[]
+ /** Multi-series form with full per-series control (color, fill, stroke). Bars render stacked.
+ * When set, the single-series conveniences (`data`, `color`, `fillOpacity`,
+ * `dashedFromIndex`) are ignored — express them on the series entries instead. */
+ series?: Series[]
/** Optional x-axis labels — when omitted, indices stand in. Consumers can look up
* the hovered index against their own labels. */
labels?: string[]
theme: ChartTheme
color?: string
+ /** `line` (default) draws a gradient-filled trend line; `bar` draws stacked bars. */
+ type?: 'line' | 'bar'
height?: number
/** Fill the parent's height (flex child) instead of using a fixed `height`. */
fill?: boolean
@@ -22,19 +30,18 @@ export interface SparklineProps {
dashedFromIndex?: number
/** Fires the hovered index, or -1 when not hovering. */
onHoverIndexChange?: (index: number) => void
+ /** Tooltip content renderer. Sparkline tooltips are off by default; supplying this enables them. */
+ tooltip?: (ctx: TooltipContext) => React.ReactNode
className?: string
dataAttr?: string
onError?: (error: Error, info: React.ErrorInfo) => void
}
-const SPARKLINE_CONFIG: LineChartConfig = {
- hideXAxis: true,
- hideYAxis: true,
- showCrosshair: true,
- tooltip: { enabled: false },
- // Reserve room for the hover highlight ring (radius + 2 = 6px) so it isn't clipped at the top/bottom edge.
- margins: { top: 6, right: 0, bottom: 6, left: 0 },
-}
+const BASE_CONFIG = { hideXAxis: true, hideYAxis: true } as const
+// Reserve room for the hover highlight ring (radius + 2 = 6px) so it isn't clipped at the top/bottom edge.
+const LINE_MARGINS = { top: 6, right: 0, bottom: 6, left: 0 }
+// Bars have no hover ring — sit them flush on the baseline, with a sliver of headroom for the tallest stack.
+const BAR_MARGINS = { top: 2, right: 0, bottom: 0, left: 0 }
export function Sparkline(props: SparklineProps): React.ReactElement {
const { onError, ...rest } = props
@@ -47,46 +54,69 @@ export function Sparkline(props: SparklineProps): React.ReactElement {
function SparklineInner({
data,
+ series,
labels,
theme,
color,
+ type = 'line',
height = 120,
fill = false,
fillOpacity = 0.35,
dashedFromIndex,
onHoverIndexChange,
+ tooltip,
className,
dataAttr,
}: Omit<SparklineProps, 'onError'>): React.ReactElement {
const resolvedColor = color ?? theme.colors[0]
+ const chartSeries = useMemo<Series[]>(() => {
+ if (series) {
+ return series
+ }
+ const single: Series = { key: 'sparkline', label: 'sparkline', data: data ?? [], color: resolvedColor }
+ if (type === 'line') {
+ single.fill = { gradient: true, opacity: fillOpacity }
+ if (dashedFromIndex != null) {
+ single.stroke = { partial: { fromIndex: dashedFromIndex } }
+ }
+ }
+ return [single]
+ }, [series, data, resolvedColor, type, fillOpacity, dashedFromIndex])
+ // Longest series, not just the first — a shorter/empty leading series must not truncate the labels.
+ const pointCount = useMemo(() => Math.max(0, ...chartSeries.map(({ data }) => data.length)), [chartSeries])
const resolvedLabels = useMemo<string[]>(
- () => labels ?? Array.from({ length: data.length }, (_, i) => String(i)),
- [labels, data.length]
+ () => labels ?? Array.from({ length: pointCount }, (_, i) => String(i)),
+ [labels, pointCount]
)
- const series = useMemo<Series[]>(
- () => [
- {
- key: 'sparkline',
- label: 'sparkline',
- data,
- color: resolvedColor,
- fill: { gradient: true, opacity: fillOpacity },
- stroke: dashedFromIndex != null ? { partial: { fromIndex: dashedFromIndex } } : undefined,
- },
- ],
- [data, resolvedColor, fillOpacity, dashedFromIndex]
+ const hasTooltip = tooltip != null
+ const config = useMemo<LineChartConfig & BarChartConfig>(
+ () => ({
+ ...BASE_CONFIG,
+ ...(type === 'bar'
+ ? { barCornerRadius: 2, margins: BAR_MARGINS }
+ : { showCrosshair: true, margins: LINE_MARGINS }),
+ ...(hasTooltip ? {} : { tooltip: { enabled: false } }),
+ }),
+ [type, hasTooltip]
)
const wrapperStyle = useMemo<React.CSSProperties | undefined>(() => (fill ? undefined : { height }), [fill, height])
+ const watcher = onHoverIndexChange ? <HoverWatcher onHoverChange={onHoverIndexChange} /> : null
return (
<div
className={`relative flex flex-col ${fill ? 'flex-1 min-h-0' : ''} ${className ?? ''}`}
style={wrapperStyle}
data-attr={dataAttr}
>
- <LineChart series={series} labels={resolvedLabels} theme={theme} config={SPARKLINE_CONFIG}>
- {onHoverIndexChange ? <HoverWatcher onHoverChange={onHoverIndexChange} /> : null}
- </LineChart>
+ {type === 'bar' ? (
+ <BarChart series={chartSeries} labels={resolvedLabels} theme={theme} config={config} tooltip={tooltip}>
+ {watcher}
+ </BarChart>
+ ) : (
+ <LineChart series={chartSeries} labels={resolvedLabels} theme={theme} config={config} tooltip={tooltip}>
+ {watcher}
+ </LineChart>
+ )}
</div>
)
}