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.

fix(insights): format slope graph tooltip values with the unit formatter (#64229)

Paul D'Ambraaba28fc71206
diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.md
index d5c992e6815c..05709a6ee475 100644
--- a/packages/quill/packages/charts/AGENTS.md
+++ b/packages/quill/packages/charts/AGENTS.md
@@ -67,7 +67,7 @@ Charts fill their container and need a parent with real dimensions — a `0`-hei
- `ReferenceLine` variants: `goal` (dashed grey), `alert` (dashed red), `marker` (solid thin).
- `ValueLabels` formatter gets `(value, seriesIndex, dataIndex, context)`; in percent layouts `value` is a 0–1 fraction — use `context.rawValue` for the original.
- `Legend` is presentational: pass `items`, `onItemClick`, `hiddenKeys` — filtering series is the caller's state. `LegendItem.secondaryLabel` shows muted trailing text (e.g. a slope chart's per-series change).
-- `SlopeChart` config: `showSeriesLabels` (name beside each point; steepest line wins label collisions), `showStartLabels`/`showEndLabels` (defaults overridable per series via `meta.showStartLabel`/`showEndLabel`), `legend` (`{ show, position }`, rows carry the formatted change, ordered biggest-to-smallest by end value to match the lines' right-edge order), `valueFormatter`/`deltaFormatter`. The value axis is hidden by default — the start/end labels are the readout. The default tooltip orders its rows biggest-to-smallest by the hovered point's value (so many-breakdown tooltips match the lines' vertical order); pass your own `tooltip` to override. Per-series `meta.incompleteEnd` dashes only the second half of that connector (the last point is the current incomplete period); the renderer splits the final segment at its midpoint via `stroke.partial.fromFraction`, so a two-point line needs no phantom interior point.
+- `SlopeChart` config: `showSeriesLabels` (name beside each point; steepest line wins label collisions), `showStartLabels`/`showEndLabels` (defaults overridable per series via `meta.showStartLabel`/`showEndLabel`), `legend` (`{ show, position }`, rows carry the formatted change, ordered biggest-to-smallest by end value to match the lines' right-edge order), `valueFormatter`/`deltaFormatter`. The value axis is hidden by default — the start/end labels are the readout. The default tooltip orders its rows biggest-to-smallest by the hovered point's value (so many-breakdown tooltips match the lines' vertical order) and formats values with `valueFormatter` so they match the on-chart labels' units; pass your own `tooltip` to override. (`DefaultTooltip` itself takes an optional `valueFormatter` for the same reason — without it values fall back to `toLocaleString`.) Per-series `meta.incompleteEnd` dashes only the second half of that connector (the last point is the current incomplete period); the renderer splits the final segment at its midpoint via `stroke.partial.fromFraction`, so a two-point line needs no phantom interior point.
- y-axis `format`: `numeric | short | percentage | percentage_scaled | currency | duration | duration-ms`, plus `prefix`/`suffix`.
## Maintenance
diff --git a/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsx b/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsx
index 50635ab3dd00..87061f651d00 100644
--- a/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsx
+++ b/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsx
@@ -188,12 +188,13 @@ function SlopeChartInner<Meta = SlopeSeriesMeta>({
const legendItems = useMemo(() => slopeLegendItems(series, theme, deltaFormatter), [series, theme, deltaFormatter])
- // A caller-supplied tooltip takes precedence; otherwise sort the default rows (see helper).
+ // A caller-supplied tooltip takes precedence; otherwise sort the default rows (see helper) and
+ // format values with the chart's valueFormatter so the tooltip matches the on-chart labels' units.
const sortedTooltip = useCallback(
(ctx: TooltipContext<Meta>): React.ReactNode => (
- <DefaultTooltip {...ctx} seriesData={sortSlopeTooltipRows(ctx.seriesData)} />
+ <DefaultTooltip {...ctx} seriesData={sortSlopeTooltipRows(ctx.seriesData)} valueFormatter={valueFormatter} />
),
- []
+ [valueFormatter]
)
return (
diff --git a/packages/quill/packages/charts/src/overlays/DefaultTooltip.test.tsx b/packages/quill/packages/charts/src/overlays/DefaultTooltip.test.tsx
new file mode 100644
index 000000000000..323437a04f82
--- /dev/null
+++ b/packages/quill/packages/charts/src/overlays/DefaultTooltip.test.tsx
@@ -0,0 +1,29 @@
+import { cleanup, screen } from '@testing-library/react'
+
+import type { TooltipContext } from '../core/types'
+import { makeOverlayContext, renderOverlayInChart } from '../testing'
+import { DefaultTooltip } from './DefaultTooltip'
+
+const SCALES = { x: () => 0, y: (v: number) => v, yTicks: () => [] }
+
+const SERIES_DATA: TooltipContext['seriesData'] = [{ series: { key: 'a', label: 'A', data: [] }, value: 19402, color: '#000' }]
+
+function renderTooltip(valueFormatter?: (v: number) => string): void {
+ const ctx: TooltipContext = { label: 'When', seriesData: SERIES_DATA }
+ renderOverlayInChart(<DefaultTooltip {...ctx} valueFormatter={valueFormatter} />, makeOverlayContext(SCALES))
+}
+
+describe('DefaultTooltip', () => {
+ afterEach(cleanup)
+
+ it.each([
+ ['default (no formatter)', undefined, (19402).toLocaleString(), null as string | null],
+ ['custom valueFormatter', (v: number) => `${v}ms`, '19402ms', (19402).toLocaleString()],
+ ])('formats values with %s', (_, formatter, expected, absent) => {
+ renderTooltip(formatter)
+ screen.getByText(expected)
+ if (absent) {
+ expect(screen.queryByText(absent)).toBeNull()
+ }
+ })
+})
diff --git a/packages/quill/packages/charts/src/overlays/DefaultTooltip.tsx b/packages/quill/packages/charts/src/overlays/DefaultTooltip.tsx
index 28a136b3f6aa..365ba63fd707 100644
--- a/packages/quill/packages/charts/src/overlays/DefaultTooltip.tsx
+++ b/packages/quill/packages/charts/src/overlays/DefaultTooltip.tsx
@@ -1,7 +1,14 @@
import type { TooltipContext } from '../core/types'
import { TooltipSurface, TooltipSwatch } from './TooltipSurface'
-export function DefaultTooltip({ label, seriesData }: TooltipContext): React.ReactElement {
+interface DefaultTooltipProps extends TooltipContext {
+ /** Formats each row's value. Defaults to `toLocaleString` — pass the chart's own
+ * `valueFormatter` so the tooltip matches the axis/label units (e.g. durations, currency). */
+ valueFormatter?: (value: number) => string
+}
+
+export function DefaultTooltip({ label, seriesData, valueFormatter }: DefaultTooltipProps): React.ReactElement {
+ const format = valueFormatter ?? ((value: number): string => value.toLocaleString())
return (
<TooltipSurface>
<div className="font-semibold mb-1">{label}</div>
@@ -9,7 +16,7 @@ export function DefaultTooltip({ label, seriesData }: TooltipContext): React.Rea
<div key={s.series.key} className="flex items-center gap-2">
<TooltipSwatch color={s.color} />
<span>{s.series.label}:</span>
- <strong>{s.value.toLocaleString()}</strong>
+ <strong>{format(s.value)}</strong>
</div>
))}
</TooltipSurface>