: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
feat(insights): show the total in the stacked bar tooltip (#114037)
Sean Mallia6731aa6c21fa
diff --git a/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsx b/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsxindex 0f6599713cb7..76144c325b0b 100644--- a/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsx+++ b/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsx@@ -65,6 +65,8 @@ export interface InsightSeriesTooltipProps<Meta extends InsightSeriesMetaBase> {sortedByValue?: boolean/** Hide rows whose value is exactly 0 (e.g. absent lifecycle statuses). */hideZeroRows?: boolean+ /** Append a total row that sums the series at the hovered point (stacked bars). */+ showTotal?: boolean/** Override the default "click to view X" footer, for charts whose click goes somewhere other than persons. */footerOverride?: React.ReactNode}@@ -233,6 +235,7 @@ export function InsightSeriesTooltip<Meta extends InsightSeriesMetaBase>({renderSeriesOverride,sortedByValue = true,hideZeroRows,+ showTotal,footerOverride,}: InsightSeriesTooltipProps<Meta>): React.ReactElement {const { formatPropertyValueForDisplay } = useValues(propertyDefinitionsModel)@@ -394,6 +397,7 @@ export function InsightSeriesTooltip<Meta extends InsightSeriesMetaBase>({{...context}sortedByValue={sortedByValue}hideZeroRows={hideZeroRows}+ showTotal={showTotal}showHeader={showHeader !== false}labelFormatter={labelFormatter}labelRenderer={labelRenderer}diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsx b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsxindex a43f32f506df..9c4ef42de7ac 100644--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsx@@ -101,6 +101,24 @@ describe('TrendsBarChart (ActionsBar)', () => {expect(tooltip.row('Pageview')).toContain('134')expect(tooltip.row('Napped')).toContain('5')expect(tooltip.row('Napped')).not.toContain('139')+ expect(chart.getTooltip()?.querySelector('[data-attr="hog-chart-tooltip-total"]')).toBeNull()+ })++ it('stacked breakdown tooltip shows the total of the stack', async () => {+ // Napped by hedgehog at index 3: Spike=4, Bramble=1, Thistle=2, Conker=0, Prickles=1.+ renderInsight({+ query: trendsBar({+ series: [{ kind: NodeKind.EventsNode, event: 'Napped', name: 'Napped' }],+ breakdownFilter: { breakdown: 'hedgehog', breakdown_type: 'event' },+ }),+ })+ await screen.findByLabelText(/chart with/i, undefined, { timeout: 5000 })++ await chart.hoverTooltip(3)++ expect(chart.getTooltip()?.querySelector('[data-attr="hog-chart-tooltip-total"]')).toHaveTextContent(+ /^Total\s*8$/+ )})it('opens the persons modal on click for a single series', async () => {@@ -209,6 +227,7 @@ describe('TrendsBarChart (ActionsBar)', () => {const tooltip = await chart.hoverTooltip(2)expect(tooltip.row('Pageview')).toMatch(/%/)+ expect(chart.getTooltip()?.querySelector('[data-attr="hog-chart-tooltip-total"]')).toBeNull()})})diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsxindex e039ea56a413..d5329e7ae897 100644--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx@@ -27,7 +27,7 @@ import { groupsModel } from '~/models/groupsModel'import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'import { InsightVizNode } from '~/queries/schema/schema-general'import { QueryContext } from '~/queries/types'-import { getStackBreakdownValues } from '~/queries/utils'+import { getStackBreakdownValues, hasBreakdownFilter } from '~/queries/utils'import { ChartDisplayType } from '~/types'import { trendsDataLogic } from 'products/product_analytics/frontend/insights/trends/trendsDataLogic'@@ -118,6 +118,7 @@ export function TrendsBarChart({showValuesOnSeries,showMultipleYAxes,isSingleSeriesDefinition,+ compareFilter,} = useValues(trendsDataLogic(insightProps))const { timezone, weekStartDay, baseCurrency } = useValues(teamLogic)const { aggregationLabel } = useValues(groupsModel)@@ -135,6 +136,14 @@ export function TrendsBarChart({// Per-series y-axes are only meaningful for grouped (unstacked) bars — stacked layouts share// one axis. Mirrors the legacy ActionsLineGraph, which assigns y0/y1/… per dataset.const applyMultipleYAxes = !!showMultipleYAxes && isGrouped+ // Only a breakdown stack splits one bar into parts of a whole. A percent stack always sums to+ // 100%, and a compare stack would add two periods together.+ const showTooltipTotal =+ hasBreakdownFilter(breakdownFilter) &&+ !isAggregated &&+ !isGrouped &&+ !isPercentStackView &&+ !compareFilter?.compareconst resolvedGroupTypeLabel = resolveGroupTypeLabel(labelGroupType, aggregationLabel, context?.groupTypeLabel)@@ -404,6 +413,7 @@ export function TrendsBarChart({onRowClick,showHeader: isAggregated ? (false as const) : undefined,sortedByValue: false,+ showTotal: showTooltipTotal,}return <InsightSeriesTooltip {...sharedProps} />},@@ -421,6 +431,7 @@ export function TrendsBarChart({canHandleClick,clickDeps,isAggregated,+ showTooltipTotal,])