: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): keep compare rows in their own bands on aggregated horizontal bar (#58740)
Sam Pennington094e75bd3e02
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 60c99a903a03..3d749ef9ad68 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -1848,6 +1848,10 @@ snapshots:hash: v1.k794b7964.d669cc7bd939e7cd9bae4003a228e194a6c383d827feb04baf491a89dfc0b698.eD11DZ4cr8p1GMBdBaTW1doXInEVj9CIhnyMP8RNpFoinsights-insightstable--is-legend--light:hash: v1.k794b7964.d9eec920b0b2f3c9ea87c9e0d2b298bc37339217e4211d0cf1a147630f09b75d.xGcJlQO8pvoNnHyoMNZ4F58uqKh5PDg_UiGlO5bAMn8+ insights-trendsbarchart--aggregated-compare-breakdown--dark:+ hash: v1.k794b7964.665af86c19c56e64090968d389ad70f6c31723e48a5c33dd09471fd7f7803ee1.t7lftFAyNVc_TMUt4thz6VWm9dbx1fVSXYwzEhbKYBw+ insights-trendsbarchart--aggregated-compare-breakdown--light:+ hash: v1.k794b7964.5af2e4097a7ae88260510ae1241589c25aa3bd21ab7700f9e544b4222a307382.QRE1QgbF1lgcnLkjiCYIT9rEgCUVOegdsbSOeZ77Acwinsights-trendsbarchart--compare--dark:hash: v1.k794b7964.68689d02aa245c2ccd04888b1c2bc578136eda4117f2fff911bde3f5c276b9da.svBnhEDDJ61_Vp6T3AUEjT3wlS2aamf9o-YNooKTRlwinsights-trendsbarchart--compare--light:diff --git a/frontend/src/scenes/trends/viz/shared/TrendsTooltip.tsx b/frontend/src/scenes/trends/viz/shared/TrendsTooltip.tsxindex cec1de5b8719..9ce08d8f491f 100644--- a/frontend/src/scenes/trends/viz/shared/TrendsTooltip.tsx+++ b/frontend/src/scenes/trends/viz/shared/TrendsTooltip.tsx@@ -27,6 +27,7 @@ interface TrendsTooltipProps {groupTypeLabel?: stringformatCompareLabel?: (label: string, dateLabel?: string) => stringonRowClick?: (datum: SeriesDatum) => void+ showHeader?: boolean}/** Bridges hog-charts TooltipContext to the legacy InsightTooltip.@@ -46,6 +47,7 @@ export function TrendsTooltip({groupTypeLabel,formatCompareLabel,onRowClick,+ showHeader,}: TrendsTooltipProps): React.ReactElement {const seriesData = useMemo<SeriesDatum[]>(() =>@@ -128,6 +130,7 @@ export function TrendsTooltip({renderCount={renderCount}onRowClick={onRowClick}hideInspectActorsSection={!onRowClick}+ showHeader={showHeader}/>)}diff --git a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.stories.tsx b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.stories.tsxindex 4381eb73a660..611db7ceaca3 100644--- a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.stories.tsx+++ b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.stories.tsx@@ -224,3 +224,120 @@ const COMPARE_BAR_INSIGHT = {export const Compare: Story = {render: () => renderTrendsBarChart(COMPARE_BAR_INSIGHT),}++const AGGREGATED_COMPARE_BREAKDOWN_INSIGHT = {+ id: 201,+ short_id: 'barValueCompare',+ name: 'Browser breakdown compare',+ derived_name: 'Pageview count',+ filters: {},+ last_refresh: '2023-07-11T12:00:00Z',+ refreshing: false,+ saved: true,+ is_sample: false,+ description: '',+ tags: [],+ favorited: false,+ created_at: '2023-07-11T12:00:00Z',+ updated_at: '2023-07-11T12:00:00Z',+ last_modified_at: '2023-07-11T12:00:00Z',+ dashboards: [],+ dashboard_tiles: [],+ result: [+ {+ action: ACTION,+ label: 'Chrome',+ count: 0,+ aggregated_value: 900,+ data: [],+ labels: [],+ days: [],+ breakdown_value: 'Chrome',+ compare: true,+ compare_label: 'current',+ filter: {+ date_from: '2023-07-04T00:00:00Z',+ date_to: '2023-07-10T23:59:59Z',+ display: 'ActionsBarValue',+ insight: 'TRENDS',+ interval: 'day',+ },+ },+ {+ action: ACTION,+ label: 'Chrome',+ count: 0,+ aggregated_value: 600,+ data: [],+ labels: [],+ days: [],+ breakdown_value: 'Chrome',+ compare: true,+ compare_label: 'previous',+ filter: {+ date_from: '2023-06-27T00:00:00Z',+ date_to: '2023-07-03T23:59:59Z',+ display: 'ActionsBarValue',+ insight: 'TRENDS',+ interval: 'day',+ },+ },+ {+ action: ACTION,+ label: 'Safari',+ count: 0,+ aggregated_value: 500,+ data: [],+ labels: [],+ days: [],+ breakdown_value: 'Safari',+ compare: true,+ compare_label: 'current',+ filter: {+ date_from: '2023-07-04T00:00:00Z',+ date_to: '2023-07-10T23:59:59Z',+ display: 'ActionsBarValue',+ insight: 'TRENDS',+ interval: 'day',+ },+ },+ {+ action: ACTION,+ label: 'Safari',+ count: 0,+ aggregated_value: 350,+ data: [],+ labels: [],+ days: [],+ breakdown_value: 'Safari',+ compare: true,+ compare_label: 'previous',+ filter: {+ date_from: '2023-06-27T00:00:00Z',+ date_to: '2023-07-03T23:59:59Z',+ display: 'ActionsBarValue',+ insight: 'TRENDS',+ interval: 'day',+ },+ },+ ],+ query: {+ kind: 'InsightVizNode',+ source: {+ dateRange: { date_from: '2023-07-04', date_to: '2023-07-10' },+ filterTestAccounts: false,+ interval: 'day',+ kind: 'TrendsQuery',+ series: [{ event: '$pageview', kind: 'EventsNode', math: 'total', name: '$pageview' }],+ trendsFilter: { display: 'ActionsBarValue' },+ breakdownFilter: { breakdown: '$browser', breakdown_type: 'event' },+ compareFilter: { compare: true },+ version: 2,+ },+ full: true,+ },+}++export const AggregatedCompareBreakdown: Story = {+ render: () => renderTrendsBarChart(AGGREGATED_COMPARE_BREAKDOWN_INSIGHT),+}diff --git a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsx b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsxindex 6b6babf5262a..23aab2e746fc 100644--- a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsx+++ b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsx@@ -57,6 +57,13 @@ describe('TrendsBarChart (ActionsBar)', () => {expect(tooltip.row('Pageview')).toContain('134')})+ it('shows a date header in the tooltip', async () => {+ renderInsight({ query: trendsBar(), featureFlags: HOG_CHARTS_FLAG })++ const tooltip = await chart.hoverTooltip(2)+ expect(tooltip.title()).toMatch(/Jun/)+ })+it('opens the persons modal on click for a single series', async () => {renderInsight({ query: trendsBar(), featureFlags: HOG_CHARTS_FLAG })@@ -143,6 +150,13 @@ describe('TrendsBarChart (ActionsBarValue)', () => {})})+ it('omits the header from the tooltip', async () => {+ renderInsight({ query: aggregatedBar(), featureFlags: HOG_CHARTS_FLAG })++ const tooltip = await chart.hoverTooltip(0)+ expect(tooltip.title()).toBe('')+ })+it('opens the persons modal on click without resolving a day', async () => {renderInsight({ query: aggregatedBar(), featureFlags: HOG_CHARTS_FLAG })@@ -155,6 +169,19 @@ describe('TrendsBarChart (ActionsBarValue)', () => {expect(personsModal.title()).not.toMatch(/Wednesday/)})+ it('opens the persons modal on click in compare mode instead of pinning the tooltip', async () => {+ renderInsight({+ query: aggregatedBar({ compareFilter: { compare: true } }),+ featureFlags: HOG_CHARTS_FLAG,+ })++ await chart.clickAtIndex(0)++ await waitFor(() => {+ expect(personsModal.get()).toBeInTheDocument()+ })+ })+it('fires context.onDataPointClick without a day argument', async () => {// Per-band breakdown resolution is covered at the unit-handler level — the// hog-charts hover/click helpers don't yet handle horizontal axis-orientation, sodiff --git a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.tsx b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.tsxindex 699ee5c68a9f..3acf407e77f5 100644--- a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.tsx+++ b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.tsx@@ -46,7 +46,8 @@ interface TrendsBarChartProps {}const EMPTY_LABELS: string[] = []-const TOOLTIP_CONFIG = { pinnable: true, placement: 'top' as const }+const TIME_SERIES_TOOLTIP_CONFIG = { pinnable: true, placement: 'top' as const }+const AGGREGATED_TOOLTIP_CONFIG = { pinnable: false }const buildBarMeta = (r: IndexedTrendResult): TrendsSeriesMeta => ({action: r.action,@@ -159,7 +160,7 @@ export function TrendsBarChart({ context, inSharedMode = false }: TrendsBarChartallDays: currentPeriodResult?.days ?? [],goalLines,valueLabels: showValuesOnSeries ? { formatter: valueLabelFormatter } : false,- tooltip: TOOLTIP_CONFIG,+ tooltip: TIME_SERIES_TOOLTIP_CONFIG,}),[trendsFilter,@@ -184,7 +185,7 @@ export function TrendsBarChart({ context, inSharedMode = false }: TrendsBarChartconst aggregatedConfig: BarChartConfig = useMemo(() => ({showGrid: true,- tooltip: TOOLTIP_CONFIG,+ tooltip: AGGREGATED_TOOLTIP_CONFIG,yScaleType: yAxisScaleType === 'log10' ? 'log' : 'linear',axisOrientation: 'horizontal',barLayout: 'stacked',@@ -276,6 +277,7 @@ export function TrendsBarChart({ context, inSharedMode = false }: TrendsBarChartgroupTypeLabel={resolvedGroupTypeLabel}formatCompareLabel={context?.formatCompareLabel}onRowClick={onRowClick}+ showHeader={isAggregated ? false : undefined}/>)},diff --git a/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.test.ts b/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.test.tsindex 924f5e0db67d..1d3b2398eca0 100644--- a/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.test.ts+++ b/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.test.ts@@ -111,6 +111,31 @@ describe('buildTrendsBarAggregatedSeries', () => {expect(series.map((s) => s.color)).toEqual(colors)})+ it.each([+ {+ name: 'suffixes labels with compare_label so compare-against-previous rows get distinct bands',+ results: [+ { id: 'a', label: 'Microsoft Edge', compare_label: 'current', aggregated_value: 100 },+ { id: 'b', label: 'Microsoft Edge', compare_label: 'previous', aggregated_value: 80 },+ { id: 'c', label: 'Safari', compare_label: 'current', aggregated_value: 60 },+ ] satisfies Partial<TrendsBarResultLike>[],+ expected: ['Microsoft Edge - current', 'Microsoft Edge - previous', 'Safari - current'],+ },+ {+ name: 'leaves labels unchanged when compare_label is absent',+ results: [+ { id: 'a', label: 'Chrome', aggregated_value: 1 },+ { id: 'b', label: 'Safari', aggregated_value: 2 },+ ] satisfies Partial<TrendsBarResultLike>[],+ expected: ['Chrome', 'Safari'],+ },+ ])('$name', ({ results, expected }) => {+ const { labels } = buildTrendsBarAggregatedSeries(results.map(mkResult), { getColor: () => RED })+ expect(labels).toEqual(expected)+ // No duplicates — every band gets a unique d3 domain key.+ expect(new Set(labels).size).toBe(labels.length)+ })+it('drops hidden results so visible bars are densely packed', () => {const results = [mkResult({ id: 'a', label: 'A', aggregated_value: 1 }),diff --git a/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.ts b/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.tsindex 285a9a64d23c..6c6ca4387fa0 100644--- a/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.ts+++ b/frontend/src/scenes/trends/viz/trends-bar-chart/trendsBarChartTransforms.ts@@ -99,7 +99,11 @@ export function buildTrendsBarAggregatedSeries<R extends TrendsBarResultLike, M// Hidden results are dropped entirely — keeping them as `excluded` series would leave// a phantom band on the category axis with no bar.const visible = opts.getHidden ? results.filter((r, i) => !opts.getHidden!(r, i)) : results- const labels = visible.map((r) => r.label ?? '')+ // d3.scaleBand collapses duplicate domain entries — suffix compare rows so each gets its own band.+ const labels = visible.map((r) => {+ const base = r.label ?? ''+ return r.compare_label ? `${base} - ${r.compare_label}` : base+ })const n = visible.lengthconst series = visible.map((r, index) => {const data = new Array<number>(n).fill(0)