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): truncate long SQL chart labels (#93354)

Sam Pennington3eab7000a702
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 5cf6193dbc89..0b11a5b933f9 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -1049,9 +1049,9 @@ snapshots:
components-hogcharts-timeseriesbarchart--pinned-value-domain--light:
hash: v1.k794b7964.efbea7e65b8b73c11114eabde48afb7f9832e2db235b9e41feffa087728cdeb1.2aK7Wuhumf_-_4qQPTwIPGXdfE8tdhYfyzBi4Ae9OOI
components-hogcharts-timeseriesbarchart--rotated-category-labels--dark:
- hash: v1.k794b7964.421b4b6f115efaf09da9aaeec399333439ee3227961d21b820d26692aba8d499.J4_LjrWMsXTd-NjAeWiphcLZbDDbAp1eOs0vnPO0eYM
+ hash: v1.k794b7964.70db253467238152c98daab686316ce5d83e4d5ec7d7463fa4a8f691038dc329.4FQHj7D1Dt9ilK4eoAdTGbL5Ktm51tADu4dws63thBk
components-hogcharts-timeseriesbarchart--rotated-category-labels--light:
- hash: v1.k794b7964.def3ffc157f845eb1d8f51289c8f668cad913f88cb2e29773f63ecaa2575c666.E64UCu2ptxFlJriQF_Qowf6dKncjrkbICHgYHoCh6R4
+ hash: v1.k794b7964.3bbb8717f7b17a02acdba69f63c4813d8283ea1b928590c000419525fc4c60f5.6brV9nqf-jpraOAH5qg_GT_uTCTjgereLoWKIVeOLVM
components-hogcharts-timeseriesbarchart--y-axis-formats--dark:
hash: v1.k794b7964.66f0d459b4954743f10f429a64e3e7f6a74b26aaf42896449ee98ec5b8a5e783.bMjI6PrvYtjTBFJBN_l9tquTCL9z2MiPiACCyg7NwpM
components-hogcharts-timeseriesbarchart--y-axis-formats--light:
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 5ff7b819facd..86f73658fd5d 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.test.ts
@@ -1,4 +1,5 @@
import {
+ MAX_CATEGORY_LABEL_WIDTH,
type TooltipContext,
type TrendLineConfig,
type ValueLabelContext,
@@ -821,6 +822,7 @@ describe('sqlLineGraphAdapter', () => {
})
expect(config.xAxis?.tickLabelRotation).toBe(expected)
+ expect(config.maxCategoryLabelWidth).toBe(MAX_CATEGORY_LABEL_WIDTH)
})
it('forces a linear y-axis scale for percent-stacked bars', () => {
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts
index ddaaf116e50f..1806a22fdd1a 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/Charts/sqlLineGraphAdapter.ts
@@ -1,5 +1,6 @@
import { lemonToast } from '@posthog/lemon-ui'
import {
+ MAX_CATEGORY_LABEL_WIDTH,
type AxisLinesConfig,
type ChartLegendConfig,
type Series,
@@ -472,6 +473,7 @@ export function buildBarChartConfig({
return {
xAxis: buildXAxisConfig(xData, chartSettings, timezone, SQL_BAR_TICK_LABEL_ROTATION),
+ maxCategoryLabelWidth: MAX_CATEGORY_LABEL_WIDTH,
yAxis:
rightSeries.length > 0
? [
diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.md
index 874d1e859b77..b7747e31e951 100644
--- a/packages/quill/packages/charts/AGENTS.md
+++ b/packages/quill/packages/charts/AGENTS.md
@@ -50,7 +50,7 @@ const series: Series[] = [
- `labels` entries must be **unique** — the x-scale keys positions off the strings, so a duplicate collapses onto the first occurrence's position and draws the series backwards. On time-series charts pass ISO date strings (never formatted display labels like "1–7 Jun", which repeat across years) and format ticks via `xAxis.timezone`/`interval` or `tickFormatter`.
- Line-only options: `points`, `stroke.pattern` (dashes/projections), `fill.opacity` / `fill.gradient` (area), `fill.lowerData` (ribbon).
- Bar-only options: `bars` (per-bar `color`/`label`/`meta` overrides, plus `hatch: true` to fill that bar with a diagonal-hatch pattern in its resolved color — flags individual not-final bars, e.g. buckets still being ingested, at arbitrary indices; for a contiguous trailing range `stroke.partial.fromIndex` does the same); `trackData` caps each bar's interactive extent at a per-bar ceiling in value units — the region beyond `trackData[i]` is a fully inert gap: no hover, tooltip, pointer cursor, highlight, or click (the chart vetoes the hover there via `resolveHoverIndex`). On grouped charts with `bars.track` the hatched "share of a whole" track also fills only to the ceiling; on stacked charts no track is drawn — the ceiling only bounds interactivity. Funnel compare uses it to draw a shorter period's volume gap as empty space rather than drop-off, in both its grouped (left-to-right) and stacked (top-to-bottom) charts.
-- `BarChartConfig.bars.minBarSize` (px) floors each non-zero bar's thickness along the value axis so a single-occurrence bucket stays visible beside a spike three orders of magnitude taller (the old chart.js `minBarLength`) — zero-valued bars are never floored, so empty buckets stay empty. On a stacked chart only the outermost segment is floored — flooring an interior one would oversize a rect the segment above overpaints while still capturing its hover and clicks — so a breakdown stack floors only its top segment, and the option is aimed at single-series volume charts and grouped bars; `Sparkline` doesn't expose the prop. `TimeSeriesBarChart` takes `minBarSize`, `bandPadding`, and `margins` as top-level config keys (alongside `barCornerRadius`/`fillStyle`) rather than under `bars`.
+- `BarChartConfig.bars.minBarSize` (px) floors each non-zero bar's thickness along the value axis so a single-occurrence bucket stays visible beside a spike three orders of magnitude taller (the old chart.js `minBarLength`) — zero-valued bars are never floored, so empty buckets stay empty. On a stacked chart only the outermost segment is floored — flooring an interior one would oversize a rect the segment above overpaints while still capturing its hover and clicks — so a breakdown stack floors only its top segment, and the option is aimed at single-series volume charts and grouped bars; `Sparkline` doesn't expose the prop. `TimeSeriesBarChart` takes `minBarSize`, `bandPadding`, `margins`, and `maxCategoryLabelWidth` as top-level config keys (alongside `barCornerRadius`/`fillStyle`) rather than under `bars`.
- `ChartConfig.margins` overrides only the sides it sets — a side left `undefined` keeps the chart's computed margin, so an object built conditionally (`{ top: reserveOrUndefined }`) is safe. `SlopeChart` merges consumer margins over its computed label gutters with the same rule (`applyMarginOverride`), so an `undefined` side keeps the gutter its value/name labels reserved.
- `yAxisId` scales a series against a second axis; `meta` carries arbitrary data into tooltips.
diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.stories.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.stories.tsx
index e0db03d418fd..2343f24b67a5 100644
--- a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.stories.tsx
+++ b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.stories.tsx
@@ -78,7 +78,11 @@ export const RotatedCategoryLabels: Story = {
series={LONG_CATEGORY_SERIES}
labels={LONG_CATEGORY_LABELS}
theme={theme}
- config={{ xAxis: { tickLabelRotation: -45 }, yAxis: { showGrid: true } }}
+ config={{
+ xAxis: { tickLabelRotation: -45 },
+ yAxis: { showGrid: true },
+ maxCategoryLabelWidth: 160,
+ }}
/>
</Stage>
)
diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx
index a742edc2a616..7bf931a86500 100644
--- a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx
+++ b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx
@@ -64,6 +64,8 @@ export interface TimeSeriesBarChartConfig {
minBarSize?: number
/** Per-side overrides on the computed chart margins — see {@link ChartConfig.margins}. */
margins?: Partial<ChartMargins>
+ /** Max pixel width for category labels before truncation. See {@link ChartConfig.maxCategoryLabelWidth}. */
+ maxCategoryLabelWidth?: number
/** Ease the hover highlight in over this many ms (`true` = default duration). Omit to snap. */
animateHover?: boolean | number
/** Built-in legend with click-to-toggle series visibility. Hidden by default. */
@@ -119,6 +121,7 @@ export function TimeSeriesBarChart<Meta = unknown>({
bandPadding,
minBarSize,
margins,
+ maxCategoryLabelWidth,
animateHover,
legend,
trendLines,
@@ -148,6 +151,7 @@ export function TimeSeriesBarChart<Meta = unknown>({
const barChartConfig: BarChartConfig = {
margins,
+ maxCategoryLabelWidth,
yScaleType: primaryYAxis?.scale,
xTickFormatter,
xTickLabelRotation: xAxis?.tickLabelRotation,
diff --git a/packages/quill/packages/charts/src/overlays/AxisLabels.tsx b/packages/quill/packages/charts/src/overlays/AxisLabels.tsx
index c3ebd387c316..4b26022fd164 100644
--- a/packages/quill/packages/charts/src/overlays/AxisLabels.tsx
+++ b/packages/quill/packages/charts/src/overlays/AxisLabels.tsx
@@ -50,31 +50,11 @@ function truncateWithTitle(fullText: string, maxCategoryLabelWidth: number): { t
/** Greedily keep entries left→right. Horizontal labels use their measured text boxes; rotated
* labels use the minimum perpendicular separation between parallel lines of text. */
-function dropOverlappingLabels<T extends { text: string; x: number }>(
- candidates: T[],
- padding: number,
- tickLabelRotation = 0
-): T[] {
+function dropOverlappingLabels<T extends { text: string; x: number }>(candidates: T[], padding: number): T[] {
if (candidates.length === 0) {
return []
}
- const rotation = normalizeTickLabelRotation(tickLabelRotation)
- if (rotation !== 0) {
- // Perpendicular separation keeps the parallel lines of text from overlapping; `padding`
- // floors the along-axis spacing so the same caller-provided gap applies as in the flat path.
- const minimumGap = Math.max(minimumRotatedTickLabelGap(rotation), padding)
- const visible: T[] = []
- let lastX = -Infinity
- for (const candidate of candidates) {
- if (candidate.x >= lastX + minimumGap) {
- visible.push(candidate)
- lastX = candidate.x
- }
- }
- return visible
- }
-
const ctx = getTextMeasureCtx()
if (!ctx) {
return candidates
@@ -105,21 +85,27 @@ export function computeVisibleXLabels(
tickLabelRotation = 0
): XLabelCandidate[] {
const candidates: XLabelCandidate[] = []
+ const rotation = normalizeTickLabelRotation(tickLabelRotation)
+ const minimumGap = rotation === 0 ? 0 : Math.max(minimumRotatedTickLabelGap(rotation), CATEGORY_LABEL_PADDING)
+ let lastX = -Infinity
+
for (let i = 0; i < labels.length; i++) {
const x = xScale(labels[i])
if (x == null) {
continue
}
const fullText = formatter ? formatter(labels[i], i) : labels[i]
- if (fullText === null) {
+ if (fullText === null || (rotation !== 0 && x < lastX + minimumGap)) {
continue
}
- // The truncated text drives both display and overlap measurement below.
const { text, title } = truncateWithTitle(fullText, maxCategoryLabelWidth)
candidates.push({ index: i, text, title, x })
+ if (rotation !== 0) {
+ lastX = x
+ }
}
- return dropOverlappingLabels(candidates, CATEGORY_LABEL_PADDING, tickLabelRotation)
+ return rotation === 0 ? dropOverlappingLabels(candidates, CATEGORY_LABEL_PADDING) : candidates
}
interface ValueTickCandidate {