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 legend on lifecycle chart (#60459)

Sam Pennington8d769c811d5b
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 47a6835858f8..cc3574a0472d 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -537,21 +537,25 @@ snapshots:
components-hogcharts-legend--interactive--light:
hash: v1.k794b7964.82d79f7e0a07c188f597a89fbc0a479e1b9605d007ba6f5fb71087a312fc0764.kFDhmb2BkonZoo3bR5snWR-aN_yY6R2-utLZ5ify3_E
components-hogcharts-legend--layout-bottom--dark:
- hash: v1.k794b7964.6f34c5c79807ea26a631024458558c84093ab248138fbb7cba4d24deccda210d.xJ098X5I0o1k6yvWtmYXYJGlgsrbw5TpIN8C-osCojY
+ hash: v1.k794b7964.e0380d81940d37f3f340a3136f4f8d8594c6e9a6f628097f8b2cda3265d55f03.UBGvFKyD6buahJXp9ZamajRe8pCwlNlCdRwJrNBvx_c
components-hogcharts-legend--layout-bottom--light:
- hash: v1.k794b7964.ebb34148784fd49f208e59207fcfc3aa1476aeddd0af0cc3987543af514e6f0c.ygBJg6yMPl8-arZKotkxgO5XzM4AkWb_HsYkcTOkqes
+ hash: v1.k794b7964.8f97725453e51428ff9497991208f64c73fb309295eb7e53a1bf0bdc61b6573b.IVE1Embb1O4jo8mh0mCSH4F5D_FEgwIcKl8eAhFHNas
components-hogcharts-legend--layout-left--dark:
- hash: v1.k794b7964.e5ffa84c0c4612544185166fd7127e091f743d0a4e80e610021e9898cf68c16c.RWP2gB5lEXOaZJk2wYPvfCzAeDRDCBd59flzROZBj_E
+ hash: v1.k794b7964.75d89188a8dc1300f8134072f67052e812a3a98c67f2e45b7672421e9d9e7e4e.JF2aBZw3Go_PwlNxeprXqSoUNHcKn_zRlDjUj_umrJw
components-hogcharts-legend--layout-left--light:
- hash: v1.k794b7964.88db7c4f4fab4b0fee6b0be8b1508c91798270e69c17b507e046fc530dd2fafd.Yn1GAbiBehr5i16TVhPR01gSC2GpOfBnmo5LHnmCWqs
+ hash: v1.k794b7964.734434f8506a82bf4bfd795fe771a4dc4603dec1924b97426f1f723271b038f6.y3GVSkCayKaXlG9j9JKJRyCNbOD_l01nF8NxfRv2J2g
components-hogcharts-legend--layout-right--dark:
- hash: v1.k794b7964.501db0b97c4d897a816cf08afba0f837a54be6f3533dd253b8410f34c57528d5.iT0_3sNr9zqZPacv3XSvc7iFxAhpejg9l26-8ek1wCA
+ hash: v1.k794b7964.fad942fd3efcf8f4478e74615a46818e5ff2e0393d7b1ebd86f2d2f99a48584b.5JfPyKB6riVLkavCHA4BvBRYkGH-tsCEKgEp76IRrTg
components-hogcharts-legend--layout-right--light:
- hash: v1.k794b7964.81902d1fc7bc428f1455585a08aa7667b409fe0edbf56f269ad38b56e21c53e0.nXCrZLvGoncvjvDYQ0Iq7VmbmorY7jNA6e1qCirbzbQ
+ hash: v1.k794b7964.621179da1b3fb0a2f46af3ed66431ec287d5942ea5e3b832b1f77dfc7b1ccd74.H7UFItAwihph7mpstIAFBY_L1ifnKHLNoVTgfrWywTQ
components-hogcharts-legend--layout-top--dark:
- hash: v1.k794b7964.07957a22916e5a09eedba69d385c07646d97d3d9483b3fc8d56ae0fae6eea448.4uZJgPeHOc567hzQaIzohLOCyS3dXMJOKB-boXjbYVA
+ hash: v1.k794b7964.9c6096d910d7028a42eafafbcabb940d600f48494ac4e3b4d4af493786a65600.oKiBtPecHu-BKcMY7RdWZpfLV-3aztODvdNGTmaPK90
components-hogcharts-legend--layout-top--light:
- hash: v1.k794b7964.efd2345115e956ad9a0c4267248d90c40aade934d5dd896e10fad79f2afb425d.1JqrR7X4fPy38s0mKd20fHKfgLjrdwSNa05lG5GPcHA
+ hash: v1.k794b7964.9495377c22ab7b943c36924440a67bf311e7fa0f5a59486b7132bff9c92fd419.s9kNN2YJMVz--JssIjOo3sCnpC2ib7bHYQozYcg3sAw
+ components-hogcharts-legend--legend-hidden--dark:
+ hash: v1.k794b7964.ceab375cc7f0c1370d831bbf2a13032083f34e9e13092535117d339e9ea7fffb.4r1GIynFITt0-Q_eNyTBj8ZdveGtJtWmTSNAQcCXMiI
+ components-hogcharts-legend--legend-hidden--light:
+ hash: v1.k794b7964.53ae7d64bdd07ae8ed2f0c4403fea3733f06b78ed1f7ba40bc17dbbf71b0b3c3.Hfsd1a5oo1zsMLiXn8z-wsq2MynuxREukeIdluYETUM
components-hogcharts-legend--many-items-wraps--dark:
hash: v1.k794b7964.f6c2b82b8d93c25d9c21b9afc1d914d041138d25836485cf2d76a577d55a37c5.Ut_YYVfVrtgFs0gV16xnW-pxSADhDhx9tsfA_XEPirQ
components-hogcharts-legend--many-items-wraps--light:
@@ -2084,6 +2088,10 @@ snapshots:
hash: v1.k794b7964.0c556ee6fdcab5b92dfdc52f88803303ef884a0ab729ef82a4c6da51acb24c3f.QOa-WyWqTtPEFLJkamvn4q5CELTHgIvThx2ldXpfR0I
insights-trendslifecyclechart--stacked--light:
hash: v1.k794b7964.a65059fedd22997962f3cca875571b75d337d4d4a35795e27c5154379a877e7c.ANRBq-VSdDBRCpI3opE7Mu4kXK2TL2MG7HwLnrSJHE4
+ insights-trendslifecyclechart--stacked-with-legend--dark:
+ hash: v1.k794b7964.34ee21c93cb09cc3cbd620bf960e4ea4553c38b56eca794f6f8ebe3fb645ba93.4sjTzpM-9CeL36Lb6yIbgNPV6jDX_sxBEU9IaMF8KIc
+ insights-trendslifecyclechart--stacked-with-legend--light:
+ hash: v1.k794b7964.60416255e840786210d0236e71ef9eb3771d0d1d73161355913bccbf72449afe.WVEhZOzCzFz_et6-07xYPEsrsNf64sF5BSPw8hASzYM
insights-trendslifecyclechart--stacked-with-values-on-series--dark:
hash: v1.k794b7964.bc4c1f78bb5bc68955a459d215fca4bd5d459ea3992b5eca13ad40ee39ee4366.OX1_Hj6tCAyTwV7c24Xu-mTkSDv6PE_e0CofnkyQETM
insights-trendslifecyclechart--stacked-with-values-on-series--light:
@@ -2092,6 +2100,10 @@ snapshots:
hash: v1.k794b7964.a8cf1b1997745830adfb3ff10128cc607e058957c774fff1130fcb6ff404d866.krGwejVU2Th4ixNpXHRtz-e9X3orZ804ySTK-6yGkaI
insights-trendslifecyclechart--unstacked--light:
hash: v1.k794b7964.3417b88ceba60b38260452443223e486eac91f477061bc4662f76bb78f7c40e2.WOssmFNdeWS4-WAqpXRvxTO-Bpe_iL-lnRo-R0jHhCg
+ insights-trendslifecyclechart--unstacked-with-legend--dark:
+ hash: v1.k794b7964.c97d90ab0c373436972e47b4f34ecaae07be8497c52bbf06717aae714399d251.-UDtNIEVgoDj4eP2ORpqfdI7mI4QOpETAGTbvsdouJY
+ insights-trendslifecyclechart--unstacked-with-legend--light:
+ hash: v1.k794b7964.4cb9e27ef90ccb219404af7ef3eeab2430fbf687b4b0eaf723d1e33509c70668.rPDT6IL8vTnQdAImKGwbivsRJmeYnIqhcPM-iixJPsk
insights-trendslifecyclechart--unstacked-with-values-on-series--dark:
hash: v1.k794b7964.d11ad52519f19f8f938e0419e78f5c98a6097fa501cfea43d2baa3ba8aea3948.F_NC7gkGfnvJ6y7t6_cvfdk-dqMjcfJvhSYyg_Bn5bg
insights-trendslifecyclechart--unstacked-with-values-on-series--light:
diff --git a/frontend/src/lib/hog-charts/components/Legend/ChartLegend.test.tsx b/frontend/src/lib/hog-charts/components/Legend/ChartLegend.test.tsx
new file mode 100644
index 000000000000..1c8a26bbd72e
--- /dev/null
+++ b/frontend/src/lib/hog-charts/components/Legend/ChartLegend.test.tsx
@@ -0,0 +1,61 @@
+import { render } from '@testing-library/react'
+
+import { ChartLegend } from './ChartLegend'
+import type { LegendItem } from './Legend'
+
+const ITEMS: LegendItem[] = [
+ { key: 'a', label: 'A', color: '#000' },
+ { key: 'b', label: 'B', color: '#fff' },
+]
+
+describe('ChartLegend', () => {
+ it('renders children unwrapped when show is false', () => {
+ const { container } = render(
+ <ChartLegend show={false} items={ITEMS}>
+ <div data-attr="chart">C</div>
+ </ChartLegend>
+ )
+ expect(container.children).toHaveLength(1)
+ expect((container.firstChild as HTMLElement).getAttribute('data-attr')).toBe('chart')
+ expect(container.textContent).not.toContain('A')
+ })
+
+ it('renders children unwrapped when items is empty', () => {
+ const { container } = render(
+ <ChartLegend items={[]}>
+ <div data-attr="chart">C</div>
+ </ChartLegend>
+ )
+ expect(container.children).toHaveLength(1)
+ expect((container.firstChild as HTMLElement).getAttribute('data-attr')).toBe('chart')
+ })
+
+ it('renders legend and chart inside a flex wrapper when show is true', () => {
+ const { container } = render(
+ <ChartLegend show items={ITEMS} legendDataAttr="my-legend">
+ <div data-attr="chart">C</div>
+ </ChartLegend>
+ )
+ expect(container.textContent).toContain('A')
+ expect(container.textContent).toContain('B')
+ expect(container.querySelector('[data-attr="chart"]')).not.toBeNull()
+ expect(container.querySelector('[data-attr="my-legend"]')).not.toBeNull()
+ const outer = container.firstChild as HTMLElement
+ expect(outer.className).toContain('flex-1')
+ expect(outer.className).toContain('min-h-0')
+ })
+
+ it.each([
+ ['top', 'flex-col'],
+ ['bottom', 'flex-col'],
+ ['left', 'flex-row'],
+ ['right', 'flex-row'],
+ ] as const)('position=%s sets layout direction to %s', (position, direction) => {
+ const { container } = render(
+ <ChartLegend items={ITEMS} position={position}>
+ <div>C</div>
+ </ChartLegend>
+ )
+ expect((container.firstChild as HTMLElement).className).toContain(direction)
+ })
+})
diff --git a/frontend/src/lib/hog-charts/components/Legend/ChartLegend.tsx b/frontend/src/lib/hog-charts/components/Legend/ChartLegend.tsx
new file mode 100644
index 000000000000..cc9f26dfefe6
--- /dev/null
+++ b/frontend/src/lib/hog-charts/components/Legend/ChartLegend.tsx
@@ -0,0 +1,59 @@
+import React from 'react'
+
+import { ChartLegendLayout } from './ChartLegendLayout'
+import { Legend, type LegendItem } from './Legend'
+
+export interface ChartLegendProps {
+ /** When false, renders children with no wrapper and no legend. Defaults to true. */
+ show?: boolean
+ items: LegendItem[]
+ position?: 'top' | 'bottom' | 'left' | 'right'
+ align?: 'start' | 'center' | 'end'
+ gap?: number
+ onItemClick?: (key: string) => void
+ hiddenKeys?: string[]
+ className?: string
+ /** data-attr on the inner `<Legend>`. The outer layout wrapper has no data-attr. */
+ legendDataAttr?: string
+ children: React.ReactNode
+}
+
+export function ChartLegend({
+ show = true,
+ items,
+ position = 'top',
+ align = 'center',
+ gap,
+ onItemClick,
+ hiddenKeys,
+ className,
+ legendDataAttr,
+ children,
+}: ChartLegendProps): React.ReactElement {
+ if (!show || items.length === 0) {
+ return <>{children}</>
+ }
+ const orientation = position === 'left' || position === 'right' ? 'vertical' : 'horizontal'
+ // Bakes `flex-1 min-h-0` so consumers in a flex-col parent don't have to remember it.
+ const wrapperClassName = `flex-1 min-h-0 ${className ?? ''}`.trim()
+ return (
+ <ChartLegendLayout
+ legend={
+ <Legend
+ items={items}
+ orientation={orientation}
+ align={align}
+ onItemClick={onItemClick}
+ hiddenKeys={hiddenKeys}
+ dataAttr={legendDataAttr}
+ />
+ }
+ position={position}
+ align={align}
+ gap={gap}
+ className={wrapperClassName}
+ >
+ {children}
+ </ChartLegendLayout>
+ )
+}
diff --git a/frontend/src/lib/hog-charts/components/Legend/ChartLegendLayout.tsx b/frontend/src/lib/hog-charts/components/Legend/ChartLegendLayout.tsx
index feca7b584fb1..d286b7fa1d04 100644
--- a/frontend/src/lib/hog-charts/components/Legend/ChartLegendLayout.tsx
+++ b/frontend/src/lib/hog-charts/components/Legend/ChartLegendLayout.tsx
@@ -29,7 +29,8 @@ export function ChartLegendLayout({
const isRow = position === 'left' || position === 'right'
const legendFirst = position === 'top' || position === 'left'
const legendSlot = legend ? <div className="flex-none shrink-0">{legend}</div> : null
- const chartSlot = <div className="flex-1 min-w-0 min-h-0">{children}</div>
+ // `flex flex-col` so the inner chart's `flex: 1` resolves; `self-stretch` so the chart fills the cross axis regardless of `align` (which then only affects the legend).
+ const chartSlot = <div className="flex flex-col flex-1 min-w-0 min-h-0 self-stretch">{children}</div>
return (
<div
className={`flex min-w-0 min-h-0 ${isRow ? 'flex-row' : 'flex-col'} ${ALIGN_CLASS[align]} ${className ?? ''}`}
diff --git a/frontend/src/lib/hog-charts/components/Legend/Legend.stories.tsx b/frontend/src/lib/hog-charts/components/Legend/Legend.stories.tsx
index 9fb192a3d859..9d9aad4f750c 100644
--- a/frontend/src/lib/hog-charts/components/Legend/Legend.stories.tsx
+++ b/frontend/src/lib/hog-charts/components/Legend/Legend.stories.tsx
@@ -4,7 +4,7 @@ import { useMemo, useState } from 'react'
import { DAYS, SERIES } from '../../charts/time-series-fixtures'
import { TimeSeriesBarChart } from '../../charts/TimeSeriesBarChart/TimeSeriesBarChart'
import { Stage, useReactiveTheme } from '../../story-helpers'
-import { ChartLegendLayout } from './ChartLegendLayout'
+import { ChartLegend } from './ChartLegend'
import { Legend, type LegendItem } from './Legend'
import { legendItemsFromSeries } from './legendItemsFromSeries'
@@ -66,28 +66,31 @@ export const ManyItemsWraps: Story = {
),
}
-function ChartWithLegend({ position }: { position: 'top' | 'bottom' | 'left' | 'right' }): JSX.Element {
+function ChartLegendStory({
+ show = true,
+ position,
+}: {
+ show?: boolean
+ position: 'top' | 'bottom' | 'left' | 'right'
+}): JSX.Element {
const theme = useReactiveTheme()
- const isRow = position === 'left' || position === 'right'
const items = useMemo(() => legendItemsFromSeries(SERIES, theme), [theme])
return (
<Stage width={520} height={320}>
- <ChartLegendLayout
- legend={<Legend items={items} orientation={isRow ? 'vertical' : 'horizontal'} />}
- position={position}
- >
+ <ChartLegend show={show} items={items} position={position}>
<TimeSeriesBarChart
series={SERIES}
labels={DAYS}
theme={theme}
config={{ yAxis: { showGrid: true } }}
/>
- </ChartLegendLayout>
+ </ChartLegend>
</Stage>
)
}
-export const LayoutTop: Story = { render: () => <ChartWithLegend position="top" /> }
-export const LayoutBottom: Story = { render: () => <ChartWithLegend position="bottom" /> }
-export const LayoutLeft: Story = { render: () => <ChartWithLegend position="left" /> }
-export const LayoutRight: Story = { render: () => <ChartWithLegend position="right" /> }
+export const LayoutTop: Story = { render: () => <ChartLegendStory position="top" /> }
+export const LayoutBottom: Story = { render: () => <ChartLegendStory position="bottom" /> }
+export const LayoutLeft: Story = { render: () => <ChartLegendStory position="left" /> }
+export const LayoutRight: Story = { render: () => <ChartLegendStory position="right" /> }
+export const LegendHidden: Story = { render: () => <ChartLegendStory show={false} position="top" /> }
diff --git a/frontend/src/lib/hog-charts/index.ts b/frontend/src/lib/hog-charts/index.ts
index e5588ee1d7f7..13f73c2d4c74 100644
--- a/frontend/src/lib/hog-charts/index.ts
+++ b/frontend/src/lib/hog-charts/index.ts
@@ -118,6 +118,6 @@ export { ciRanges, linearRegression, movingAverage, trendLine } from './utils/st
// Generic UI primitives (no canvas) — composed alongside charts by adapters
export { Legend } from './components/Legend/Legend'
export type { LegendItem, LegendProps } from './components/Legend/Legend'
-export { ChartLegendLayout } from './components/Legend/ChartLegendLayout'
-export type { ChartLegendLayoutProps } from './components/Legend/ChartLegendLayout'
+export { ChartLegend } from './components/Legend/ChartLegend'
+export type { ChartLegendProps } from './components/Legend/ChartLegend'
export { legendItemsFromSeries } from './components/Legend/legendItemsFromSeries'
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 266786b3cf8f..85d181861387 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.stories.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.stories.tsx
@@ -119,9 +119,17 @@ interface LifecycleInsightOpts {
shortId: string
name?: string
showValuesOnSeries?: boolean
+ showLegend?: boolean
}
-function lifecycleInsight({ stacked, id, shortId, name, showValuesOnSeries }: LifecycleInsightOpts): object {
+function lifecycleInsight({
+ stacked,
+ id,
+ shortId,
+ name,
+ showValuesOnSeries,
+ showLegend,
+}: LifecycleInsightOpts): object {
return {
id,
short_id: shortId,
@@ -157,7 +165,7 @@ function lifecycleInsight({ stacked, id, shortId, name, showValuesOnSeries }: Li
interval: 'day',
kind: 'LifecycleQuery',
series: [{ event: '$pageview', kind: 'EventsNode', math: 'total', name: '$pageview' }],
- lifecycleFilter: { stacked, showValuesOnSeries },
+ lifecycleFilter: { stacked, showValuesOnSeries, showLegend },
version: 2,
},
full: true,
@@ -204,3 +212,31 @@ export const UnstackedWithValuesOnSeries: Story = {
/>
),
}
+
+export const StackedWithLegend: Story = {
+ render: () => (
+ <LifecycleStory
+ insightFixture={lifecycleInsight({
+ stacked: true,
+ id: 304,
+ shortId: 'lifecycleStackedLegend',
+ name: 'Lifecycle stacked (with legend)',
+ showLegend: true,
+ })}
+ />
+ ),
+}
+
+export const UnstackedWithLegend: Story = {
+ render: () => (
+ <LifecycleStory
+ insightFixture={lifecycleInsight({
+ stacked: false,
+ id: 305,
+ shortId: 'lifecycleUnstackedLegend',
+ name: 'Lifecycle unstacked (with legend)',
+ showLegend: true,
+ })}
+ />
+ ),
+}
diff --git a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.test.tsx b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.test.tsx
index 8935f59b7ad3..1afdd1efcef9 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.test.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.test.tsx
@@ -131,6 +131,31 @@ describe('TrendsLifecycleChart', () => {
})
})
+ it('renders the legend items in the same order as the rendered series', async () => {
+ renderInsight({
+ query: buildLifecycleQuery({ lifecycleFilter: { showLegend: true } }) as unknown as InsightQuery,
+ featureFlags: HOG_CHARTS_FLAG,
+ mocks: { additionalMockResponses: lifecycleMocks },
+ })
+
+ await screen.findByTestId('trend-lifecycle-graph')
+ const legend = await screen.findByTestId('trend-lifecycle-legend')
+ // Status order must match buildTrendsLifecycleSeries' sort: dormant → returning → resurrecting → new.
+ const labels = Array.from(legend.children).map((el) => el.textContent?.trim())
+ expect(labels).toEqual(['Dormant', 'Returning', 'Resurrecting', 'New'])
+ })
+
+ it('omits the legend when showLegend is not set', async () => {
+ renderInsight({
+ query: buildLifecycleQuery() as unknown as InsightQuery,
+ featureFlags: HOG_CHARTS_FLAG,
+ mocks: { additionalMockResponses: lifecycleMocks },
+ })
+
+ await screen.findByTestId('trend-lifecycle-graph')
+ expect(screen.queryByTestId('trend-lifecycle-legend')).not.toBeInTheDocument()
+ })
+
it('falls back to the legacy renderer when the flag is off', async () => {
renderInsight({
query: buildLifecycleQuery() as unknown as InsightQuery,
diff --git a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx
index 5f1febcad78b..36175d139cfa 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx
@@ -2,7 +2,7 @@ import { useValues } from 'kea'
import { useCallback, useMemo } from 'react'
import { buildTheme } from 'lib/charts/utils/theme'
-import { TimeSeriesBarChart } from 'lib/hog-charts'
+import { ChartLegend, TimeSeriesBarChart, legendItemsFromSeries } 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'
@@ -60,6 +60,7 @@ export function TrendsLifecycleChart({ context, inSharedMode = false }: TrendsLi
hasPersonsModal,
querySource,
showValuesOnSeries,
+ showLegend,
} = useValues(trendsDataLogic(insightProps))
const { timezone, weekStartDay, baseCurrency } = useValues(teamLogic)
@@ -77,6 +78,8 @@ export function TrendsLifecycleChart({ context, inSharedMode = false }: TrendsLi
return { series: lifecycleSeries, labels: currentPeriodResult?.labels ?? EMPTY_LABELS }
}, [indexedResults, currentPeriodResult?.labels])
+ const legendItems = useMemo(() => legendItemsFromSeries(series, theme), [series, theme])
+
const valueLabelFormatter = useCallback(
(value: number) => formatAggregationAxisValue(trendsFilter, value, baseCurrency),
[trendsFilter, baseCurrency]
@@ -191,18 +194,22 @@ export function TrendsLifecycleChart({ context, inSharedMode = false }: TrendsLi
const annotationsDates = currentPeriodResult?.days ?? []
return (
- <TimeSeriesBarChart<TrendsSeriesMeta>
- series={series}
- labels={labels}
- config={timeSeriesConfig}
- theme={theme}
- tooltip={renderTooltip}
- onPointClick={canHandleClick ? onPointClick : undefined}
- className="BarGraph"
- dataAttr="trend-lifecycle-graph"
- onError={handleChartError}
- >
- {showAnnotations && <AnnotationsLayer insightNumericId={insight.id || 'new'} dates={annotationsDates} />}
- </TimeSeriesBarChart>
+ <ChartLegend show={!!showLegend} items={legendItems} position="top" legendDataAttr="trend-lifecycle-legend">
+ <TimeSeriesBarChart<TrendsSeriesMeta>
+ series={series}
+ labels={labels}
+ config={timeSeriesConfig}
+ theme={theme}
+ tooltip={renderTooltip}
+ onPointClick={canHandleClick ? onPointClick : undefined}
+ className="BarGraph"
+ dataAttr="trend-lifecycle-graph"
+ onError={handleChartError}
+ >
+ {showAnnotations && (
+ <AnnotationsLayer insightNumericId={insight.id || 'new'} dates={annotationsDates} />
+ )}
+ </TimeSeriesBarChart>
+ </ChartLegend>
)
}