: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(charts): preserve shared SQL chart value domains (#117944)
Marius Andrae5f0f79d91cf
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 284425c0615f..fb2512afbdbe 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -1476,6 +1476,10 @@ snapshots:hash: v1.k794b7964.abb36f352932cf1ebbfb4c4267c3f0e5dfbf89cdbee7bfeaa5f04ad6a36cf91c.uWW2PK6INwUQRQLerG3WIftvpF1y8_ThERfFUk4IruIcomponents-hogcharts-timeseriescombochart--dual-y-axis--light:hash: v1.k794b7964.224ed925d58f3aaa3670535fda23aa26e4132b578fa844270d8ed44f38f77ecf.D72PsCXUb5FKgyhu7EUOxzQ2nLfWYOx95dmKTAw7H2w+ components-hogcharts-timeseriescombochart--shared-value-domain--dark:+ hash: v1.k794b7964.08219c59f23f020eadc1a51cdd7ee2c4223956dcc2d90516b89b8756def5b9fe.T-lvU5ZHfDCo_PWD7hj6NM7-b2jwXw3oa4Bpr6e0XPg+ components-hogcharts-timeseriescombochart--shared-value-domain--light:+ hash: v1.k794b7964.dc1d142541398db902f080303fa109c4817638f7434835bf4f64e21a6104dead.ez5V3lITuAL4WI1__7lMzCqaXj-Mvq94BfWEjN71IOocomponents-hogcharts-timeseriescombochart--stacked-bars-and-line--dark:hash: v1.k794b7964.2c28b0405bc0bc75bdca9d041071aa6e7a3cc08715786da6e76b1b91009c2329.6pDV8-cbIiNwaO3yxO3Om4fMOBVNpi0lTD0ArlJzekscomponents-hogcharts-timeseriescombochart--stacked-bars-and-line--light:diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsxindex d402bbd34070..e76ceecd4747 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/SqlChart.tsx@@ -1,6 +1,6 @@import type { ReactNode } from 'react'-import type { XAxisConfig, ChartMargins } from '@posthog/quill-charts'+import type { XAxisConfig, ChartMargins, ValueDomain } from '@posthog/quill-charts'import { ChartSettings, GoalLine } from '~/queries/schema/schema-general'import { ChartDisplayType } from '~/types'@@ -43,6 +43,8 @@ export type SqlChartProps = {labelFormatter?: (label: string) => stringxAxis?: XAxisConfigmargins?: Partial<ChartMargins>+ /** Shared bounds for comparable charts rendered from the same worksheet result. */+ valueDomain?: ValueDomainchildren?: ReactNode}diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/useSqlChartModel.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/useSqlChartModel.test.tsnew file mode 100644index 000000000000..6908c4a66b65--- /dev/null+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/useSqlChartModel.test.ts@@ -0,0 +1,44 @@+import { renderHook } from '@testing-library/react'++import { initKeaTests } from '~/test/init'+import { ChartDisplayType } from '~/types'++import { buildBarValueChartConfig } from './sqlLineGraphAdapter'+import { useSqlChartModel } from './useSqlChartModel'++it('keeps off-scale reference lines when horizontal bars have a partial shared domain', () => {+ initKeaTests()+ const { result } = renderHook(() =>+ useSqlChartModel(+ {+ visualizationType: ChartDisplayType.ActionsBarValue,+ chartSettings: {},+ xData: {+ column: {+ name: 'category',+ type: { name: 'STRING', isNumerical: false },+ label: 'Category',+ dataIndex: 0,+ },+ data: ['A', 'B'],+ },+ yData: [+ {+ column: {+ name: 'count',+ type: { name: 'INTEGER', isNumerical: true },+ label: 'Count',+ dataIndex: 1,+ },+ data: [10, 20],+ settings: {},+ },+ ],+ goalLines: [{ label: 'Target', value: 200, displayIfCrossed: true }],+ valueDomain: { min: 0, include: [100] },+ },+ buildBarValueChartConfig+ )+ )+ expect(result.current?.config.bars?.valueDomain).toEqual({ min: 0, include: [200, 100] })+})diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/useSqlChartModel.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/useSqlChartModel.tsindex c01d8c932d7f..0f7efaa893ef 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/useSqlChartModel.ts+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/useSqlChartModel.ts@@ -7,6 +7,9 @@ import {type TooltipConfig,type XAxisConfig,type ChartMargins,+ type YAxisConfig,+ type ValueDomain,+ type BarsConfig,} from '@posthog/quill-charts'import { useChartTheme, useChartConfig } from 'lib/charts/hooks'@@ -34,7 +37,14 @@ export interface SqlChartModel<TConfig> {}export function useSqlChartModel<- TConfig extends { tooltip?: TooltipConfig; xAxis?: XAxisConfig; margins?: Partial<ChartMargins> },+ TConfig extends {+ tooltip?: TooltipConfig+ xAxis?: XAxisConfig+ yAxis?: YAxisConfig | YAxisConfig[]+ valueDomain?: ValueDomain+ bars?: BarsConfig+ margins?: Partial<ChartMargins>+ },>({xData,@@ -48,6 +58,7 @@ export function useSqlChartModel<labelFormatter,xAxis,margins,+ valueDomain,}: SqlChartProps,buildConfig: (args: BuildBarConfigArgs) => TConfig): SqlChartModel<TConfig> | null {@@ -98,6 +109,31 @@ export function useSqlChartModel<: undefinedreturn {...config,+ ...(valueDomain+ ? {+ valueDomain,+ ...(visualizationType === ChartDisplayType.ActionsBarValue+ ? {+ bars: {+ ...config.bars,+ valueDomain: {+ ...config.bars?.valueDomain,+ ...valueDomain,+ include: [+ ...(config.bars?.valueDomain?.include ?? []),+ ...(valueDomain.include ?? []),+ ],+ },+ },+ }+ : {}),+ yAxis: Array.isArray(config.yAxis)+ ? config.yAxis.map((axis, index) =>+ index === 0 ? { ...axis, min: valueDomain.min, max: valueDomain.max } : axis+ )+ : { ...config.yAxis, min: valueDomain.min, max: valueDomain.max },+ }+ : {}),...(formatLabel && visualizationType === ChartDisplayType.ActionsBarValue? { xTickFormatter: formatLabel }: {}),@@ -124,6 +160,7 @@ export function useSqlChartModel<labelFormatter,xAxis,margins,+ valueDomain,])const labels = useMemo(diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.stories.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.stories.tsxindex ce202af252a0..f7a31e9dc4bf 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.stories.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.stories.tsx@@ -43,6 +43,27 @@ export const Basic: Story = {},}+export const SharedValueDomain: Story = {+ render: () => {+ const theme = useReactiveTheme()+ return (+ <Stage>+ <TimeSeriesComboChart+ series={BAR_AND_LINE}+ labels={MONTHLY_LABELS}+ theme={theme}+ config={{+ valueDomain: { min: 0, max: 500 },+ xAxis: { timezone: 'UTC', interval: 'month' },+ yAxis: { showGrid: true },+ legend: { show: true },+ }}+ />+ </Stage>+ )+ },+}+export const StackedBarsAndLine: Story = {render: () => {const theme = useReactiveTheme()diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.test.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.test.tsxindex 748f75f0d43a..a9e5384d45bd 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.test.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.test.tsx@@ -30,7 +30,12 @@ describe('TimeSeriesComboChart', () => {return null}renderHogChart(- <TimeSeriesComboChart series={BAR_AND_LINE} labels={LABELS} theme={THEME} config={{ margins: { bottom: 76 } }}>+ <TimeSeriesComboChart+ series={BAR_AND_LINE}+ labels={LABELS}+ theme={THEME}+ config={{ margins: { bottom: 76 } }}+ ><Measure /></TimeSeriesComboChart>)@@ -100,6 +105,20 @@ describe('TimeSeriesComboChart', () => {})describe('config.yAxis', () => {+ it('pins the value domain even when a goal line extends beyond it', () => {+ const { chart } = renderHogChart(+ <TimeSeriesComboChart+ series={BAR_AND_LINE}+ labels={LABELS}+ theme={THEME}+ config={{ valueDomain: { min: 0, max: 100 }, goalLines: [{ value: 200, label: 'Target' }] }}+ />+ )+ const ticks = chart.yTicks().map(Number)+ expect(Math.min(...ticks)).toBe(0)+ expect(Math.max(...ticks)).toBe(100)+ })+it('hides y-axis ticks when yAxis.hide is true', () => {const { chart } = renderHogChart(<TimeSeriesComboChartdiff --git a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsxindex ea92e1a0b066..db287d8cf841 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsx@@ -15,7 +15,7 @@ import type {import { ReferenceLines } from '../../overlays/ReferenceLine'import { TrendLineOverlay } from '../../overlays/TrendLineOverlay'import { ValueLabels } from '../../overlays/ValueLabels'-import type { GoalLineConfig } from '../../utils/goal-lines'+import { mergeValueDomains, type GoalLineConfig } from '../../utils/goal-lines'import { useTimeSeriesTooltipConfig, type XAxisConfig, type YAxisConfig } from '../../utils/use-axis-formatters'import { ComboChart } from '../ComboChart/ComboChart'import { useTrendLineSeries, type TrendLineConfig } from '../utils/use-derived-series'@@ -28,6 +28,8 @@ export interface TimeSeriesComboChartConfig {xAxis?: XAxisConfig/** Single object for a standard left axis; array for dual left+right axes. */yAxis?: YAxisConfig | YAxisConfig[]+ /** Pin the primary value scale, overriding its automatic data and goal-line extent. */+ valueDomain?: ComboChartConfig['valueDomain']valueLabels?: boolean | ValueLabelsConfiggoalLines?: GoalLineConfig[]/** Type used for series that don't set {@link Series.type}. Defaults to `'line'`. */@@ -143,7 +145,7 @@ export function TimeSeriesComboChart<Meta = unknown>({divergingStack,barCornerRadius,tooltip: timeSeriesTooltipConfig,- valueDomain,+ valueDomain: mergeValueDomains(config?.valueDomain, valueDomain),yAxes,}diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsxindex 3cfd844862b6..328014c43831 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsx@@ -532,6 +532,18 @@ describe('TimeSeriesLineChart', () => {expect(lines[0].label).toBe('Target')})+ it('includes shared domain values beyond the line data', () => {+ const { chart } = renderHogChart(+ <TimeSeriesLineChart+ series={[{ key: 'a', label: 'A', data: [10, 20, 30] }]}+ labels={LABELS}+ theme={THEME}+ config={{ valueDomain: { min: 0, include: [100] } }}+ />+ )+ expect(Math.max(...chart.yTicks().map(Number))).toBeGreaterThanOrEqual(100)+ })+it('extends the value axis so a goal line above the data still renders', () => {const { chart } = renderHogChart(<TimeSeriesLineChartdiff --git a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsxindex 0d8727982160..7a509286a6ad 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx@@ -29,6 +29,7 @@ import type { ValueLabelsConfig } from '../utils/use-value-labels'export type { ConfidenceIntervalConfig, MovingAverageConfig, TrendLineConfig }export interface TimeSeriesLineChartConfig {+ valueDomain?: LineChartConfig['valueDomain']/** Reserve space for custom overlays; unspecified sides keep their computed margins. */margins?: LineChartConfig['margins']xAxis?: XAxisConfig@@ -136,7 +137,10 @@ export function TimeSeriesLineChart<Meta = unknown>({const { referenceLines, valueDomain: goalLineDomain } = useGoalLines(goalLines, finalSeries)const boundsDomain = useValueBounds(primaryYAxis)// A capped axis still has to stretch to reach an off-scale goal line.- const valueDomain = useMemo(() => mergeValueDomains(goalLineDomain, boundsDomain), [goalLineDomain, boundsDomain])+ const valueDomain = useMemo(+ () => mergeValueDomains(config?.valueDomain, mergeValueDomains(goalLineDomain, boundsDomain)),+ [config?.valueDomain, goalLineDomain, boundsDomain]+ )// `startAtZero === false` floats the primary axis to its data range; the default (undefined/true)// keeps the baseline clamped to 0. A log scale has no zero baseline to clamp, so it's a no-op there.diff --git a/packages/quill/packages/charts/src/docs/axes.md b/packages/quill/packages/charts/src/docs/axes.mdindex 7c0104ec2efd..329a6151e31c 100644--- a/packages/quill/packages/charts/src/docs/axes.md+++ b/packages/quill/packages/charts/src/docs/axes.md@@ -44,8 +44,9 @@ Duration formatters accept seconds (`duration`), milliseconds (`duration_ms`), o- The y-axis clamps a non-negative axis down to 0 by default. To float it to the data range, set `yAxis.startAtZero: false` on `TimeSeriesLineChart`, or `config.floatBaseline: true` on the lower-level `LineChart`. Ignored on a log scale and applied to the primary axis only. Bar charts always draw from zero.- `yAxis.min` and `max` pin either end on `TimeSeriesLineChart` (`config.valueDomain: { min, max }` on `LineChart`, the same option that carries `include`). Both set pins the axis: skips `nice()`, discards `include`, overrides percent layout. One set clamps that end and leaves the other automatic; it runs after `include` folding, the zero clamp, and `nice()`, so only the extreme label is the raw bound while interior ticks stay round. A single clamp is ignored under a percent layout, where the domain is already 0..1.- On a log scale a non-positive bound is dropped. A non-finite bound counts as unset, so `{ min: 0, max: Math.max(...[]) }` floors at zero rather than collapsing. An inverted `min >= max` pair falls back to the automatic domain rather than swapping.+- `TimeSeriesLineChart` also accepts `config.valueDomain` for shared bounds and `include` values. Explicit domain bounds take precedence over `yAxis.min/max`; include values merge with reference-line extents.- Overlays positioned off the value scale hide themselves outside a bounded window rather than painting over the chrome: a goal line drops out, and so does the value label of a clipped point. A point sitting exactly on the bound keeps its label.-- Bar and combo charts ignore `min` and `max` even though they take the same `yAxis` config. A bar encodes magnitude as length from zero, so a bounded baseline misreads it: 10 vs 11 would draw as 1 vs 2. Pin `valueDomain` there instead, which rescales the bars rather than truncating them. `TimeSeriesBarChart` takes `config.valueDomain`; setting both `min` and `max` skips `nice()` and wins over the goal-line stretch. Pin `{ min: 0, max: dataMax }` so the tallest bar reaches the plot top on an axis-less volume chart, where nice-rounded headroom is invisible waste. `include` merges with the goal-line stretch (`mergeValueDomains` in `utils/goal-lines.ts`).+- Bar and combo charts ignore `min` and `max` even though they take the same `yAxis` config. A bar encodes magnitude as length from zero, so a bounded baseline misreads it: 10 vs 11 would draw as 1 vs 2. Pin `valueDomain` there instead, which rescales the bars rather than truncating them. `TimeSeriesBarChart` and `TimeSeriesComboChart` take `config.valueDomain`; setting both `min` and `max` skips `nice()` and wins over the goal-line stretch. Pin `{ min: 0, max: dataMax }` so the tallest bar reaches the plot top on an axis-less volume chart, where nice-rounded headroom is invisible waste. `include` merges with the goal-line stretch (`mergeValueDomains` in `utils/goal-lines.ts`).## Multiple y-axes