: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(data-viz): route SQL charts by resolved series type (#74629)
Sam Penningtonc08e409fe5d8
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.test.tsindex c2d96c2443ad..ceefa8b99a49 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.test.ts+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.test.ts@@ -38,4 +38,24 @@ describe('sqlChartComponentFor', () => {])('routes mixed bar + line series on %s to SqlComboGraph', (_name, visualizationType) => {expect(sqlChartComponentFor({ ...baseProps(visualizationType), yData: mixedYData }).name).toBe('SqlComboGraph')})++ it('routes an all-bar line chart to SqlBarGraph', () => {+ const allBars = [+ ySeries('a', { display: { displayType: 'bar' } }),+ ySeries('b', { display: { displayType: 'bar' } }),+ ]+ expect(sqlChartComponentFor({ ...baseProps(ChartDisplayType.ActionsLineGraph), yData: allBars }).name).toBe(+ 'SqlBarGraph'+ )+ })++ it("routes a percent stack whose line shares the bars' axis to SqlBarGraph", () => {+ expect(+ sqlChartComponentFor({+ ...baseProps(ChartDisplayType.ActionsStackedBar),+ yData: mixedYData,+ chartSettings: { stackBars100: true },+ }).name+ ).toBe('SqlBarGraph')+ })})diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsxindex 4dd76211272a..3e2e0525746c 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsx@@ -6,7 +6,7 @@ import { AxisBreakdownSeries } from '../seriesBreakdownLogic'import { SqlBarGraph } from './SqlBarGraph'import { SqlComboGraph } from './SqlComboGraph'import { SqlLineGraph } from './SqlLineGraph'-import { canRenderSqlBarGraph, canRenderSqlComboGraph } from './sqlLineGraphAdapter'+import { sqlChartKind } from './sqlLineGraphAdapter'export type SqlChartProps = {xData: AxisSeries<string> | null@@ -27,13 +27,14 @@ export type SqlChartProps = {* series, bar for bar-only, line/area otherwise. (Pie has its own wrapper — see PieChart.)*/export function sqlChartComponentFor(props: SqlChartProps): (props: SqlChartProps) => JSX.Element {- if (canRenderSqlComboGraph(props)) {- return SqlComboGraph+ switch (sqlChartKind(props)) {+ case 'combo':+ return SqlComboGraph+ case 'bar':+ return SqlBarGraph+ case 'line':+ return SqlLineGraph}- if (canRenderSqlBarGraph(props)) {- return SqlBarGraph- }- return SqlLineGraph}/** Entry point for rendering a non-pie SQL (DataVisualization) chart: dispatches line, area, bar,diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.tsindex 282a731261eb..b29ee9d6c285 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts@@ -25,9 +25,6 @@ import {buildSeries,buildSqlTooltipConfig,buildTrendLineConfigs,- canRenderSqlBarGraph,- canRenderSqlComboGraph,- canRenderSqlLineGraph,capYSeriesData,comboBarLayoutForDisplay,exceedsMaxSeries,@@ -35,6 +32,7 @@ import {hasAxisTickFormatting,hasMixedSeriesTypes,seriesDisplayType,+ sqlChartKind,} from './sqlLineGraphAdapter'const numericColumn = (name: string, dataIndex: number): AxisSeries<number | null>['column'] => ({@@ -65,70 +63,142 @@ const baseProps = (overrides: Partial<SqlChartProps>): SqlChartProps => ({})describe('sqlLineGraphAdapter', () => {- describe('canRenderSqlLineGraph', () => {- it.each([- ['line graph', ChartDisplayType.ActionsLineGraph, true],- ['area graph', ChartDisplayType.ActionsAreaGraph, true],- ['bar graph', ChartDisplayType.ActionsBar, false],- ['stacked bar graph', ChartDisplayType.ActionsStackedBar, false],- ])('returns %s support correctly for %s', (_name, visualizationType, expected) => {- expect(canRenderSqlLineGraph(baseProps({ visualizationType }))).toBe(expected)- })+ describe('sqlChartKind', () => {+ // Explicit line + bar so the mix holds regardless of the base chart type's auto resolution.+ const mixed = [+ ySeries('a', [1], { display: { displayType: 'line' } }),+ ySeries('b', [2], { display: { displayType: 'bar' } }),+ ]- it('falls back when any series renders as a bar', () => {- const yData = [ySeries('a', [1]), ySeries('b', [2], { display: { displayType: 'bar' } })]- expect(canRenderSqlLineGraph(baseProps({ yData }))).toBe(false)+ it.each([+ ['line graph', ChartDisplayType.ActionsLineGraph, 'line'],+ ['area graph', ChartDisplayType.ActionsAreaGraph, 'line'],+ ['bar graph', ChartDisplayType.ActionsBar, 'bar'],+ ['stacked bar graph', ChartDisplayType.ActionsStackedBar, 'bar'],+ // Pie never reaches dispatch — PieChart wraps it separately.+ ['pie graph', ChartDisplayType.ActionsPie, 'line'],+ ])('follows the chart type for a %s with no series', (_name, visualizationType, expected) => {+ expect(sqlChartKind(baseProps({ visualizationType }))).toBe(expected)})it.each([['line graph', ChartDisplayType.ActionsLineGraph],['area graph', ChartDisplayType.ActionsAreaGraph],- ])('renders trend-line series natively rather than falling back for a %s', (_name, visualizationType) => {- const yData = [ySeries('a', [1], { display: { trendLine: true } })]- expect(canRenderSqlLineGraph(baseProps({ visualizationType, yData }))).toBe(true)+ ['bar graph', ChartDisplayType.ActionsBar],+ ['stacked bar graph', ChartDisplayType.ActionsStackedBar],+ ])('picks combo for mixed series on a %s', (_name, visualizationType) => {+ expect(sqlChartKind(baseProps({ visualizationType, yData: mixed }))).toBe('combo')})- it('renders right y-axis series natively rather than falling back', () => {- const yData = [ySeries('a', [1]), ySeries('b', [2], { display: { yAxisPosition: 'right' } })]- expect(canRenderSqlLineGraph(baseProps({ yData }))).toBe(true)+ it('does not pick combo for an unsupported chart type', () => {+ expect(sqlChartKind(baseProps({ visualizationType: ChartDisplayType.ActionsPie, yData: mixed }))).toBe(+ 'line'+ )})- })- describe('canRenderSqlBarGraph', () => {it.each([- ['line graph', ChartDisplayType.ActionsLineGraph, false],- ['area graph', ChartDisplayType.ActionsAreaGraph, false],- ['bar graph', ChartDisplayType.ActionsBar, true],- ['stacked bar graph', ChartDisplayType.ActionsStackedBar, true],- ])('returns %s support correctly for %s', (_name, visualizationType, expected) => {- expect(canRenderSqlBarGraph(baseProps({ visualizationType }))).toBe(expected)+ ['line', 'line' as const],+ ['area', 'area' as const],+ ])('picks line when every column on a bar graph is overridden to %s', (_name, displayType) => {+ const yData = [+ ySeries('a', [1], { display: { displayType } }),+ ySeries('b', [2], { display: { displayType } }),+ ]+ expect(sqlChartKind(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe('line')})+ // Regression: the per-column setting is more specific than the chart-level type, so an+ // all-bar line chart must draw bars. It used to fall through to the line renderer, which+ // ignores per-series type and drew them as lines.it.each([- ['line', 'line' as const],- ['area', 'area' as const],- ])('falls back when any series overrides display to %s (mixed combo chart)', (_name, displayType) => {- const yData = [ySeries('a', [1]), ySeries('b', [2], { display: { displayType } })]- expect(canRenderSqlBarGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(- false- )+ ['line graph', ChartDisplayType.ActionsLineGraph],+ ['area graph', ChartDisplayType.ActionsAreaGraph],+ ])('picks bar when every column on a %s is overridden to bar', (_name, visualizationType) => {+ const yData = [+ ySeries('a', [1], { display: { displayType: 'bar' } }),+ ySeries('b', [2], { display: { displayType: 'bar' } }),+ ]+ expect(sqlChartKind(baseProps({ visualizationType, yData }))).toBe('bar')})- // Trend lines render natively via TrendLineOverlay — no fallback needed.+ // Trend lines and right-axis series render natively on every path — they never change the kind.it.each([['a plain series', [ySeries('a', [1], { display: { trendLine: true } })]],['a breakdown series', [breakdownSeries('chrome', [1], { display: { trendLine: true } })]],- ])('renders natively when %s has a trend line', (_name, yData) => {- expect(canRenderSqlBarGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(- true- )+ ])('keeps the bar kind when %s has a trend line', (_name, yData) => {+ expect(sqlChartKind(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe('bar')})- it('renders natively when any series targets the right y-axis', () => {+ it('keeps the bar kind when a series targets the right y-axis', () => {const yData = [ySeries('a', [1], { display: { yAxisPosition: 'right' } })]- expect(canRenderSqlBarGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(- true- )+ expect(sqlChartKind(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe('bar')+ })++ it('keeps the line kind for trend-line and right-axis series on a line graph', () => {+ const yData = [+ ySeries('a', [1], { display: { trendLine: true } }),+ ySeries('b', [2], { display: { yAxisPosition: 'right' } }),+ ]+ expect(sqlChartKind(baseProps({ yData }))).toBe('line')+ })++ it('picks combo when a series has a trend line alongside the mix', () => {+ const yData = [+ ySeries('a', [1], { display: { displayType: 'bar' } }),+ ySeries('b', [2], { display: { displayType: 'line', trendLine: true } }),+ ]+ expect(sqlChartKind(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe('combo')+ })++ it('picks combo for percent-stacked bars when the line is on the right axis', () => {+ const yData = [+ ySeries('a', [1], { display: { displayType: 'line', yAxisPosition: 'right' } }),+ ySeries('b', [2], { display: { displayType: 'bar' } }),+ ]+ expect(+ sqlChartKind(+ baseProps({+ visualizationType: ChartDisplayType.ActionsStackedBar,+ yData,+ chartSettings: { stackBars100: true },+ })+ )+ ).toBe('combo')+ })++ // Regression: the bars' axis clamps to [0, 1] in percent mode, so a line on that same axis+ // can't be reconciled with it. Degrade to a plain percent stack — this used to fall through+ // to the line renderer, which dropped the stacking entirely.+ it("picks bar for percent-stacked bars when a line shares the bars' axis", () => {+ expect(+ sqlChartKind(+ baseProps({+ visualizationType: ChartDisplayType.ActionsStackedBar,+ yData: mixed,+ chartSettings: { stackBars100: true },+ })+ )+ ).toBe('bar')+ })++ // Regression: TimeSeriesBarChart percent-stacks every series it's given regardless of+ // series.type, so degrading to bar here would force the right-axis line into the same [0, 1]+ // stack as the left-axis bar/line pair and render it as a constant 100% bar.+ it("picks combo for percent-stacked bars when a line shares the bars' axis and another line is on the right axis", () => {+ const yData = [+ ySeries('a', [1], { display: { displayType: 'bar' } }),+ ySeries('b', [2], { display: { displayType: 'line' } }),+ ySeries('c', [3], { display: { displayType: 'line', yAxisPosition: 'right' } }),+ ]+ expect(+ sqlChartKind(+ baseProps({+ visualizationType: ChartDisplayType.ActionsStackedBar,+ yData,+ chartSettings: { stackBars100: true },+ })+ )+ ).toBe('combo')})})@@ -190,76 +260,6 @@ describe('sqlLineGraphAdapter', () => {})})- describe('canRenderSqlComboGraph', () => {- // Explicit line + bar so the mix holds regardless of the base chart type's auto resolution.- const mixed = [- ySeries('a', [1], { display: { displayType: 'line' } }),- ySeries('b', [2], { display: { displayType: 'bar' } }),- ]-- it.each([- ['line graph', ChartDisplayType.ActionsLineGraph],- ['area graph', ChartDisplayType.ActionsAreaGraph],- ['bar graph', ChartDisplayType.ActionsBar],- ['stacked bar graph', ChartDisplayType.ActionsStackedBar],- ])('renders mixed series on a %s', (_name, visualizationType) => {- expect(canRenderSqlComboGraph(baseProps({ visualizationType, yData: mixed }))).toBe(true)- })-- it('does not render when series are all one type', () => {- const yData = [ySeries('a', [1]), ySeries('b', [2])]- expect(canRenderSqlComboGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(- false- )- })-- it('does not claim an unsupported chart type', () => {- expect(- canRenderSqlComboGraph(baseProps({ visualizationType: ChartDisplayType.ActionsPie, yData: mixed }))- ).toBe(false)- })-- it('renders natively when any series has a trend line (TrendLineOverlay handles it)', () => {- const yData = [- ySeries('a', [1], { display: { displayType: 'bar' } }),- ySeries('b', [2], { display: { displayType: 'line', trendLine: true } }),- ]- expect(canRenderSqlComboGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(- true- )- })-- it('renders natively for percent-stacked bars when the line is on the right axis', () => {- const yData = [- ySeries('a', [1], { display: { displayType: 'line', yAxisPosition: 'right' } }),- ySeries('b', [2], { display: { displayType: 'bar' } }),- ]- expect(- canRenderSqlComboGraph(- baseProps({- visualizationType: ChartDisplayType.ActionsStackedBar,- yData,- chartSettings: { stackBars100: true },- })- )- ).toBe(true)- })-- it("falls back for percent-stacked bars when a line shares the bars' axis", () => {- // The bars' axis clamps to [0, 1] in percent mode — a line on the same (default/left)- // axis has no way to plot its raw values there, so the combo path is unavailable.- expect(- canRenderSqlComboGraph(- baseProps({- visualizationType: ChartDisplayType.ActionsStackedBar,- yData: mixed,- chartSettings: { stackBars100: true },- })- )- ).toBe(false)- })- })-describe('comboBarLayoutForDisplay', () => {it.each([['stacked for a stacked bar graph', ChartDisplayType.ActionsStackedBar, {}, 'stacked'],diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.tsindex 179f41f76423..1570fcd14e75 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts@@ -64,10 +64,7 @@ export function seriesDisplayType(/** True when the series resolve to a mix of bar and line/area — the case neither the line-only nor* the bar-only quill path can render, so it routes to {@link SqlComboGraph}. */-export function hasMixedSeriesTypes(- yData: NonNullable<SqlChartProps['yData']>,- visualizationType: ChartDisplayType-): boolean {+export function hasMixedSeriesTypes(yData: SqlLineYSeries[], visualizationType: ChartDisplayType): boolean {let hasBar = falselet hasLineLike = falsefor (const series of yData) {@@ -104,75 +101,56 @@ export function buildTrendLineConfigs(ySeriesData: SqlLineYSeries[] | null | und}, [])}-/**- * Plain line/area charts — including goal lines, trend lines, and right y-axis series — render here.- * Series that mix a bar with a line/area route to {@link canRenderSqlComboGraph}; other mixes fall- * back to the legacy chart.js path.- */-export function canRenderSqlLineGraph(props: SqlChartProps): boolean {- const { visualizationType, yData } = props+export type SqlChartKind = 'line' | 'bar' | 'combo'- if (- visualizationType !== ChartDisplayType.ActionsLineGraph &&- visualizationType !== ChartDisplayType.ActionsAreaGraph- ) {- return false- }- if (yData?.some((series) => series.settings?.display?.displayType === 'bar')) {- return false- }- return true-}--export function canRenderSqlBarGraph(props: SqlChartProps): boolean {- const { visualizationType, yData } = props-- if (visualizationType !== ChartDisplayType.ActionsBar && visualizationType !== ChartDisplayType.ActionsStackedBar) {- return false- }- if (- yData?.some((series) => {- const displayType = series.settings?.display?.displayType- return displayType === 'line' || displayType === 'area'- })- ) {- return false- }- return true-}+/** The slice of chart props renderer dispatch reads — `SqlChartProps` and Customer analytics'+ * `BillingChartProps` both satisfy it. */+export type SqlChartKindProps = Pick<SqlChartProps, 'visualizationType' | 'yData' | 'chartSettings'>/**- * Mixed bar + line/area series render on quill's {@link TimeSeriesComboChart}. Percent-stacked- * bars are supported as long as every line/area series is routed to the right axis — one sharing- * the bars' axis can't be reconciled with the bars' [0, 1] percent scale, so that case falls back.+ * The single source of truth for which quill renderer draws a SQL insight. Resolves the per-series+ * display types once (`auto` inherits the chart-level type) and picks from there, so the answer+ * follows the series rather than the chart-level type alone: a line chart whose every column is set+ * to Bar draws bars.*/-export function canRenderSqlComboGraph(props: SqlChartProps): boolean {- const { visualizationType, yData, chartSettings } = props-- if (- visualizationType !== ChartDisplayType.ActionsLineGraph &&- visualizationType !== ChartDisplayType.ActionsAreaGraph &&- visualizationType !== ChartDisplayType.ActionsBar &&- visualizationType !== ChartDisplayType.ActionsStackedBar- ) {- return false+export function sqlChartKind({ visualizationType, yData, chartSettings }: SqlChartKindProps): SqlChartKind {+ const isBarBase =+ visualizationType === ChartDisplayType.ActionsBar || visualizationType === ChartDisplayType.ActionsStackedBar+ const isLineBase =+ visualizationType === ChartDisplayType.ActionsLineGraph ||+ visualizationType === ChartDisplayType.ActionsAreaGraph++ // Pie and friends never reach dispatch — PieChart wraps them separately.+ if (!isBarBase && !isLineBase) {+ return 'line'}- if (!yData || !hasMixedSeriesTypes(yData, visualizationType)) {- return false+ if (!yData?.length) {+ return isBarBase ? 'bar' : 'line'}- // Percent-stacked bars clamp their axis to [0, 1] — a line/area series sharing that same axis- // would plot its raw values off-scale with no way to reconcile the two domains. Only allow a- // percent-stack combo when every non-bar series is routed to the right axis instead.- if (- visualizationType === ChartDisplayType.ActionsStackedBar &&- chartSettings.stackBars100 &&- yData.some(++ if (hasMixedSeriesTypes(yData, visualizationType)) {+ // Percent-stacked bars clamp their axis to [0, 1] — a line/area series sharing that same+ // axis would plot its raw values off-scale with no way to reconcile the two domains. Keep+ // the percent stack and draw those series as bars — but only when every non-bar series is on+ // the left axis: `TimeSeriesBarChart` has no notion of `series.type` and percent-stacks every+ // series it's given, so a right-axis non-bar series would still get force-stacked into a+ // constant 100% bar. A right-axis non-bar series has its own scale and is safe to combo.+ const hasNonBarOnLeftAxis = yData.some((series) => seriesDisplayType(visualizationType, series.settings) !== 'bar' && !isRightAxisSeries(series))- ) {- return false+ const hasNonBarOnRightAxis = yData.some(+ (series) => seriesDisplayType(visualizationType, series.settings) !== 'bar' && isRightAxisSeries(series)+ )+ const sharesPercentAxis =+ visualizationType === ChartDisplayType.ActionsStackedBar &&+ chartSettings.stackBars100 &&+ hasNonBarOnLeftAxis &&+ !hasNonBarOnRightAxis+ return sharesPercentAxis ? 'bar' : 'combo'}- return true++ // Not mixed, so every series resolved the same way — the first one speaks for all of them.+ return seriesDisplayType(visualizationType, yData[0].settings) === 'bar' ? 'bar' : 'line'}export function barLayoutForDisplay(diff --git a/products/customer_analytics/frontend/components/Accounts/AccountBillingChart.tsx b/products/customer_analytics/frontend/components/Accounts/AccountBillingChart.tsxindex 8b7461f5c0cd..5f03841d359c 100644--- a/products/customer_analytics/frontend/components/Accounts/AccountBillingChart.tsx+++ b/products/customer_analytics/frontend/components/Accounts/AccountBillingChart.tsx@@ -26,8 +26,7 @@ import {buildComboChartConfig,buildLineChartConfig,buildSeries,- canRenderSqlBarGraph,- canRenderSqlComboGraph,+ sqlChartKind,} from '~/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter'import { AxisSeries, dataVisualizationLogic } from '~/queries/nodes/DataVisualization/dataVisualizationLogic'import {@@ -120,13 +119,14 @@ function BillingChartByKind({chartProps: BillingChartPropshiddenKeys: string[]}): JSX.Element | null {- if (canRenderSqlComboGraph(chartProps)) {- return <BillingComboChart chartProps={chartProps} hiddenKeys={hiddenKeys} />+ switch (sqlChartKind(chartProps)) {+ case 'combo':+ return <BillingComboChart chartProps={chartProps} hiddenKeys={hiddenKeys} />+ case 'bar':+ return <BillingBarChart chartProps={chartProps} hiddenKeys={hiddenKeys} />+ case 'line':+ return <BillingLineChart chartProps={chartProps} hiddenKeys={hiddenKeys} />}- if (canRenderSqlBarGraph(chartProps)) {- return <BillingBarChart chartProps={chartProps} hiddenKeys={hiddenKeys} />- }- return <BillingLineChart chartProps={chartProps} hiddenKeys={hiddenKeys} />}// One subcomponent per chart kind because useBillingChartModel's config type follows the builder it's given.