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.

feat(data-warehouse): allow per-breakdown color customization on SQL insights (#59068)

Thomas Obermüller7e758628f45e
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 abdad9c897a0..951ee95cdab6 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.test.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/PieChart.test.ts
@@ -45,11 +45,13 @@ describe('buildPieSlices', () => {
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' } },
},
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx b/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx
index dcb1fd06aaa6..0e838e1c016f 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx
+++ b/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx
@@ -4,6 +4,7 @@ import { Form } from 'kea-forms'
import { IconGear, IconPlusSmall, IconTrash } from '@posthog/icons'
import {
LemonButton,
+ LemonColorButton,
LemonColorGlyph,
LemonColorPicker,
LemonInput,
@@ -16,10 +17,12 @@ import {
Popover,
} from '@posthog/lemon-ui'
-import { getSeriesColor, getSeriesColorPalette } from 'lib/colors'
+import { DataColorToken, getSeriesColor, getSeriesColorPalette } from 'lib/colors'
import { LemonField } from 'lib/lemon-ui/LemonField'
+import { dataThemeLogic } from 'scenes/dataThemeLogic'
import { INSIGHT_UNIT_OPTIONS_SHORT } from 'scenes/insights/aggregationAxisFormat'
+import { ResultCustomizationBy } from '~/queries/schema/schema-general'
import { ChartDisplayType } from '~/types'
import { AxisSeries, dataVisualizationLogic } from '../dataVisualizationLogic'
@@ -654,16 +657,34 @@ const BreakdownSeries = ({
series: AxisBreakdownSeries<number | null>
index: number
}): JSX.Element => {
+ const { chartSettings } = useValues(dataVisualizationLogic)
+ const { updateChartSettings } = useActions(dataVisualizationLogic)
+ const { getTheme } = useValues(dataThemeLogic)
+
+ const theme = getTheme(undefined)
+ const themeTokens = theme ? (Object.keys(theme) as DataColorToken[]) : []
+ const selectedToken = chartSettings.resultCustomizations?.[series.breakdownValue]?.color ?? null
const seriesColor = series.settings?.display?.color ?? getSeriesColor(index)
return (
- <div className="flex gap-1 mb-2">
- <div className="flex gap-2">
- <LemonColorGlyph color={seriesColor} className="mr-2" />
- <span>{series.name ? series.name : '[No value]'}</span>
- </div>
- {/* For now let's keep things simple and not allow too much configuration */}
- {/* We may just want to add a show/hide button here */}
+ <div className="flex gap-1 mb-2 items-center">
+ <LemonColorPicker
+ colorTokens={themeTokens}
+ selectedColorToken={selectedToken}
+ customButton={<LemonColorButton type="tertiary" color={seriesColor} className="mr-2" />}
+ onSelectColorToken={(token) => {
+ updateChartSettings({
+ resultCustomizations: {
+ ...chartSettings.resultCustomizations,
+ [series.breakdownValue]: {
+ assignmentBy: ResultCustomizationBy.Value,
+ color: token,
+ },
+ },
+ })
+ }}
+ />
+ <span>{series.name ? series.name : '[No value]'}</span>
</div>
)
}
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts
index 7ac3c39a907e..310d8766fa67 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts
@@ -299,6 +299,7 @@ describe('seriesBreakdownLogic', () => {
seriesData: [
{
name: 'Safari',
+ breakdownValue: 'Safari',
data: [11, 32, 282],
settings: {
formatting: { prefix: '', suffix: '' },
@@ -307,6 +308,7 @@ describe('seriesBreakdownLogic', () => {
},
{
name: 'Firefox',
+ breakdownValue: 'Firefox',
data: [22, 60, 820],
settings: {
formatting: { prefix: '', suffix: '' },
@@ -315,6 +317,7 @@ describe('seriesBreakdownLogic', () => {
},
{
name: 'Chrome',
+ breakdownValue: 'Chrome',
data: [59, 173, 2218],
settings: {
formatting: { prefix: '', suffix: '' },
@@ -370,6 +373,7 @@ describe('seriesBreakdownLogic', () => {
seriesData: [
{
name: 'Safari',
+ breakdownValue: 'Safari',
data: [11, 32, null],
settings: {
formatting: { prefix: '', suffix: '' },
@@ -378,6 +382,7 @@ describe('seriesBreakdownLogic', () => {
},
{
name: 'Firefox',
+ breakdownValue: 'Firefox',
data: [null, null, 820],
settings: {
formatting: { prefix: '', suffix: '' },
@@ -434,6 +439,7 @@ describe('seriesBreakdownLogic', () => {
seriesData: [
{
name: 'Safari',
+ breakdownValue: 'Safari',
data: [0, 32, 0],
settings: {
formatting: { prefix: '', suffix: '' },
@@ -442,6 +448,7 @@ describe('seriesBreakdownLogic', () => {
},
{
name: 'Firefox',
+ breakdownValue: 'Firefox',
data: [0, 0, 820],
settings: {
formatting: { prefix: '', suffix: '' },
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts
index 5328a2742c7f..9b5cb420d3ff 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts
@@ -1,10 +1,25 @@
import { actions, afterMount, connect, kea, key, listeners, path, props, selectors } from 'kea'
+import { DataColorTheme } from 'lib/colors'
+import { dataThemeLogic, getColorFromToken } from 'scenes/dataThemeLogic'
+
import { AxisSeries, AxisSeriesSettings, SelectedYAxis, dataVisualizationLogic } from '../dataVisualizationLogic'
import type { seriesBreakdownLogicType } from './seriesBreakdownLogicType'
+/**
+ * Sentinel used to key result customizations for null / undefined breakdown values.
+ * Mirrors `BREAKDOWN_NULL_STRING_LABEL` from `scenes/insights/utils` to keep the
+ * collision-resistance contract consistent with trends/funnels breakdowns.
+ */
+export const BREAKDOWN_NULL_KEY = '$$_posthog_breakdown_null_$$'
+
+export const getBreakdownValueKey = (value: unknown): string =>
+ value === null || value === undefined ? BREAKDOWN_NULL_KEY : String(value)
+
export interface AxisBreakdownSeries<T> {
name: string
+ /** Stable key derived from the raw breakdown column value, used for result customization lookups. */
+ breakdownValue: string
data: T[]
settings?: AxisSeriesSettings
}
@@ -76,6 +91,8 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
values: [
dataVisualizationLogic,
['query', 'response', 'columns', 'selectedXAxis', 'selectedYAxis', 'chartSettings'],
+ dataThemeLogic,
+ ['getTheme'],
],
})),
actions(({ values }) => ({
@@ -120,6 +137,7 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
s.response,
s.columns,
s.chartSettings,
+ s.getTheme,
],
(
selectedBreakdownColumn,
@@ -128,7 +146,8 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
xSeries,
response,
columns,
- chartSettings
+ chartSettings,
+ getTheme: (themeId: string | number | null | undefined) => DataColorTheme | null
): BreakdownSeriesData<number | null> => {
if (
!response ||
@@ -175,6 +194,8 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
const multipleYSeries = yAxis.length > 1
const showNullsAsZero = chartSettings.showNullsAsZero ?? false
+ const resultCustomizations = chartSettings.resultCustomizations ?? {}
+ const theme = getTheme(undefined)
const seriesData: AxisBreakdownSeries<number | null>[] = yAxis.flatMap((selectedYAxis) => {
const yColumn = columns.find((n) => n.name === selectedYAxis.name)
@@ -186,13 +207,19 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
const seriesName = multipleYSeries
? `${selectedYAxis.name} - ${value || '[No value]'}`
: value || '[No value]'
+ const breakdownValue = getBreakdownValueKey(value)
+ const customColorToken = resultCustomizations[breakdownValue]?.color
+ const customColor =
+ customColorToken && theme ? getColorFromToken(theme, customColorToken) : undefined
// first filter data by breakdown column value
const filteredData = data.filter((n) => n[breakdownColumn.dataIndex] === value)
if (filteredData.length === 0) {
return {
name: seriesName,
+ breakdownValue,
data: [],
+ settings: customColor ? { display: { color: customColor } } : undefined,
}
}
@@ -223,15 +250,19 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
return {
name: seriesName,
+ breakdownValue,
data: dataset,
// we copy supported settings over from the selected
// y-axis since we don't support setting these on the
- // breakdown series at the moment
+ // breakdown series at the moment. The per-breakdown
+ // color customization (if any) wins over the inherited
+ // y-axis color.
settings: {
formatting: selectedYAxis.settings.formatting,
display: {
yAxisPosition: selectedYAxis.settings?.display?.yAxisPosition,
displayType: selectedYAxis.settings?.display?.displayType,
+ color: customColor,
},
},
}
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.json
index cafb5a1f7dd4..613baa8fcb00 100644
--- a/frontend/src/queries/schema.json
+++ b/frontend/src/queries/schema.json
@@ -10487,6 +10487,13 @@
"leftYAxisSettings": {
"$ref": "#/definitions/YAxisSettings"
},
+ "resultCustomizations": {
+ "additionalProperties": {
+ "$ref": "#/definitions/ResultCustomizationByValue"
+ },
+ "description": "Per-breakdown-value color customizations. Keyed by the raw breakdown column value.",
+ "type": "object"
+ },
"rightYAxisSettings": {
"$ref": "#/definitions/YAxisSettings"
},
diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.ts
index 3b968d1fe7f4..8c4e95e224fb 100644
--- a/frontend/src/queries/schema/schema-general.ts
+++ b/frontend/src/queries/schema/schema-general.ts
@@ -1159,6 +1159,8 @@ export interface ChartSettings {
showPieTotal?: boolean
showNullsAsZero?: boolean
heatmap?: HeatmapSettings
+ /** Per-breakdown-value color customizations. Keyed by the raw breakdown column value. */
+ resultCustomizations?: Record<string, ResultCustomizationByValue>
}
export interface ConditionalFormattingRule {
diff --git a/posthog/schema.py b/posthog/schema.py
index 0d30c7e22164..e7d464bcc324 100644
--- a/posthog/schema.py
+++ b/posthog/schema.py
@@ -12817,6 +12817,10 @@ class ChartSettings(BaseModel):
goalLines: list[GoalLine] | None = None
heatmap: HeatmapSettings | None = None
leftYAxisSettings: YAxisSettings | None = None
+ resultCustomizations: dict[str, ResultCustomizationByValue] | None = Field(
+ default=None,
+ description=("Per-breakdown-value color customizations. Keyed by the raw breakdown column value."),
+ )
rightYAxisSettings: YAxisSettings | None = None
seriesBreakdownColumn: str | None = None
showLegend: bool | None = None
diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.ts
index 78d46d680b3a..e15521d79b0e 100644
--- a/products/product_analytics/frontend/generated/api.schemas.ts
+++ b/products/product_analytics/frontend/generated/api.schemas.ts
@@ -6549,10 +6549,17 @@ export interface ChartAxisApi {
settings?: SettingsApi | null
}
+/**
+ * Per-breakdown-value color customizations. Keyed by the raw breakdown column value.
+ */
+export type ChartSettingsApiResultCustomizations = { [key: string]: ResultCustomizationByValueApi } | null
+
export interface ChartSettingsApi {
goalLines?: GoalLineApi[] | null
heatmap?: HeatmapSettingsApi | null
leftYAxisSettings?: YAxisSettingsApi | null
+ /** Per-breakdown-value color customizations. Keyed by the raw breakdown column value. */
+ resultCustomizations?: ChartSettingsApiResultCustomizations
rightYAxisSettings?: YAxisSettingsApi | null
seriesBreakdownColumn?: string | null
showLegend?: boolean | null
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 90d8e1cd3a2f..217dabd382d9 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -6894,6 +6894,11 @@ export namespace Schemas {
settings?: Settings | null;
}
+ /**
+ * Per-breakdown-value color customizations. Keyed by the raw breakdown column value.
+ */
+ export type ChartSettingsResultCustomizations = {[key: string]: ResultCustomizationByValue} | null;
+
export interface HeatmapGradientStop {
color: string;
value: number;
@@ -6951,6 +6956,8 @@ export namespace Schemas {
goalLines?: GoalLine[] | null;
heatmap?: HeatmapSettings | null;
leftYAxisSettings?: YAxisSettings | null;
+ /** Per-breakdown-value color customizations. Keyed by the raw breakdown column value. */
+ resultCustomizations?: ChartSettingsResultCustomizations;
rightYAxisSettings?: YAxisSettings | null;
seriesBreakdownColumn?: string | null;
showLegend?: boolean | null;