: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(insights): gate trends chart empty state on all series, not just the first (#78606)
posthog[bot]c5f0e0d5b1c3
diff --git a/products/product_analytics/frontend/insights/shared/hasTrendsChartData.test.ts b/products/product_analytics/frontend/insights/shared/hasTrendsChartData.test.tsnew file mode 100644index 000000000000..c6e8f0b87e2c--- /dev/null+++ b/products/product_analytics/frontend/insights/shared/hasTrendsChartData.test.ts@@ -0,0 +1,60 @@+import type { IndexedTrendResult } from 'scenes/trends/types'++import { hasTrendsChartData } from './hasTrendsChartData'++function makeResult(overrides: Partial<IndexedTrendResult> = {}): IndexedTrendResult {+ return {+ action: null,+ count: 0,+ data: [],+ days: [],+ labels: [],+ label: '',+ aggregated_value: 0,+ seriesIndex: 0,+ id: 0,+ colorIndex: 0,+ ...overrides,+ }+}++// `TrendResult['data']` is typed as non-nullable `number[]`, but the aggregated-formula path on+// the backend really does send `data: None` over the wire — the `!!result.data` guard in+// `hasTrendsChartData` exists for that case. Cast at the call site (not by widening the shared+// type) to model the real, type-violating payload without loosening `data` for every consumer.+const withMissingData = (overrides: Partial<IndexedTrendResult> = {}): IndexedTrendResult =>+ makeResult({ data: undefined, ...overrides } as Partial<IndexedTrendResult>)++describe('hasTrendsChartData', () => {+ it.each<[string, IndexedTrendResult[] | undefined, boolean]>([+ ['undefined result set', undefined, false],+ ['empty result set', [], false],+ ['every series empty', [makeResult({ data: [0, 0], count: 0 }), makeResult({ data: [0, 0], count: 0 })], false],+ [+ 'a later series has data, even if the first series is empty',+ [makeResult({ data: [0, 0, 0], count: 0 }), makeResult({ data: [1, 2, 3], count: 6 })],+ true,+ ],+ [+ 'non-zero aggregated value even with a missing data array (aggregated query shape)',+ [withMissingData({ count: 0, aggregated_value: 42 })],+ true,+ ],+ ['non-finite aggregated value is ignored', [withMissingData({ count: 0, aggregated_value: NaN })], false],+ ['non-zero count with a missing data array', [withMissingData({ count: 5 })], false],+ // Pins the union's current semantics for the ORed contracts (see hasTrendsChartData.ts):+ // an aggregated-value-zero result with a non-zero count still counts as having data.+ [+ 'an aggregated-value-zero result with a non-zero count is still treated as having data',+ [makeResult({ aggregated_value: 0, count: 5, data: [] })],+ true,+ ],+ [+ 'lifecycle dormant series carries a negative count and still counts as data',+ [makeResult({ data: [-3, -1, 0], count: -4 })],+ true,+ ],+ ])('%s', (_name, results, expected) => {+ expect(hasTrendsChartData(results)).toBe(expected)+ })+})diff --git a/products/product_analytics/frontend/insights/shared/hasTrendsChartData.ts b/products/product_analytics/frontend/insights/shared/hasTrendsChartData.tsnew file mode 100644index 000000000000..55d9c2a5509d--- /dev/null+++ b/products/product_analytics/frontend/insights/shared/hasTrendsChartData.ts@@ -0,0 +1,16 @@+import type { IndexedTrendResult } from 'scenes/trends/types'++// Gate the empty state on the whole result set, not just the first series — trendsDataLogic+// re-sorts and interleaves results for compare mode, breakdown ordering, and unstacked bars, so+// index 0 isn't a stable "does this tile have data" signal.+export function hasTrendsChartData(indexedResults: IndexedTrendResult[] | undefined): boolean {+ if (!indexedResults?.length) {+ return false+ }+ return indexedResults.some((result: IndexedTrendResult) => {+ if (Number.isFinite(result.aggregated_value) && result.aggregated_value !== 0) {+ return true+ }+ return !!result.data && result.count !== 0+ })+}diff --git a/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.tsx b/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.tsxindex de562a7fe7e6..09dfda8a27d2 100644--- a/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.tsx+++ b/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.tsx@@ -20,6 +20,7 @@ import { InsightVizNode } from '~/queries/schema/schema-general'import { QueryContext } from '~/queries/types'import { ChartDisplayType } from '~/types'+import { hasTrendsChartData } from '../../shared/hasTrendsChartData'import { InsightSeriesTooltip } from '../../shared/InsightSeriesTooltip'import { makeChartErrorHandler } from '../../trends/shared/chartErrorHandler'import { getTrendsSeriesDisplayLabel } from '../../trends/shared/getTrendsSeriesDisplayLabel'@@ -88,7 +89,7 @@ export function StickinessBarChart({ context }: StickinessBarChartProps): JSX.Elconst bucketCount = currentPeriodResult?.labels?.length ?? 0const labels = useMemo(() => buildStickinessLabels(bucketCount, interval), [bucketCount, interval])- const hasData = (indexedResults ?? []).some((r: IndexedTrendResult) => r.count !== 0)+ const hasData = hasTrendsChartData(indexedResults)// `TimeSeriesBarChart` has a single y-axis — `showMultipleYAxes` is intentionally not forwarded.const series: Series<TrendsSeriesMeta>[] = useMemo(diff --git a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.tsx b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.tsxindex c1e26b40a029..c954ce20c03a 100644--- a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.tsx+++ b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.tsx@@ -20,6 +20,7 @@ import { InsightVizNode } from '~/queries/schema/schema-general'import { QueryContext } from '~/queries/types'import { chartStyleCurve } from '../../shared/chartStyleAdapter'+import { hasTrendsChartData } from '../../shared/hasTrendsChartData'import { InsightSeriesTooltip } from '../../shared/InsightSeriesTooltip'import { makeChartErrorHandler } from '../../trends/shared/chartErrorHandler'import { getTrendsSeriesDisplayLabel } from '../../trends/shared/getTrendsSeriesDisplayLabel'@@ -88,10 +89,7 @@ export function StickinessLineChart({ context }: StickinessLineChartProps): JSX.const bucketCount = currentPeriodResult?.labels?.length ?? 0const labels = useMemo(() => buildStickinessLabels(bucketCount, interval), [bucketCount, interval])- const hasData =- indexedResults &&- indexedResults[0]?.data &&- indexedResults.filter((result: IndexedTrendResult) => result.count !== 0).length > 0+ const hasData = hasTrendsChartData(indexedResults)const series: Series<TrendsSeriesMeta>[] = useMemo(() =>diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsxindex d536afe1ea88..9b943f81bb6e 100644--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx@@ -31,6 +31,7 @@ import { QueryContext } from '~/queries/types'import { getStackBreakdownValues } from '~/queries/utils'import { ChartDisplayType } from '~/types'+import { hasTrendsChartData } from '../../shared/hasTrendsChartData'import { InsightSeriesTooltip } from '../../shared/InsightSeriesTooltip'import { INSIGHT_TOOLTIP_CONFIG } from '../../shared/tooltipConfig'import { AnnotationsLayer } from '../shared/AnnotationsLayer'@@ -129,13 +130,7 @@ export function TrendsBarChart({const resolvedGroupTypeLabel = resolveGroupTypeLabel(labelGroupType, aggregationLabel, context?.groupTypeLabel)- const hasData =- !!indexedResults?.[0] &&- (isAggregated- ? indexedResults.some(- (r: IndexedTrendResult) => Number.isFinite(r.aggregated_value) && r.aggregated_value !== 0- )- : !!indexedResults[0].data && indexedResults.some((r: IndexedTrendResult) => r.count !== 0))+ const hasData = hasTrendsChartData(indexedResults)const stackBreakdowns = !!querySource && !!getStackBreakdownValues(querySource)diff --git a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsxindex 3f014d558e45..1713435239d7 100644--- a/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsLifecycleChart/TrendsLifecycleChart.tsx@@ -19,6 +19,7 @@ import { InsightVizNode } from '~/queries/schema/schema-general'import { QueryContext } from '~/queries/types'import type { LifecycleToggle } from '~/types'+import { hasTrendsChartData } from '../../shared/hasTrendsChartData'import { InsightSeriesTooltip } from '../../shared/InsightSeriesTooltip'import { AnnotationsLayer } from '../shared/AnnotationsLayer'import { buildBaseLegendConfig } from '../shared/buildBaseLegendConfig'@@ -80,10 +81,7 @@ export function TrendsLifecycleChart({ context, inSharedMode = false }: TrendsLiconst isStacked = lifecycleFilter?.stacked ?? true- const hasData =- !!indexedResults?.[0] &&- !!indexedResults[0].data &&- indexedResults.some((r: IndexedTrendResult) => r.count !== 0)+ const hasData = hasTrendsChartData(indexedResults)const formatValue = useCallback((value: number) => formatAggregationAxisValue(trendsFilter, value, baseCurrency),diff --git a/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsx b/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsxindex fa968bd59227..161717ffab79 100644--- a/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsx@@ -584,6 +584,22 @@ describe('TrendsLineChart', () => {expect(screen.queryByLabelText(/chart with/i)).not.toBeInTheDocument()})+ it('renders the chart when the first series is empty but a later one has data', async () => {+ // Regresses the bug this PR fixes: the old check only looked at+ // indexedResults[0], so a leading empty series blanked the whole chart even when a+ // later series (here, ActiveSeries) had real counts.+ renderInsight({+ query: buildTrendsQuery({+ series: [{ kind: NodeKind.EventsNode, event: 'ZeroCounts', name: 'ZeroCounts' }],+ }),+ })++ await waitFor(() => {+ expect(screen.getByLabelText(/chart with/i)).toBeInTheDocument()+ })+ expect(screen.queryByTestId('insight-empty-state')).not.toBeInTheDocument()+ })+it('uses context.emptyStateHeading override when provided', async () => {renderInsight({query: buildTrendsQuery({diff --git a/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.tsxindex e56c46c75555..57dbd384e405 100644--- a/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.tsx@@ -27,6 +27,7 @@ import { QueryContext } from '~/queries/types'import { ChartDisplayType } from '~/types'import { chartStyleCurve } from '../../shared/chartStyleAdapter'+import { hasTrendsChartData } from '../../shared/hasTrendsChartData'import { InsightSeriesTooltip } from '../../shared/InsightSeriesTooltip'import { INSIGHT_TOOLTIP_CONFIG } from '../../shared/tooltipConfig'import { AnnotationsLayer } from '../shared/AnnotationsLayer'@@ -104,10 +105,7 @@ export function TrendsLineChart({const labels = currentPeriodResult?.labels ?? []- const hasData =- indexedResults &&- indexedResults[0]?.data &&- indexedResults.filter((result: IndexedTrendResult) => result.count !== 0).length > 0+ const hasData = hasTrendsChartData(indexedResults)const valueLabelFormatter = useCallback((value: number) => {