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.

fix(insights): render negative values in stacked hog-charts bar charts (#61390)

Christiaan Hendriksenefebe51cce58
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx
index bd68b88b8e3a..31605e553bdd 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx
@@ -251,7 +251,9 @@ export function TrendsBarChart({
// Dashboard/card tiles are a fixed height, so cap the rows to those that fit. The full
// insight page is `embedded: false` — even when opened from a dashboard (dashboardId in
// the URL) — so it keeps the grow-to-fit-all behavior and renders every breakdown row.
- bars: { fitToHeight: embedded },
+ // divergingStack keeps negative values (e.g. a `A*(-1)` formula) below the zero baseline
+ // instead of clamping them to 0.
+ bars: { fitToHeight: embedded, divergingStack: true },
}
}, [
yAxisScaleType,
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts
index 381821cf9444..ebbac2e0a8bb 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts
+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.test.ts
@@ -247,15 +247,16 @@ describe('buildTrendsBarAggregatedSeries', () => {
describe('buildTrendsBarTimeSeriesConfig', () => {
it.each([
- { isPercentStackView: false, isGrouped: false, expected: 'stacked' },
- { isPercentStackView: false, isGrouped: true, expected: 'grouped' },
- { isPercentStackView: true, isGrouped: false, expected: 'percent' },
- { isPercentStackView: true, isGrouped: true, expected: 'percent' },
+ { isPercentStackView: false, isGrouped: false, expected: 'stacked', expectedDiverging: true },
+ { isPercentStackView: false, isGrouped: true, expected: 'grouped', expectedDiverging: false },
+ { isPercentStackView: true, isGrouped: false, expected: 'percent', expectedDiverging: false },
+ { isPercentStackView: true, isGrouped: true, expected: 'percent', expectedDiverging: false },
])(
- 'maps isPercentStackView=$isPercentStackView / isGrouped=$isGrouped to barLayout=$expected',
- ({ isPercentStackView, isGrouped, expected }) => {
+ 'maps isPercentStackView=$isPercentStackView / isGrouped=$isGrouped to barLayout=$expected / divergingStack=$expectedDiverging',
+ ({ isPercentStackView, isGrouped, expected, expectedDiverging }) => {
const cfg = buildTrendsBarTimeSeriesConfig({ isPercentStackView, isGrouped })
expect(cfg.barLayout).toBe(expected)
+ expect(cfg.divergingStack).toBe(expectedDiverging)
}
)
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts
index 299867eca248..5f521e7987dc 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts
+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/trendsBarChartTransforms.ts
@@ -113,6 +113,9 @@ export function buildTrendsBarTimeSeriesConfig(opts: BuildTrendsBarTimeSeriesCon
valueLabels: opts.valueLabels,
goalLines: goalLineConfigs,
barLayout: opts.isPercentStackView ? 'percent' : opts.isGrouped ? 'grouped' : 'stacked',
+ // Stacked bars must preserve negative values (e.g. a `A*(-1)` formula) so they render
+ // below the zero baseline instead of being clamped to 0. Only the stacked layout stacks.
+ divergingStack: !opts.isPercentStackView && !opts.isGrouped,
tooltip: opts.tooltip,
}
}