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(insights): show value labels on hog-charts lifecycle insight (#60131)

Sam Pennington73c1396a6820
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index d6dd348587f9..f6fbbc974a88 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -721,9 +721,9 @@ snapshots:
components-hogcharts-timeserieslinechart--y-axis-formats--light:
hash: v1.k794b7964.2bef66aacecd65e45499ee99bf537162c17cb404a4c8d4871b7435fd463e8727.zc_zuF07aw8GGaVPuSnLNC-oWq1W6s78IjNb62kwLjo
components-hogcharts-valuelabels--cross-series-overlap-removal--dark:
- hash: v1.k794b7964.b007e0b3d5d267086c7feac666e863bf138c94e442834f38d827d9866bcfdca7.XPNu2APWwr3st71RImwnGyR-XvpLOXXKvqOOIAWV9VM
+ hash: v1.k794b7964.70b992c81df62237e75b4e7ef9e8a0619bf344c47846d5de0150193f06401968.d7cGU5PPTbQg6dU92MqhGDSqgdOn9NbShb64cqPIV_A
components-hogcharts-valuelabels--cross-series-overlap-removal--light:
- hash: v1.k794b7964.448986696bb416400c7b45c0cead3c555fb08740a06107d4c67cd6de3eac778a.-CC2KkgSe2QGS72LRQi30FdWUr3HiiDwZMFszFORz50
+ hash: v1.k794b7964.d7079d2593a2c6017ec0b6aaa09dd9e3cf3a06601bee1718c051e89bcbb3abfd.P2mMz7Nwya-iJlmgLAb4SMy8mHSTkzlOiQzvXO6brfA
components-hogcharts-valuelabels--dual-y-axes--dark:
hash: v1.k794b7964.70bd72cf9ac044f579156cb4075fb7473451e3ccfa92729d3c671526d78b2a4b.HDMQoBphdAnraD6C-ENB3-7NeEI_0cn5-SmvFtyVuyM
components-hogcharts-valuelabels--dual-y-axes--light:
@@ -2036,10 +2036,18 @@ snapshots:
hash: v1.k794b7964.a0b63c471c3939cbe79812a900b8f887ec600442e98d21627b874b5d8832c592.xjKULqFFbTBlaBNOQ-FWvGL_P1LfLzY3V1t-lj-gSy0
insights-trendslifecyclechart--stacked--light:
hash: v1.k794b7964.06299a8e05030704231ec0ab4031ff3193330c613a3b7f9688db9ce7e932fe49.-8nYhPa8-aLNwcTVGon6dY_Rn4KRC-l_frb-ikmvk1I
+ insights-trendslifecyclechart--stacked-with-values-on-series--dark:
+ hash: v1.k794b7964.e22cd804180d74ed539a09566d39a007c1218607709aeb38b4327b232097d0aa.YgSKLxUs3A8W3NeF3LLTH3Z4SxhKGMlMjYXv6JsM8B4
+ insights-trendslifecyclechart--stacked-with-values-on-series--light:
+ hash: v1.k794b7964.b8c4ee6cc4288293871d4c9dc83850e7e0cefa73bdc4a3984042c41953f01d26.PwlFs66ER2t5gbBToQJc2jL7ybJlg0K3h3CzWbUIRug
insights-trendslifecyclechart--unstacked--dark:
hash: v1.k794b7964.6b5f1e5ce04e682f39d957a57192db9dc38d29deae409146c45c275d8575341e.1wwEDPbO-jOXRNDOTT81-gPNbyMM2C5QpussoL6iEPA
insights-trendslifecyclechart--unstacked--light:
hash: v1.k794b7964.3467d1d62a847176237a5278030dcd296df1283111f6260f86f2d467e2cc457b.xxTvFm43ZodSjCbtbMNLpjn20JZLLK6h4-MElnqQa5I
+ insights-trendslifecyclechart--unstacked-with-values-on-series--dark:
+ hash: v1.k794b7964.51b27e46fb212a16b34a262aa0fd268060bc75f0664158514f7094ba20e3e19b.9e95m03noDYfUybRn95SPOcn1CmTppeZfkLIPxSSUlw
+ insights-trendslifecyclechart--unstacked-with-values-on-series--light:
+ hash: v1.k794b7964.266358bb2f7faba7c50e0b2bd2b4924c2abd746d9c1c6c5f79ce50c04757a26e.ccDgypjgZFpI8NbSzW1piWaUjaREnUunrT5UU6mUH9E
insights-trendslinechart--breakdown--dark:
hash: v1.k794b7964.26e070b747cdce14d42696be55405981bf7149f088bd0def208f50cd7f8abedd.VZhMtciuCaFsReKDPdj_svmfFGvxR_TsXGdwIPHd-34
insights-trendslinechart--breakdown--light:
@@ -4815,7 +4823,7 @@ snapshots:
scenes-app-insights-funnels--funnel-left-to-right-edit--light:
hash: v1.k794b7964.c63132bb5b13cbe97938ebdb2e59e320ebc338bd12b2d3bfd6af81e56270650a.3gc3UBg-8GVo4HkNBH7UrogK2q1uh9pXPDjTdYGT8nc
scenes-app-insights-funnels--funnel-left-to-right-edit-viewports--medium--dark:
- hash: v1.k794b7964.dad9df902a133cafd6b3314a3800fcb45dabbbb59a0766c87c226549392104fc.u8mciTuIKrwI-d3yiUx6dVpr3oLoJJ7tgvRU7hutwhU
+ hash: v1.k794b7964.8887db0213a228a0f958496f8de814bd30d4735f99f8dcd886fbc9740a703afd.U7j0SBm9JuPzdVQVGChjxt-Iw1svrkRV06tAv9pg-mM
scenes-app-insights-funnels--funnel-left-to-right-edit-viewports--medium--light:
hash: v1.k794b7964.1dd3425e658b907855610a715527654f68a242ed0c1427803dcf4d2289806fed.xd6ZnWiKWTOAglDDiM771sUAuByJ76icZNkrOre_orA
scenes-app-insights-funnels--funnel-left-to-right-edit-viewports--superwide--dark:
@@ -4823,9 +4831,9 @@ snapshots:
scenes-app-insights-funnels--funnel-left-to-right-edit-viewports--superwide--light:
hash: v1.k794b7964.c95c5a49f7a20b7fcb1041dd26c73ee87c9a906cb229247a44fd4633bbdc9173.StrJ2VBA5acLqvnsiO2CS_o6qadxeaeO6ST8lxf5UdI
scenes-app-insights-funnels--funnel-left-to-right-edit-viewports--wide--dark:
- hash: v1.k794b7964.c47bdb853ef9b6becfb675e2ca0bdbdb0c1b4385cf2442d50dd92752d90f7906.URrKOLP9uUjbKWcui2yOA7hm_JsXsbkAlOcWJshoQrI
+ hash: v1.k794b7964.293297b83994468984576e5c15fa2cbbb2c4c0a7a8234959b9deba539efa383b.O7SwG4Drfr3z02hcLNC1LsLsGWddouhybBp4xZaUE-A
scenes-app-insights-funnels--funnel-left-to-right-edit-viewports--wide--light:
- hash: v1.k794b7964.6c391cbc05b64230fbd8defdbb960b131fd433409817497873fb9ffc705cd360.EIwByuofuT77yP259A9ffqfjSFwW9MrUDOvsZNA_aZM
+ hash: v1.k794b7964.c63132bb5b13cbe97938ebdb2e59e320ebc338bd12b2d3bfd6af81e56270650a.KSiw8WpvjWXIDh87ExL4cPnopVJlTXvzU8N8ruh6Hi8
scenes-app-insights-funnels--funnel-time-to-convert--dark:
hash: v1.k794b7964.d64808d2bfc36017f89b57feeaa373e20188738a622801207fa1beb59d02fdd9.cFuKF_Ow887Z3X3BoNhtTvkUWY2vips9HDBe-B02eQE
scenes-app-insights-funnels--funnel-time-to-convert--light:
diff --git a/frontend/src/lib/hog-charts/overlays/ValueLabels.test.tsx b/frontend/src/lib/hog-charts/overlays/ValueLabels.test.tsx
index ed5527000b98..a61202c5c5d4 100644
--- a/frontend/src/lib/hog-charts/overlays/ValueLabels.test.tsx
+++ b/frontend/src/lib/hog-charts/overlays/ValueLabels.test.tsx
@@ -106,6 +106,31 @@ describe('ValueLabels', () => {
})
})
+ it('vertical: flips a top-clipping label inside the bar instead of expanding margins', () => {
+ // yScale(100) = 16, so an above-placed label would render with top=16-22=-6 and get
+ // clipped by the wrapper's `overflow: hidden`. The overlay flips it below the bar top.
+ const series: ResolvedSeries[] = [{ key: 's', label: 'S', color: '#f00', data: [100] }]
+ const { container } = renderInChart(makeContext(series, { labels: ['Mon'] }), <ValueLabels />)
+ const divs = labelDivs(container)
+ expect(divs).toHaveLength(1)
+ expect(divs[0].style.transform).toBe('translateX(-50%)')
+ expect(divs[0].style.top).toBe('16px')
+ })
+
+ it('horizontal: flips a right-clipping label inside the bar instead of expanding margins', () => {
+ // Place the value-coord 4px from the right wrapper edge so the label (>4px wide) would
+ // overflow when anchored on its leading edge; flip to anchor on its trailing edge instead.
+ const series: ResolvedSeries[] = [{ key: 's', label: 'S', color: '#f00', data: [1] }]
+ const ctx = makeContext(series, {
+ axisOrientation: 'horizontal',
+ labels: ['Mon'],
+ scales: { x: () => 60, y: () => 796, yTicks: () => [0, 1] },
+ })
+ const divs = labelDivs(renderInChart(ctx, <ValueLabels />).container)
+ expect(divs).toHaveLength(1)
+ expect(divs[0].style.transform).toBe('translate(-100%, -50%)')
+ })
+
it('drops overlapping labels via greedy collision avoidance', () => {
// Two points at the same x: only one should survive.
const series: ResolvedSeries[] = [{ key: 's', label: 'S', color: '#f00', data: [50, 50] }]
diff --git a/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx b/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx
index 10a305a699a1..833408a0a60a 100644
--- a/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx
+++ b/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx
@@ -2,7 +2,7 @@ import React, { useMemo } from 'react'
import { useChartHover, useChartLayout } from '../core/chart-context'
import { resolveYScaleForSeries } from '../core/scales'
-import type { ChartScales, ResolvedSeries, ResolveValueFn } from '../core/types'
+import type { ChartDimensions, ChartScales, ResolvedSeries, ResolveValueFn } from '../core/types'
import { getTextMeasureCtx } from '../utils/text-measure'
export type ValueLabelsMode = 'per-segment' | 'stack-total'
@@ -260,6 +260,39 @@ function rectsOverlap(a: Rect, b: Rect, gap: number): boolean {
return a.left < b.right + gap && a.right + gap > b.left && a.top < b.bottom + gap && a.bottom + gap > b.top
}
+function fitsWithinWrapper(c: Candidate, above: boolean, dimensions: ChartDimensions, isHorizontal: boolean): boolean {
+ if (isHorizontal) {
+ const left = above ? c.x : c.x - c.width
+ return left >= 0 && left + c.width <= dimensions.width
+ }
+ const top = above ? c.y - LABEL_HEIGHT : c.y
+ return top >= 0 && top + LABEL_HEIGHT <= dimensions.height
+}
+
+// If the default placement (above/right of the bar) would push the label past the chart
+// wrapper edge — which has `overflow: hidden` — flip it inside the bar instead. Avoids
+// reserving global plot-area headroom (which would shrink every chart vertically) for a
+// case that only hits when a bar reaches the axis top/right. Only flips when the flipped
+// position actually fits — if neither side fits, keep the original choice.
+function flipClippedCandidates(
+ candidates: Candidate[],
+ dimensions: ChartDimensions,
+ isHorizontal: boolean
+): Candidate[] {
+ for (const c of candidates) {
+ if (c.centerAnchor) {
+ continue
+ }
+ if (fitsWithinWrapper(c, c.above, dimensions, isHorizontal)) {
+ continue
+ }
+ if (fitsWithinWrapper(c, !c.above, dimensions, isHorizontal)) {
+ c.above = !c.above
+ }
+ }
+ return candidates
+}
+
function applyCollisionAvoidance(candidates: Candidate[], minGap: number, isHorizontal: boolean): Candidate[] {
if (candidates.length === 0) {
return candidates
@@ -354,7 +387,7 @@ export function ValueLabels({
minGap = 4,
mode = 'per-segment',
}: ValueLabelsProps): React.ReactElement | null {
- const { series, scales, labels, theme, resolvePositionValue, axis } = useChartLayout()
+ const { series, scales, labels, theme, resolvePositionValue, axis, dimensions } = useChartLayout()
const { hoverIndex } = useChartHover()
const isHorizontal = axis.orientation === 'horizontal'
const isPercent = axis.isPercent
@@ -364,20 +397,24 @@ export function ValueLabels({
const visible = useMemo(
() =>
applyCollisionAvoidance(
- buildCandidates({
- series,
- labels,
- scales,
- resolvePositionValue,
- valueFormatter: formatter,
- isHorizontal,
- mode,
- isPercent,
- }),
+ flipClippedCandidates(
+ buildCandidates({
+ series,
+ labels,
+ scales,
+ resolvePositionValue,
+ valueFormatter: formatter,
+ isHorizontal,
+ mode,
+ isPercent,
+ }),
+ dimensions,
+ isHorizontal
+ ),
minGap,
isHorizontal
),
- [series, labels, scales, resolvePositionValue, formatter, minGap, isHorizontal, mode, isPercent]
+ [series, labels, scales, resolvePositionValue, formatter, minGap, isHorizontal, mode, isPercent, dimensions]
)
// Skip the lift when a dataIndex has labels at multiple distinct x positions
diff --git a/products/product_analytics/frontend/insights/trends/StickinessLineChart/StickinessLineChart.test.tsx b/products/product_analytics/frontend/insights/trends/StickinessLineChart/StickinessLineChart.test.tsx
index 63e1ecd8e841..f70197e088a3 100644
--- a/products/product_analytics/frontend/insights/trends/StickinessLineChart/StickinessLineChart.test.tsx
+++ b/products/product_analytics/frontend/insights/trends/StickinessLineChart/StickinessLineChart.test.tsx
@@ -30,9 +30,7 @@ describe('StickinessLineChart', () => {
it('renders the chart from a StickinessQuery with one series', async () => {
renderInsight({ query: buildStickinessQuery(), featureFlags: HOG_CHARTS_FLAG })
- await waitFor(() => {
- expect(screen.getByRole('img', { name: /chart with 1 data series/i })).toBeInTheDocument()
- })
+ await screen.findByRole('img', { name: /chart with 1 data series/i }, { timeout: 3000 })
})
})
diff --git a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.stories.tsx b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.stories.tsx
index d1564ac6a1f4..86853a63280b 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.stories.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.stories.tsx
@@ -113,11 +113,19 @@ const lifecycleSeries = (status: 'new' | 'returning' | 'resurrecting' | 'dormant
status,
})
-function lifecycleInsight(stacked: boolean, id: number, shortId: string): object {
+interface LifecycleInsightOpts {
+ stacked: boolean
+ id: number
+ shortId: string
+ name?: string
+ showValuesOnSeries?: boolean
+}
+
+function lifecycleInsight({ stacked, id, shortId, name, showValuesOnSeries }: LifecycleInsightOpts): object {
return {
id,
short_id: shortId,
- name: stacked ? 'Lifecycle stacked' : 'Lifecycle unstacked',
+ name: name ?? (stacked ? 'Lifecycle stacked' : 'Lifecycle unstacked'),
derived_name: 'Lifecycle',
filters: {},
last_refresh: '2023-07-11T12:00:00Z',
@@ -148,7 +156,7 @@ function lifecycleInsight(stacked: boolean, id: number, shortId: string): object
interval: 'day',
kind: 'LifecycleQuery',
series: [{ event: '$pageview', kind: 'EventsNode', math: 'total', name: '$pageview' }],
- lifecycleFilter: { stacked },
+ lifecycleFilter: { stacked, showValuesOnSeries },
version: 2,
},
full: true,
@@ -157,9 +165,41 @@ function lifecycleInsight(stacked: boolean, id: number, shortId: string): object
}
export const Stacked: Story = {
- render: () => <LifecycleStory insightFixture={lifecycleInsight(true, 300, 'lifecycleStacked')} />,
+ render: () => (
+ <LifecycleStory insightFixture={lifecycleInsight({ stacked: true, id: 300, shortId: 'lifecycleStacked' })} />
+ ),
}
export const Unstacked: Story = {
- render: () => <LifecycleStory insightFixture={lifecycleInsight(false, 301, 'lifecycleUnstacked')} />,
+ render: () => (
+ <LifecycleStory insightFixture={lifecycleInsight({ stacked: false, id: 301, shortId: 'lifecycleUnstacked' })} />
+ ),
+}
+
+export const StackedWithValuesOnSeries: Story = {
+ render: () => (
+ <LifecycleStory
+ insightFixture={lifecycleInsight({
+ stacked: true,
+ id: 302,
+ shortId: 'lifecycleStackedValues',
+ name: 'Lifecycle stacked (values on series)',
+ showValuesOnSeries: true,
+ })}
+ />
+ ),
+}
+
+export const UnstackedWithValuesOnSeries: Story = {
+ render: () => (
+ <LifecycleStory
+ insightFixture={lifecycleInsight({
+ stacked: false,
+ id: 303,
+ shortId: 'lifecycleUnstackedValues',
+ name: 'Lifecycle unstacked (values on series)',
+ showValuesOnSeries: true,
+ })}
+ />
+ ),
}
diff --git a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx
index 5e8e840e6652..5f1febcad78b 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx
@@ -4,6 +4,7 @@ import { useCallback, useMemo } from 'react'
import { buildTheme } from 'lib/charts/utils/theme'
import { TimeSeriesBarChart } from 'lib/hog-charts'
import type { PointClickData, TimeSeriesBarChartConfig, TooltipContext } from 'lib/hog-charts'
+import { formatAggregationAxisValue } from 'scenes/insights/aggregationAxisFormat'
import { InsightEmptyState } from 'scenes/insights/EmptyStates'
import { insightLogic } from 'scenes/insights/insightLogic'
import type { SeriesDatum } from 'scenes/insights/InsightTooltip/insightTooltipUtils'
@@ -58,6 +59,7 @@ export function TrendsLifecycleChart({ context, inSharedMode = false }: TrendsLi
formula,
hasPersonsModal,
querySource,
+ showValuesOnSeries,
} = useValues(trendsDataLogic(insightProps))
const { timezone, weekStartDay, baseCurrency } = useValues(teamLogic)
@@ -75,6 +77,11 @@ export function TrendsLifecycleChart({ context, inSharedMode = false }: TrendsLi
return { series: lifecycleSeries, labels: currentPeriodResult?.labels ?? EMPTY_LABELS }
}, [indexedResults, currentPeriodResult?.labels])
+ const valueLabelFormatter = useCallback(
+ (value: number) => formatAggregationAxisValue(trendsFilter, value, baseCurrency),
+ [trendsFilter, baseCurrency]
+ )
+
const timeSeriesConfig: TimeSeriesBarChartConfig = useMemo(
() =>
buildTrendsLifecycleConfig({
@@ -85,9 +92,20 @@ export function TrendsLifecycleChart({ context, inSharedMode = false }: TrendsLi
interval,
timezone,
allDays: currentPeriodResult?.days ?? [],
+ valueLabels: showValuesOnSeries ? { formatter: valueLabelFormatter } : false,
tooltip: LIFECYCLE_TOOLTIP_CONFIG,
}),
- [trendsFilter, baseCurrency, isStacked, yAxisScaleType, interval, timezone, currentPeriodResult?.days]
+ [
+ trendsFilter,
+ baseCurrency,
+ isStacked,
+ yAxisScaleType,
+ interval,
+ timezone,
+ currentPeriodResult?.days,
+ showValuesOnSeries,
+ valueLabelFormatter,
+ ]
)
const canHandleClick = !!context?.onDataPointClick || !!hasPersonsModal
diff --git a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.test.ts b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.test.ts
index 27fac8cd6f98..442a6951936a 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.test.ts
+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.test.ts
@@ -141,6 +141,18 @@ describe('buildTrendsLifecycleConfig', () => {
const cfg = buildTrendsLifecycleConfig({ isStacked: true, tooltip })
expect(cfg.tooltip).toEqual(tooltip)
})
+
+ it.each([
+ { input: true, expected: true },
+ { input: false, expected: false },
+ {
+ input: { formatter: (v: number) => `${v}!` },
+ expected: { formatter: expect.any(Function) },
+ },
+ ])('forwards valueLabels=$input to the config', ({ input, expected }) => {
+ const cfg = buildTrendsLifecycleConfig({ isStacked: true, valueLabels: input })
+ expect(cfg.valueLabels).toEqual(expected)
+ })
})
describe('shortenLifecycleLabel', () => {
diff --git a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.ts b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.ts
index 07fa1755e277..39ce54975dbc 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.ts
+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/trendsLifecycleChartTransforms.ts
@@ -73,6 +73,7 @@ export interface BuildTrendsLifecycleConfigOpts {
interval?: IntervalType | null
timezone?: string
allDays?: string[]
+ valueLabels?: TimeSeriesBarChartConfig['valueLabels']
tooltip?: TimeSeriesBarChartConfig['tooltip']
}
@@ -88,6 +89,7 @@ export function buildTrendsLifecycleConfig(opts: BuildTrendsLifecycleConfigOpts)
allDays: opts.allDays ?? [],
},
yAxis,
+ valueLabels: opts.valueLabels,
barLayout: opts.isStacked ? 'stacked' : 'grouped',
// Only meaningful in stacked layout — dormant stacks below 0.
divergingStack: opts.isStacked,