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): resolve hovered segment in stacked bar value tooltip (#67896)

Sam Pennington19e0045a8963
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx
index 295299934642..dbaf3274f42b 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx
@@ -51,6 +51,7 @@ import {
buildTrendsBarAggregatedSeries,
buildTrendsBarTimeSeries,
buildTrendsBarTimeSeriesConfig,
+ pickAggregatedTooltipSeriesData,
} from './trendsBarChartTransforms'
interface TrendsBarChartProps {
@@ -367,11 +368,10 @@ export function TrendsBarChart({
const renderTooltip = useCallback(
(ctx: TooltipContext<TrendsSeriesMeta>) => {
- // BarTooltip already put the cursor-visible segment at seriesData[0] — keep just that.
const tooltipCtx: TooltipContext<TrendsSeriesMeta> = isAggregated
? {
...ctx,
- seriesData: ctx.seriesData.slice(0, 1),
+ seriesData: pickAggregatedTooltipSeriesData(ctx),
}
: ctx
const onRowClick = canHandleClick
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts
index aef9da40aa6d..36d19e5f2ef3 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts
+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts
@@ -1,3 +1,5 @@
+import type { Series } from '@posthog/quill-charts'
+
import { hexToRGBA } from 'lib/utils/colors'
import type { CurrencyCode, GoalLine as SchemaGoalLine, TrendsFilter } from '~/queries/schema/schema-general'
@@ -7,6 +9,7 @@ import {
buildTrendsBarChartModel,
buildTrendsBarTimeSeries,
buildTrendsBarTimeSeriesConfig,
+ pickAggregatedTooltipSeriesData,
type TrendsBarResultLike,
} from './trendsBarChartTransforms'
@@ -266,6 +269,28 @@ describe('buildTrendsBarAggregatedSeries', () => {
})
})
+describe('pickAggregatedTooltipSeriesData', () => {
+ const entry = (key: string, value: number): { series: Series; value: number; color: string } => ({
+ series: { key, label: key, data: [value] },
+ value,
+ color: RED,
+ })
+ const seriesData = [entry('0', 11), entry('1', 4), entry('2', 2)]
+
+ it.each([
+ { name: 'resolves the hovered segment by key, not the first entry', hoveredSeriesKey: '1', expected: ['1'] },
+ { name: 'falls back to the first entry without a hovered key', hoveredSeriesKey: undefined, expected: ['0'] },
+ { name: 'falls back to the first entry for an unknown key', hoveredSeriesKey: 'missing', expected: ['0'] },
+ ])('$name', ({ hoveredSeriesKey, expected }) => {
+ const picked = pickAggregatedTooltipSeriesData({ hoveredSeriesKey, seriesData })
+ expect(picked.map((e) => e.series.key)).toEqual(expected)
+ })
+
+ it('returns no entries for empty seriesData', () => {
+ expect(pickAggregatedTooltipSeriesData({ hoveredSeriesKey: undefined, seriesData: [] })).toEqual([])
+ })
+})
+
describe('buildTrendsBarTimeSeriesConfig', () => {
it.each([
{ isPercentStackView: false, isGrouped: false, expected: 'stacked', expectedDiverging: true },
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts
index 173d8cc77820..3c0f552fc420 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts
+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts
@@ -1,5 +1,5 @@
import { DEFAULT_Y_AXIS_ID, normalizeAxisLabel } from '@posthog/quill-charts'
-import type { Series, TimeInterval, TimeSeriesBarChartConfig, YAxisConfig } from '@posthog/quill-charts'
+import type { Series, TimeInterval, TimeSeriesBarChartConfig, TooltipContext, YAxisConfig } from '@posthog/quill-charts'
import { COMPARE_PREVIOUS_DIM_OPACITY, dimHexColor } from '../shared/compareDimming'
import { schemaGoalLinesToConfigs } from '../shared/goalLinesAdapter'
@@ -212,3 +212,15 @@ export function buildTrendsBarAggregatedSeries<R extends TrendsBarResultLike, M
})
return { series, labels, displayLabels }
}
+
+/** The aggregated tooltip describes one bar/segment. Stacked-breakdown mode keeps every sparse
+ * segment in `seriesData` in declaration order, so resolve the hovered one by key — `[0]` is
+ * another segment's zero cell. Without a hovered key, fall back to the first entry. */
+export function pickAggregatedTooltipSeriesData<M>(
+ ctx: Pick<TooltipContext<M>, 'hoveredSeriesKey' | 'seriesData'>
+): TooltipContext<M>['seriesData'] {
+ const hovered = ctx.hoveredSeriesKey
+ ? ctx.seriesData.find((entry) => entry.series.key === ctx.hoveredSeriesKey)
+ : undefined
+ return hovered ? [hovered] : ctx.seriesData.slice(0, 1)
+}