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-viz): dual y-axis, trend lines, and stacked tooltip fix for SQL charts (#67494)

Sam Penningtonb8a363bbc450
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.test.ts
index 2f55067dc0fd..0ac4d730e63a 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.test.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.test.ts
@@ -28,7 +28,7 @@ describe('sqlChartComponentFor', () => {
['bar', ChartDisplayType.ActionsBar, true, 'SqlBarGraph'],
['line with the flag off', ChartDisplayType.ActionsLineGraph, false, 'LegacyLineGraph'],
// Pie is not routed here — it has its own wrapper (see PieChart.test.tsx).
- ['pie (handled by the PieChart wrapper, not here)', ChartDisplayType.ActionsPie, true, 'LegacyLineGraph'],
+ ['pie (handled by the PieChart wrapper, not here)', ChartDisplayType.ActionsPie, true, 'SqlLineGraph'],
])('routes %s to the right component', (_name, visualizationType, newChartsEnabled, expected) => {
expect(sqlChartComponentFor(baseProps(visualizationType), newChartsEnabled).name).toBe(expected)
})
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.tsx
index 493f83e852f5..f6cbe35524ca 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.tsx
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/LineGraph.tsx
@@ -52,7 +52,6 @@ import {
AREA_FILL_OPACITY,
canRenderSqlBarGraph,
canRenderSqlComboGraph,
- canRenderSqlLineGraph,
capYSeriesData,
exceedsMaxSeries,
isAreaSeries,
@@ -743,16 +742,16 @@ export function sqlChartComponentFor(
props: LineGraphProps,
newChartsEnabled: boolean
): (props: LineGraphProps) => JSX.Element {
- if (newChartsEnabled && canRenderSqlLineGraph(props)) {
- return SqlLineGraph
+ if (!newChartsEnabled) {
+ return LegacyLineGraph
}
- if (newChartsEnabled && canRenderSqlBarGraph(props)) {
- return SqlBarGraph
- }
- if (newChartsEnabled && canRenderSqlComboGraph(props)) {
+ if (canRenderSqlComboGraph(props)) {
return SqlComboGraph
}
- return LegacyLineGraph
+ if (canRenderSqlBarGraph(props)) {
+ return SqlBarGraph
+ }
+ return SqlLineGraph
}
export const LineGraph = (props: LineGraphProps): JSX.Element => {
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlBarGraph.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlBarGraph.tsx
index c969a5d13856..ada8defe3bc6 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlBarGraph.tsx
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlBarGraph.tsx
@@ -1,18 +1,27 @@
import clsx from 'clsx'
+import { useCallback } from 'react'
-import { TimeSeriesBarChart } from '@posthog/quill-charts'
+import { TimeSeriesBarChart, type PointClickData } from '@posthog/quill-charts'
import { makeChartErrorHandler } from 'products/product_analytics/frontend/insights/trends/shared/chartErrorHandler'
import { LineGraphProps } from './LineGraph'
-import { buildBarChartConfig } from './sqlLineGraphAdapter'
+import { type SqlLineSeriesMeta, buildBarChartConfig } from './sqlLineGraphAdapter'
import { useSqlChartModel } from './useSqlChartModel'
const handleChartError = makeChartErrorHandler('sql-bar-chart')
export const SqlBarGraph = (props: LineGraphProps): JSX.Element => {
+ const { onPointClick: onPointClickProp } = props
const model = useSqlChartModel(props, buildBarChartConfig)
+ const onPointClick = useCallback(
+ (data: PointClickData<SqlLineSeriesMeta>) => {
+ onPointClickProp?.(data.series.key, data.dataIndex, data.label)
+ },
+ [onPointClickProp]
+ )
+
return (
<div
className={clsx(
@@ -27,6 +36,7 @@ export const SqlBarGraph = (props: LineGraphProps): JSX.Element => {
labels={model.labels}
theme={model.theme}
config={model.config}
+ onPointClick={onPointClickProp ? onPointClick : undefined}
onError={handleChartError}
/>
)}
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts
index 9590b29058ca..90bfd36e178f 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts
@@ -114,21 +114,20 @@ describe('sqlLineGraphAdapter', () => {
)
})
- // Trend lines force the legacy fallback on the bar path because quill's
- // TimeSeriesBarChart has no trend-line support — keep this until it does.
+ // Trend lines render natively via TrendLineOverlay — no fallback needed.
it.each([
['a plain series', [ySeries('a', [1], { display: { trendLine: true } })]],
['a breakdown series', [breakdownSeries('chrome', [1], { display: { trendLine: true } })]],
- ])('falls back when %s has a trend line', (_name, yData) => {
+ ])('renders natively when %s has a trend line', (_name, yData) => {
expect(canRenderSqlBarGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(
- false
+ true
)
})
- it('falls back when any series targets the right y-axis', () => {
+ it('renders natively when any series targets the right y-axis', () => {
const yData = [ySeries('a', [1], { display: { yAxisPosition: 'right' } })]
expect(canRenderSqlBarGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(
- false
+ true
)
})
})
@@ -220,13 +219,13 @@ describe('sqlLineGraphAdapter', () => {
).toBe(false)
})
- it('falls back when any series has a trend line (no combo trend-line support)', () => {
+ 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(
- false
+ true
)
})
@@ -241,16 +240,6 @@ describe('sqlLineGraphAdapter', () => {
)
).toBe(false)
})
-
- it('falls back when any series targets the right y-axis', () => {
- const yData = [
- ySeries('a', [1], { display: { displayType: 'bar' } }),
- ySeries('b', [2], { display: { displayType: 'line', yAxisPosition: 'right' } }),
- ]
- expect(canRenderSqlComboGraph(baseProps({ visualizationType: ChartDisplayType.ActionsBar, yData }))).toBe(
- false
- )
- })
})
describe('comboBarLayoutForDisplay', () => {
@@ -708,7 +697,7 @@ describe('sqlLineGraphAdapter', () => {
timezone: 'UTC',
visualizationType: ChartDisplayType.ActionsStackedBar,
})
- expect(config.yAxis?.scale).toBe('linear')
+ expect((config.yAxis as YAxisConfig)?.scale).toBe('linear')
})
it('formats y-axis ticks with the first series column settings for plain bars', () => {
@@ -719,7 +708,7 @@ describe('sqlLineGraphAdapter', () => {
visualizationType: ChartDisplayType.ActionsBar,
ySeriesData: [ySeries('revenue', [1200], { formatting: { prefix: '$' } })],
})
- expect(config.yAxis?.tickFormatter?.(1200)).toBe('$1200')
+ expect((config.yAxis as YAxisConfig)?.tickFormatter?.(1200)).toBe('$1200')
})
it('skips the column tick formatter for percent-stacked bars (the axis is 0–100%)', () => {
@@ -730,10 +719,10 @@ describe('sqlLineGraphAdapter', () => {
visualizationType: ChartDisplayType.ActionsStackedBar,
ySeriesData: [ySeries('revenue', [1200], { formatting: { prefix: '$' } })],
})
- expect(config.yAxis?.tickFormatter).toBeUndefined()
+ expect((config.yAxis as YAxisConfig)?.tickFormatter).toBeUndefined()
})
- it('never emits trend lines (quill bar charts have no trend-line support)', () => {
+ it('emits trend line config so TrendLineOverlay can render it', () => {
const ySeriesData = [ySeries('a', [1, 2], { display: { trendLine: true } })]
const config = buildBarChartConfig({
xData: dateXData,
@@ -742,7 +731,35 @@ describe('sqlLineGraphAdapter', () => {
visualizationType: ChartDisplayType.ActionsBar,
ySeriesData,
})
- expect('trendLines' in config).toBe(false)
+ expect(config.trendLines?.length).toBeGreaterThan(0)
+ })
+
+ it('skips trend lines for percent-stacked bars, where they would render off-scale', () => {
+ const ySeriesData = [ySeries('a', [1, 2], { display: { trendLine: true } })]
+ const config = buildBarChartConfig({
+ xData: dateXData,
+ chartSettings: { stackBars100: true },
+ timezone: 'UTC',
+ visualizationType: ChartDisplayType.ActionsStackedBar,
+ ySeriesData,
+ })
+ expect(config.trendLines).toEqual([])
+ })
+
+ it('emits a per-axis array with forceLinear threaded through both gutters when a series targets the right axis', () => {
+ const ySeriesData = [ySeries('a', [1, 2]), ySeries('b', [3, 4], { display: { yAxisPosition: 'right' } })]
+ const config = buildBarChartConfig({
+ xData: dateXData,
+ chartSettings: { stackBars100: true, leftYAxisSettings: { scale: 'logarithmic' } },
+ timezone: 'UTC',
+ visualizationType: ChartDisplayType.ActionsStackedBar,
+ ySeriesData,
+ })
+ // forceLinear (percent layout) overrides the logarithmic setting on both gutters.
+ expect(config.yAxis).toMatchObject([
+ { id: 'left', position: 'left', scale: 'linear' },
+ { id: 'right', position: 'right', scale: 'linear' },
+ ])
})
})
@@ -780,14 +797,14 @@ describe('sqlLineGraphAdapter', () => {
expect(config.tooltip).toMatchObject({ enabled: true, pinnable: true })
})
- it('never emits trend lines (combo charts have no trend-line support)', () => {
+ it('emits trendLines config (TrendLineOverlay renders them on combo charts)', () => {
const config = buildComboChartConfig({
xData: dateXData,
chartSettings: {},
timezone: 'UTC',
visualizationType: ChartDisplayType.ActionsBar,
})
- expect('trendLines' in config).toBe(false)
+ expect('trendLines' in config).toBe(true)
})
it('honors leftYAxisSettings for the single y-axis', () => {
@@ -802,5 +819,20 @@ describe('sqlLineGraphAdapter', () => {
})
expect(config.yAxis).toEqual({ label: 'Combo Left', scale: 'log', showGrid: false, hide: false })
})
+
+ it('emits a per-axis array when a series targets the right axis', () => {
+ const ySeriesData = [ySeries('a', [1, 2]), ySeries('b', [3, 4], { display: { yAxisPosition: 'right' } })]
+ const config = buildComboChartConfig({
+ xData: dateXData,
+ chartSettings: {},
+ timezone: 'UTC',
+ visualizationType: ChartDisplayType.ActionsBar,
+ ySeriesData,
+ })
+ expect(config.yAxis).toMatchObject([
+ { id: 'left', position: 'left' },
+ { id: 'right', position: 'right' },
+ ])
+ })
})
})
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts
index 343b0ecf95c5..29347b817682 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts
@@ -136,13 +136,6 @@ export function canRenderSqlBarGraph(props: LineGraphProps): boolean {
) {
return false
}
- // quill's TimeSeriesBarChart has no trend-line support yet — fall back until it does.
- if (yData?.some((series) => series.settings?.display?.trendLine)) {
- return false
- }
- if (yData?.some((series) => series.settings?.display?.yAxisPosition === 'right')) {
- return false
- }
return true
}
@@ -165,17 +158,10 @@ export function canRenderSqlComboGraph(props: LineGraphProps): boolean {
if (!yData || !hasMixedSeriesTypes(yData, visualizationType)) {
return false
}
- // ComboChart has no trend-line support yet (same limitation as the bar path).
- if (yData.some((series) => series.settings?.display?.trendLine)) {
- return false
- }
// ComboChart supports only stacked/grouped bars, not percent (stackBars100).
if (barLayoutForDisplay(visualizationType, chartSettings) === 'percent') {
return false
}
- if (yData.some((series) => series.settings?.display?.yAxisPosition === 'right')) {
- return false
- }
return true
}
@@ -439,19 +425,33 @@ export function buildBarChartConfig({
}: BuildBarConfigArgs): TimeSeriesBarChartConfig & { yAxis?: YAxisConfig } {
const barLayout = barLayoutForDisplay(visualizationType, chartSettings)
const labelFormatter = buildSqlDateLabelFormatter(xData, timezone)
+ const leftSeries = seriesForAxis(ySeriesData, 'left')
+ const rightSeries = seriesForAxis(ySeriesData, 'right')
return {
xAxis: buildXAxisConfig(xData, chartSettings, timezone),
- yAxis: buildYAxisConfig(
- chartSettings.leftYAxisSettings,
- seriesForAxis(ySeriesData, 'left'),
- chartSettings.yAxisAtZero,
- {
- forceLinear: barLayout === 'percent',
- }
- ),
+ yAxis:
+ rightSeries.length > 0
+ ? [
+ buildYAxisConfig(chartSettings.leftYAxisSettings, leftSeries, chartSettings.yAxisAtZero, {
+ id: 'left',
+ position: 'left',
+ forceLinear: barLayout === 'percent',
+ }),
+ buildYAxisConfig(chartSettings.rightYAxisSettings, rightSeries, chartSettings.yAxisAtZero, {
+ id: 'right',
+ position: 'right',
+ forceLinear: barLayout === 'percent',
+ }),
+ ]
+ : buildYAxisConfig(chartSettings.leftYAxisSettings, leftSeries, chartSettings.yAxisAtZero, {
+ forceLinear: barLayout === 'percent',
+ }),
goalLines: schemaGoalLinesToConfigs(goalLines),
barLayout,
+ // Percent bars scale against a [0, 1] domain; trend lines plot raw series values, so they'd
+ // render off-scale and invisible.
+ trendLines: barLayout === 'percent' ? [] : buildTrendLineConfigs(ySeriesData),
legend: buildLegendConfig(chartSettings),
valueLabels: buildValueLabelsConfig(chartSettings, ySeriesData),
tooltip: { enabled: true, pinnable: true, placement: 'cursor', ...(labelFormatter ? { labelFormatter } : {}) },
@@ -468,15 +468,27 @@ export function buildComboChartConfig({
}: BuildBarConfigArgs): TimeSeriesComboChartConfig & { yAxis?: YAxisConfig } {
const labelFormatter = buildSqlDateLabelFormatter(xData, timezone)
+ const leftSeries = seriesForAxis(ySeriesData, 'left')
+ const rightSeries = seriesForAxis(ySeriesData, 'right')
+
return {
xAxis: buildXAxisConfig(xData, chartSettings, timezone),
- yAxis: buildYAxisConfig(
- chartSettings.leftYAxisSettings,
- seriesForAxis(ySeriesData, 'left'),
- chartSettings.yAxisAtZero
- ),
+ yAxis:
+ rightSeries.length > 0
+ ? [
+ buildYAxisConfig(chartSettings.leftYAxisSettings, leftSeries, chartSettings.yAxisAtZero, {
+ id: 'left',
+ position: 'left',
+ }),
+ buildYAxisConfig(chartSettings.rightYAxisSettings, rightSeries, chartSettings.yAxisAtZero, {
+ id: 'right',
+ position: 'right',
+ }),
+ ]
+ : buildYAxisConfig(chartSettings.leftYAxisSettings, leftSeries, chartSettings.yAxisAtZero),
goalLines: schemaGoalLinesToConfigs(goalLines),
barLayout: comboBarLayoutForDisplay(visualizationType),
+ trendLines: buildTrendLineConfigs(ySeriesData),
legend: buildLegendConfig(chartSettings),
valueLabels: buildValueLabelsConfig(chartSettings, ySeriesData),
tooltip: {