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(sql-viz): improve pie chart tooltips and ai (#63187)

Marius Andra5e6d2a7dac77
diff --git a/ee/hogai/chat_agent/sql/prompts.py b/ee/hogai/chat_agent/sql/prompts.py
index 56be79984ec3..d8c745004f7a 100644
--- a/ee/hogai/chat_agent/sql/prompts.py
+++ b/ee/hogai/chat_agent/sql/prompts.py
@@ -104,6 +104,7 @@ Visualization guidance:
- Single-row, single-metric results should use `BoldNumber`.
- Categorical comparisons should use `ActionsBar`.
- Use `ActionsStackedBar` only when a categorical breakdown column should split each x-axis category into colored series.
+- Use `ActionsPie` when the user asks for a pie chart or wants to see proportions of a whole across a small set of categories; set `x_axis` to the category column and `y_axis` to the single numeric value column.
- Use `TwoDimensionalHeatmap` only when the query returns x, y, and numeric value columns for a matrix-style result.
- Use `ActionsTable` for lists, raw event/person rows, or when multiple text columns are the point of the result.
diff --git a/ee/hogai/chat_agent/sql/toolkit.py b/ee/hogai/chat_agent/sql/toolkit.py
index 5f7de771352a..3fc189573ad8 100644
--- a/ee/hogai/chat_agent/sql/toolkit.py
+++ b/ee/hogai/chat_agent/sql/toolkit.py
@@ -18,12 +18,14 @@ def generate_sql_schema() -> dict:
"ActionsAreaGraph",
"ActionsBar",
"ActionsStackedBar",
+ "ActionsPie",
"TwoDimensionalHeatmap",
],
"description": (
"The visualization type for the query results. Use ActionsLineGraph or ActionsAreaGraph for "
"time-based data, BoldNumber for a single value, ActionsBar for categorical comparisons, "
"ActionsStackedBar when a breakdown column should split bars into series, "
+ "ActionsPie for part-of-a-whole proportions across a small number of categories, "
"TwoDimensionalHeatmap for x/y/value grids, and ActionsTable only when rows are the intended "
"output."
),
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.test.ts
index 951ee95cdab6..3820616251e3 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.test.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.test.ts
@@ -2,113 +2,133 @@ import { getSeriesColor } from 'lib/colors'
import { AxisSeries } from '../../dataVisualizationLogic'
import { AxisBreakdownSeries } from '../seriesBreakdownLogic'
-import { buildPieSlices } from './PieChart'
+import { buildPieSlices, formatPieSliceCount } from './PieChart'
-describe('buildPieSlices', () => {
- const xData: AxisSeries<string> = {
- column: {
- name: 'category',
- type: {
- name: 'STRING',
- isNumerical: false,
- },
- label: 'category',
- dataIndex: 0,
- },
- data: ['alpha', 'beta', 'alpha'],
- }
-
- it('aggregates a single y-series by x-axis label', () => {
- const yData: AxisSeries<number | null>[] = [
- {
- column: {
- name: 'value',
- type: {
- name: 'INTEGER',
- isNumerical: true,
- },
- label: 'value',
- dataIndex: 1,
- },
- data: [2, 3, 5],
- settings: {},
- },
- ]
-
- expect(buildPieSlices(xData, yData)).toEqual([
- { label: 'alpha', value: 7, color: getSeriesColor(0) },
- { label: 'beta', value: 3, color: getSeriesColor(1) },
- ])
+describe('PieChart', () => {
+ describe('formatPieSliceCount', () => {
+ it.each([
+ ['appends share of total', 25, 100, undefined, '25 (25%)'],
+ ['rounds share to one decimal place', 1, 3, undefined, '1 (33.3%)'],
+ ['omits share when total is zero', 5, 0, undefined, '5'],
+ ['omits share for percent-styled values', 25, 100, { formatting: { style: 'percent' as const } }, '25%'],
+ [
+ 'formats value with settings before appending share',
+ 1234.5,
+ 2469,
+ { formatting: { style: 'number' as const, decimalPlaces: 0 } },
+ '1,235 (50%)',
+ ],
+ ])('%s', (_name, value, total, settings, expected) => {
+ expect(formatPieSliceCount(value, total, settings)).toEqual(expected)
+ })
})
- it('aggregates breakdown series by series total', () => {
- const yData: AxisBreakdownSeries<number | null>[] = [
- {
- name: 'first',
- breakdownValue: 'first',
- data: [1, 2, null],
- settings: { display: { color: '#111111' } },
- },
- {
- name: 'second',
- breakdownValue: 'second',
- data: [3, 4, 5],
- settings: { display: { color: '#222222' } },
- },
- ]
-
- expect(buildPieSlices(xData, yData)).toEqual([
- { label: 'first', value: 3, color: '#111111' },
- { label: 'second', value: 12, color: '#222222' },
- ])
- })
-
- it('falls back to one slice per y-series when there is no categorical x-axis', () => {
- const noXAxis: AxisSeries<string> = {
+ describe('buildPieSlices', () => {
+ const xData: AxisSeries<string> = {
column: {
- name: 'None',
+ name: 'category',
type: {
name: 'STRING',
isNumerical: false,
},
- label: 'None',
- dataIndex: -1,
+ label: 'category',
+ dataIndex: 0,
},
- data: ['', ''],
+ data: ['alpha', 'beta', 'alpha'],
}
- const yData: AxisSeries<number | null>[] = [
- {
- column: {
- name: 'apples',
- type: {
- name: 'INTEGER',
- isNumerical: true,
+ it('aggregates a single y-series by x-axis label', () => {
+ const yData: AxisSeries<number | null>[] = [
+ {
+ column: {
+ name: 'value',
+ type: {
+ name: 'INTEGER',
+ isNumerical: true,
+ },
+ label: 'value',
+ dataIndex: 1,
},
- label: 'apples',
- dataIndex: 0,
+ data: [2, 3, 5],
+ settings: {},
},
- data: [1, 2],
- settings: {},
- },
- {
+ ]
+
+ expect(buildPieSlices(xData, yData)).toEqual([
+ { label: 'alpha', value: 7, color: getSeriesColor(0) },
+ { label: 'beta', value: 3, color: getSeriesColor(1) },
+ ])
+ })
+
+ it('aggregates breakdown series by series total', () => {
+ const yData: AxisBreakdownSeries<number | null>[] = [
+ {
+ name: 'first',
+ breakdownValue: 'first',
+ data: [1, 2, null],
+ settings: { display: { color: '#111111' } },
+ },
+ {
+ name: 'second',
+ breakdownValue: 'second',
+ data: [3, 4, 5],
+ settings: { display: { color: '#222222' } },
+ },
+ ]
+
+ expect(buildPieSlices(xData, yData)).toEqual([
+ { label: 'first', value: 3, color: '#111111' },
+ { label: 'second', value: 12, color: '#222222' },
+ ])
+ })
+
+ it('falls back to one slice per y-series when there is no categorical x-axis', () => {
+ const noXAxis: AxisSeries<string> = {
column: {
- name: 'oranges',
+ name: 'None',
type: {
- name: 'INTEGER',
- isNumerical: true,
+ name: 'STRING',
+ isNumerical: false,
},
- label: 'oranges',
- dataIndex: 1,
+ label: 'None',
+ dataIndex: -1,
},
- data: [3, 4],
- settings: {},
- },
- ]
+ data: ['', ''],
+ }
+
+ const yData: AxisSeries<number | null>[] = [
+ {
+ column: {
+ name: 'apples',
+ type: {
+ name: 'INTEGER',
+ isNumerical: true,
+ },
+ label: 'apples',
+ dataIndex: 0,
+ },
+ data: [1, 2],
+ settings: {},
+ },
+ {
+ column: {
+ name: 'oranges',
+ type: {
+ name: 'INTEGER',
+ isNumerical: true,
+ },
+ label: 'oranges',
+ dataIndex: 1,
+ },
+ data: [3, 4],
+ settings: {},
+ },
+ ]
- expect(buildPieSlices(noXAxis, yData)).toEqual([
- { label: 'apples', value: 3, color: getSeriesColor(0) },
- { label: 'oranges', value: 7, color: getSeriesColor(1) },
- ])
+ expect(buildPieSlices(noXAxis, yData)).toEqual([
+ { label: 'apples', value: 3, color: getSeriesColor(0) },
+ { label: 'oranges', value: 7, color: getSeriesColor(1) },
+ ])
+ })
})
})
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.tsx
index d307b8066b48..911b6b8b8743 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.tsx
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.tsx
@@ -11,7 +11,7 @@ import { PieChart as InsightPieChart } from 'scenes/insights/views/LineGraph/Pie
import { ChartSettings } from '~/queries/schema/schema-general'
import { InsightLogicProps, GraphType } from '~/types'
-import { AxisSeries, formatDataWithSettings } from '../../dataVisualizationLogic'
+import { AxisSeries, AxisSeriesSettings, formatDataWithSettings } from '../../dataVisualizationLogic'
import { AxisBreakdownSeries } from '../seriesBreakdownLogic'
export interface PieChartProps {
@@ -103,6 +103,16 @@ export const buildPieSlices = (
.filter((slice) => slice.value > 0)
}
+export const formatPieSliceCount = (value: number, total: number, settings?: AxisSeriesSettings): string => {
+ const formatted = String(formatDataWithSettings(value, settings) ?? value)
+ // Percent-styled values are already a share, so a share-of-total suffix would be confusing
+ if (!total || settings?.formatting?.style === 'percent') {
+ return formatted
+ }
+ const shareOfTotal = parseFloat(((value / total) * 100).toFixed(1))
+ return `${formatted} (${shareOfTotal}%)`
+}
+
export function PieChart({
xData,
yData,
@@ -145,6 +155,7 @@ export function PieChart({
tooltip={{
showHeader: false,
hideColorCol: true,
+ renderCount: (value: number) => formatPieSliceCount(value, total, formattingSettings),
}}
datasets={[
{