: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(mcp): draw pie and donut trends as pie charts in MCP app (#114154)
Paul D'Ambraae6700906529
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 1bfa2ad859d2..23df5a12b0cc 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -6316,8 +6316,14 @@ snapshots:hash: v1.k794b7964.2c12f9bc885c2159d88852c4ac472f681cc3dcb01e38e9291bfbd4184071044b.UEn1UxsfTw73_Dw6BSg5MGn9UsF3gk8BhG4y2SMb4HUmcp-apps-trends--bar-value-many-breakdowns--light:hash: v1.k794b7964.22187bb708adf85f40ea196df123bc4f11384eeef88ab0ec4d8e5fde8cd07165.IDxL-VRfm6bgHZPh_Z9MVj3g0ssz3su3PHGzNaTf-Vk+ mcp-apps-trends--donut--light:+ hash: v1.k794b7964.2ecaef94116d43c67f8eb8b41f6df93f350896e36be23dabf15d17ed794f894e.ZtobinT9oCgMvwrL2sfTGim519QmGqoi8xB_Pu4HObQmcp-apps-trends--multi-series--light:hash: v1.k794b7964.590c3b62c9f76c72da6ee2ee2a4cf22c27881b373690f0dd05039b3276674e2d.-MNk8IwYc1MD_AJwE11zBCWPKHS311soE8tuEewecRc+ mcp-apps-trends--pie--light:+ hash: v1.k794b7964.71bf9a67abbc8af67484c4ba46db9846533f0817a6c9431dbe86671f8c0c0657.gI8aNPSnINceu0ufFOIk1eB34bjlf7nWah24hc8EZRA+ mcp-apps-trends--pie-percentages--light:+ hash: v1.k794b7964.6333bf4d729ec9ca4bfd598fcf1fa09a528f1599ce8f520e0ac0dd4e090d0664.15Ipmr89-9dldfy4jSeYJzGj68hsOmPmhgafAhHmwkomcp-apps-trends--single-series--light:hash: v1.k794b7964.1b5decab0f76ab924d787808679c22a9d7e29d3b908dc4efa02492eb5a5975ec.v1FGWBy1MIcdGPJmdIHHBHAD0lgjDTnH067dbcZqZGwmcp-apps-trends--time-series-bar--light:diff --git a/services/mcp/src/ui-apps/components/Trends.stories.tsx b/services/mcp/src/ui-apps/components/Trends.stories.tsxindex 27101a5b752c..b99f3aae3c3b 100644--- a/services/mcp/src/ui-apps/components/Trends.stories.tsx+++ b/services/mcp/src/ui-apps/components/Trends.stories.tsx@@ -95,3 +95,32 @@ export const BarValueManyBreakdowns: Story = {]),name: 'Bar value — many breakdowns',}++const BROWSER_TOTALS: TrendsResult = [+ { label: 'Chrome', aggregated_value: 8421 },+ { label: 'Firefox', aggregated_value: 3204 },+ { label: 'Safari', aggregated_value: 2817 },+ { label: 'Edge', aggregated_value: 1236 },+]++export const Pie: Story = {+ render: () => renderTrends(trendsQuery('ActionsPie'), BROWSER_TOTALS),+ name: 'Pie',+}++export const PiePercentages: Story = {+ render: () =>+ renderTrends(+ {+ kind: 'TrendsQuery',+ trendsFilter: { display: 'ActionsPie', showValuesOnSeries: true, showPercentStackView: true },+ },+ BROWSER_TOTALS+ ),+ name: 'Pie — values and percentages',+}++export const Donut: Story = {+ render: () => renderTrends(trendsQuery('ActionsDonut'), BROWSER_TOTALS),+ name: 'Donut',+}diff --git a/services/mcp/src/ui-apps/components/TrendsVisualizer.tsx b/services/mcp/src/ui-apps/components/TrendsVisualizer.tsxindex 0a5ee78f8b36..557f28bd0f22 100644--- a/services/mcp/src/ui-apps/components/TrendsVisualizer.tsx+++ b/services/mcp/src/ui-apps/components/TrendsVisualizer.tsx@@ -4,8 +4,10 @@ import { emptyStateIllustration } from '@posthog/mcp-ui'import { Empty, EmptyDescription, EmptyHeader, EmptyMedia } from '@posthog/quill'import {BarChart as BarValueChart,+ buildYTickFormatter,ciRanges,DefaultTooltip,+ PieChart,SlopeChart,TimeSeriesBarChart,TimeSeriesLineChart,@@ -33,6 +35,7 @@ import {defaultChartType,displayForChartType,isBarFamily,+ pieViewFromTrendsFilter,resolveChartView,supportsPercentStack,} from './chartSettingsConfig'@@ -126,6 +129,29 @@ export function TrendsVisualizer({ query, results }: TrendsVisualizerProps): Rea)}+ if (displayType === 'ActionsPie' || displayType === 'ActionsDonut') {+ const slices = results.map((item, i) => ({+ key: String(i),+ label: getSeriesLabel(item, i),+ data: [item.aggregated_value ?? 0],+ color: colorAt(i),+ }))+ const pieView = pieViewFromTrendsFilter(query?.trendsFilter, displayType === 'ActionsDonut')+ return (+ <div>+ <ChartHeader title={TITLE} />+ <div className="flex flex-col w-full h-[400px]">+ <PieChart+ series={slices}+ theme={theme}+ config={pieView.config}+ valueFormatter={buildYTickFormatter(pieView.valueFormat)}+ />+ </div>+ </div>+ )+ }+const labels = results[0]?.days ?? results[0]?.labels ?? []const trendResults = results.map((item, i) => ({id: i,diff --git a/services/mcp/src/ui-apps/components/chartSettingsConfig.ts b/services/mcp/src/ui-apps/components/chartSettingsConfig.tsindex c8204239ffc1..85d0de11e01d 100644--- a/services/mcp/src/ui-apps/components/chartSettingsConfig.ts+++ b/services/mcp/src/ui-apps/components/chartSettingsConfig.ts@@ -1,4 +1,6 @@-import type { YAxisFormat } from '@posthog/quill-charts'+import type { PieChartConfig, YAxisFormat, YFormatterConfig } from '@posthog/quill-charts'++import { trendsFilterToYFormatterConfig } from 'products/product_analytics/frontend/insights/trends/shared/trendsAxisFormat'import type { ChartDisplayType, TrendsFilter } from './types'@@ -46,6 +48,31 @@ export function chartConfigFromTrendsFilter(trendsFilter: TrendsFilter | undefin}}+// Matches DONUT_INNER_RADIUS_RATIO in TrendsPieChart.tsx so the MCP donut looks like the insight donut.+const DONUT_INNER_RADIUS_RATIO = 0.6++export interface PieView {+ config: PieChartConfig+ valueFormat: YFormatterConfig+}++// Applies the same slice label rules as TrendsPieChart.tsx, so the preview shows the numbers the saved insight shows.+export function pieViewFromTrendsFilter(trendsFilter: TrendsFilter | undefined, isDonut: boolean): PieView {+ const showValue = !!trendsFilter?.showValuesOnSeries+ const showPercent = !!trendsFilter?.showPercentStackView+ return {+ config: {+ showValueOnSlice: showValue || showPercent,+ sliceValueDisplay: showValue && showPercent ? 'both' : showPercent ? 'percent' : 'value',+ isPercent: showPercent,+ innerRadiusRatio: isDonut ? DONUT_INNER_RADIUS_RATIO : undefined,+ legend: { show: true, position: 'bottom' },+ },+ // PieChart converts slices to percentages itself, so the value format keeps the value's own units.+ valueFormat: trendsFilterToYFormatterConfig(trendsFilter, false),+ }+}+export function defaultChartType(displayType: ChartDisplayType): ChartType {if (displayType === 'SlopeGraph') {return 'slope'diff --git a/services/mcp/src/ui-apps/components/types.ts b/services/mcp/src/ui-apps/components/types.tsindex 65c83972a41a..ccde7d2e2480 100644--- a/services/mcp/src/ui-apps/components/types.ts+++ b/services/mcp/src/ui-apps/components/types.ts@@ -14,6 +14,7 @@ export type ChartDisplayType =| 'ActionsAreaGraph'| 'BoldNumber'| 'ActionsPie'+ | 'ActionsDonut'| 'ActionsTable'| 'WorldMap'| 'SlopeGraph'diff --git a/services/mcp/tests/unit/trends-chart-settings.test.ts b/services/mcp/tests/unit/trends-chart-settings.test.tsindex a33e1320721e..fac2d8fdcfca 100644--- a/services/mcp/tests/unit/trends-chart-settings.test.ts+++ b/services/mcp/tests/unit/trends-chart-settings.test.ts@@ -5,6 +5,7 @@ import {DEFAULT_CHART_CONFIG,defaultChartType,isBarFamily,+ pieViewFromTrendsFilter,resolveChartView,supportsPercentStack,} from '../../src/ui-apps/components/chartSettingsConfig'@@ -93,4 +94,39 @@ describe('trends chart settings', () => {expect(resolveChartView(chartType, 7).effectiveType).toBe(chartType)})})++ describe('pieViewFromTrendsFilter', () => {+ it.each([+ ['no trendsFilter', undefined, { showValueOnSlice: false, sliceValueDisplay: 'value', isPercent: false }],+ [+ 'values on series',+ { showValuesOnSeries: true },+ { showValueOnSlice: true, sliceValueDisplay: 'value', isPercent: false },+ ],+ [+ 'percent view',+ { showPercentStackView: true },+ { showValueOnSlice: true, sliceValueDisplay: 'percent', isPercent: true },+ ],+ [+ 'values and percent view',+ { showValuesOnSeries: true, showPercentStackView: true },+ { showValueOnSlice: true, sliceValueDisplay: 'both', isPercent: true },+ ],+ ] as Array<[string, TrendsFilter | undefined, object]>)('slice labels for %s', (_, trendsFilter, expected) => {+ expect(pieViewFromTrendsFilter(trendsFilter, false).config).toMatchObject(expected)+ })++ it.each([+ [false, undefined],+ [true, 0.6],+ ])('donut=%s gives innerRadiusRatio %s', (isDonut, innerRadiusRatio) => {+ expect(pieViewFromTrendsFilter(undefined, isDonut).config.innerRadiusRatio).toBe(innerRadiusRatio)+ })++ it('keeps the axis format in percent view', () => {+ const trendsFilter: TrendsFilter = { showPercentStackView: true, aggregationAxisFormat: 'duration' }+ expect(pieViewFromTrendsFilter(trendsFilter, false).valueFormat).toMatchObject({ format: 'duration' })+ })+ })})