: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 share-of-total on horizontal bar and fix pie tooltip percent (#58278)
Christiaan Hendriksen16e871d79cf2
diff --git a/frontend/src/scenes/insights/aggregationAxisFormat.ts b/frontend/src/scenes/insights/aggregationAxisFormat.tsindex 0338301a4a25..d9f28731bc3c 100644--- a/frontend/src/scenes/insights/aggregationAxisFormat.ts+++ b/frontend/src/scenes/insights/aggregationAxisFormat.ts@@ -101,6 +101,29 @@ export const formatPercentStackAxisValue = (return formatAggregationAxisValue(trendsFilter, value, currency)}+// Formats a value and appends its share of the total, e.g. "1,234 (37.5%)".+// Skips the share-of-total suffix when the axis is already formatted as a percentage+// to avoid confusing output like "37% (60%)" (metric value vs share of total).+export const formatAggregationAxisValueWithShareOfTotal = (+ trendsFilter: TrendsFilter | null | undefined | Partial<TrendsFilterType>,+ value: number | string,+ total: number,+ currency?: CurrencyCode+): string => {+ const formatted = formatAggregationAxisValue(trendsFilter, value, currency)+ const aggregationAxisFormat =+ (trendsFilter as TrendsFilter)?.aggregationAxisFormat ??+ (trendsFilter as Partial<TrendsFilterType>)?.aggregation_axis_format+ if (aggregationAxisFormat === 'percentage' || aggregationAxisFormat === 'percentage_scaled') {+ return formatted+ }+ if (!total) {+ return formatted+ }+ const shareOfTotal = parseFloat(((Number(value) / total) * 100).toFixed(1))+ return `${formatted} (${shareOfTotal}%)`+}+export const axisLabel = (chartDisplayType: ChartDisplayType | null | undefined): string => {switch (chartDisplayType) {case ChartDisplayType.ActionsLineGraph:diff --git a/frontend/src/scenes/insights/aggregationAxisFormats.test.ts b/frontend/src/scenes/insights/aggregationAxisFormats.test.tsindex e61dfceb9a4e..1fb48dd20153 100644--- a/frontend/src/scenes/insights/aggregationAxisFormats.test.ts+++ b/frontend/src/scenes/insights/aggregationAxisFormats.test.ts@@ -1,4 +1,7 @@-import { formatAggregationAxisValue } from 'scenes/insights/aggregationAxisFormat'+import {+ formatAggregationAxisValue,+ formatAggregationAxisValueWithShareOfTotal,+} from 'scenes/insights/aggregationAxisFormat'import { CurrencyCode } from '~/queries/schema/schema-general'import { FilterType } from '~/types'@@ -68,3 +71,65 @@ describe('formatAggregationAxisValue', () => {})})})++describe('formatAggregationAxisValueWithShareOfTotal', () => {+ const shareTestcases = [+ { candidate: 250, total: 1000, filters: {}, expected: '250 (25%)' },+ { candidate: 333, total: 1000, filters: {}, expected: '333 (33.3%)' },+ { candidate: 1000, total: 1000, filters: {}, expected: '1,000 (100%)' },+ {+ candidate: 250,+ total: 1000,+ filters: { aggregation_axis_format: 'numeric' },+ expected: '250 (25%)',+ },+ {+ candidate: 250,+ total: 1000,+ filters: { aggregation_axis_format: 'currency' },+ expected: '$250.00 (25%)',+ },+ {+ candidate: 60,+ total: 100,+ filters: { aggregation_axis_format: 'duration' },+ expected: '1m (60%)',+ },+ // Percentage axis formats already render a % suffix, so we skip share-of-total to avoid "37% (60%)".+ {+ candidate: 37,+ total: 100,+ filters: { aggregation_axis_format: 'percentage' },+ expected: '37%',+ },+ {+ candidate: 0.37,+ total: 1,+ filters: { aggregation_axis_format: 'percentage_scaled' },+ expected: '37%',+ },+ {+ candidate: 37,+ total: 100,+ filters: { aggregationAxisFormat: 'percentage' },+ expected: '37%',+ },+ // When total is zero (or otherwise falsy), skip the share-of-total to avoid "NaN%".+ { candidate: 0, total: 0, filters: {}, expected: '0' },+ { candidate: 0, total: 0, filters: { aggregation_axis_format: 'currency' }, expected: '$0.00' },+ ]++ shareTestcases.forEach((testcase) => {+ it(`correctly formats "${testcase.candidate}" of total ${testcase.total} as ${+ testcase.expected+ } when filters are ${JSON.stringify(testcase.filters)}`, () => {+ expect(+ formatAggregationAxisValueWithShareOfTotal(+ testcase.filters as Partial<FilterType>,+ testcase.candidate,+ testcase.total+ )+ ).toEqual(testcase.expected)+ })+ })+})diff --git a/frontend/src/scenes/insights/views/LineGraph/PieChart.tsx b/frontend/src/scenes/insights/views/LineGraph/PieChart.tsxindex 5afdd6babf44..0e91e4473cb0 100644--- a/frontend/src/scenes/insights/views/LineGraph/PieChart.tsx+++ b/frontend/src/scenes/insights/views/LineGraph/PieChart.tsx@@ -16,7 +16,10 @@ import {import { SeriesLetter } from 'lib/components/SeriesGlyph'import { useChart } from 'lib/hooks/useChart'import { isString } from 'lib/utils'-import { formatAggregationAxisValue } from 'scenes/insights/aggregationAxisFormat'+import {+ formatAggregationAxisValue,+ formatAggregationAxisValueWithShareOfTotal,+} from 'scenes/insights/aggregationAxisFormat'import { insightLogic } from 'scenes/insights/insightLogic'import { InsightTooltip } from 'scenes/insights/InsightTooltip/InsightTooltip'import { SeriesDatum } from 'scenes/insights/InsightTooltip/insightTooltipUtils'@@ -245,14 +248,12 @@ export function PieChart({(a: number, b: number) => a + b,0)- const percentageLabel: number = parseFloat(- ((value / total) * 100).toFixed(1)- )- return `${formatAggregationAxisValue(+ return formatAggregationAxisValueWithShareOfTotal(trendsFilter,value,+ total,baseCurrency- )} (${percentageLabel}%)`+ )})}hideInspectActorsSection={!onClick || !showPersonsModal}diff --git a/frontend/src/scenes/trends/viz/ActionsHorizontalBar.tsx b/frontend/src/scenes/trends/viz/ActionsHorizontalBar.tsxindex c0bae3a1c619..925fb1e02932 100644--- a/frontend/src/scenes/trends/viz/ActionsHorizontalBar.tsx+++ b/frontend/src/scenes/trends/viz/ActionsHorizontalBar.tsx@@ -1,8 +1,10 @@import { useValues } from 'kea'import { useEffect, useState } from 'react'+import { formatAggregationAxisValueWithShareOfTotal } from 'scenes/insights/aggregationAxisFormat'import { insightLogic } from 'scenes/insights/insightLogic'import { formatBreakdownLabel } from 'scenes/insights/utils'+import { teamLogic } from 'scenes/teamLogic'import { datasetToActorsQuery } from 'scenes/trends/viz/datasetToActorsQuery'import { cohortsModel } from '~/models/cohortsModel'@@ -28,6 +30,7 @@ export function ActionsHorizontalBar({const { formatPropertyValueForDisplay } = useValues(propertyDefinitionsModel)const { insightProps } = useValues(insightLogic)+ const { baseCurrency } = useValues(teamLogic)const {indexedResults,labelGroupType,@@ -94,6 +97,8 @@ export function ActionsHorizontalBar({tooltip={{showHeader: false,groupTypeLabel: context?.groupTypeLabel,+ renderCount: (value: number): string =>+ formatAggregationAxisValueWithShareOfTotal(trendsFilter, value, total, baseCurrency),}}labelGroupType={labelGroupType}datasets={data}