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(charts): widen horizontal bar labels with the chart width (#120367)

Sam Pennington721183a5c3bc
diff --git a/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsx b/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsx
index 0265489aa916..2336dd0461de 100644
--- a/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsx
+++ b/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsx
@@ -4,6 +4,7 @@ import type { BarChartConfig, ChartTheme, PointClickData, Series } from '../../c
import { ReferenceLine } from '../../overlays/ReferenceLine'
import { getHogChart, getHogChartTooltip, renderHogChart, waitForHogChartTooltip } from '../../testing'
import { dimensions } from '../../testing/jsdom'
+import { measureLabelWidth } from '../../utils/text-measure'
import { BarChart } from './BarChart'
const THEME: ChartTheme = {
@@ -181,6 +182,23 @@ describe('BarChart', () => {
expect(chart.yAxisLabel()).toBe('Series')
})
+ it.each<[Orientation, boolean]>([
+ ['horizontal', true],
+ ['vertical', false],
+ ])('%s: a wide chart raises the category label cap (shows full label: %s)', (axisOrientation, showsFull) => {
+ const label = 'filesystem::play_open_in_app'
+ const { chart } = renderHogChart(
+ <BarChart
+ series={[{ key: 'a', label: 'A', data: [10] }]}
+ labels={[label]}
+ theme={THEME}
+ config={{ axisOrientation, maxCategoryLabelWidth: measureLabelWidth(label) / 2 }}
+ />
+ )
+ const [tick] = axisOrientation === 'horizontal' ? chart.yTicks() : chart.xTicks()
+ expect(tick === label).toBe(showsFull)
+ })
+
it('renders without crashing in yScaleType log with positive data', () => {
const series: Series[] = [{ key: 'a', label: 'A', data: [1, 10, 100] }]
const { chart } = renderHogChart(
diff --git a/packages/quill/packages/charts/src/core/Chart.tsx b/packages/quill/packages/charts/src/core/Chart.tsx
index 4e31aa65d12b..4106299a6e92 100644
--- a/packages/quill/packages/charts/src/core/Chart.tsx
+++ b/packages/quill/packages/charts/src/core/Chart.tsx
@@ -1,4 +1,4 @@
-import React, { useMemo } from 'react'
+import React, { useLayoutEffect, useMemo, useState } from 'react'
import {
AxisLabels,
@@ -46,6 +46,8 @@ import { computeYAxisGutters, type Gutter } from './y-axis-gutters'
const DEFAULT_AXIS_COLOR = 'rgba(0, 0, 0, 0.5)'
const DEFAULT_HOVER_ANIMATION_MS = 150
+// Horizontal charts let category labels use this share of the chart width when it beats the fixed cap.
+const HORIZONTAL_CATEGORY_LABEL_WIDTH_RATIO = 0.3
function resolveHoverAnimationMs(animateHover: boolean | number | undefined): number {
if (animateHover === true) {
@@ -157,10 +159,17 @@ export function Chart<Meta = unknown>({
isPercent = false,
animateHover,
margins: marginsOverride,
- maxCategoryLabelWidth,
+ maxCategoryLabelWidth: baseMaxCategoryLabelWidth,
yAxes,
} = config ?? {}
+ // Width from the last layout. Margins feed the canvas size, so the cap can only react to it one render later.
+ const [chartWidth, setChartWidth] = useState(0)
+ const maxCategoryLabelWidth =
+ baseMaxCategoryLabelWidth && axisOrientation === 'horizontal'
+ ? Math.max(baseMaxCategoryLabelWidth, Math.floor(chartWidth * HORIZONTAL_CATEGORY_LABEL_WIDTH_RATIO))
+ : baseMaxCategoryLabelWidth
+
const {
formatters: yAxisFormatters,
positions: yAxisPositions,
@@ -229,6 +238,8 @@ export function Chart<Meta = unknown>({
})
const { canvasRef, overlayCanvasRef, wrapperRef, dimensions, ctx, overlayCtx } = useChartCanvas({ margins })
+ const containerWidth = dimensions?.width ?? 0
+ useLayoutEffect(() => setChartWidth(containerWidth), [containerWidth])
const coloredSeries = useColoredSeries<Meta>(series, theme)
diff --git a/packages/quill/packages/charts/src/core/types.ts b/packages/quill/packages/charts/src/core/types.ts
index 9e265eec9a39..eaf198974b2b 100644
--- a/packages/quill/packages/charts/src/core/types.ts
+++ b/packages/quill/packages/charts/src/core/types.ts
@@ -293,7 +293,9 @@ export interface ChartConfig {
margins?: Partial<ChartMargins>
/** Max pixel width for category (band) tick labels before they're truncated with an ellipsis,
* with the full value revealed on hover. Also clamps the axis margin to this width so a long
- * label can't push the plot off screen. Omit (default) to render labels untruncated. */
+ * label can't push the plot off screen. Horizontal charts raise the cap to 30% of the chart
+ * width when that is wider, so a wider chart shows more of each label. Omit (default) to
+ * render labels untruncated. */
maxCategoryLabelWidth?: number
/** Per-axis config for multi-axis (dual y-axis) charts — one entry per distinct
* {@link Series.yAxisId}. When set, each axis formats ticks, labels, and scales independently;
diff --git a/packages/quill/packages/charts/src/docs/axes.md b/packages/quill/packages/charts/src/docs/axes.md
index 329a6151e31c..a972eb624756 100644
--- a/packages/quill/packages/charts/src/docs/axes.md
+++ b/packages/quill/packages/charts/src/docs/axes.md
@@ -27,7 +27,7 @@ Theme-side restyle knobs, all optional: `theme.axisLineColor` strokes the baseli
- Labels must be unique. The x-scale keys positions off the strings, so a duplicate collapses onto the first occurrence and draws the series backwards. On time-series charts pass ISO date strings, never formatted display labels that repeat across years. Set `xAxis.timezone` to format ticks and tooltip headers. The library infers the interval from `allDays` or chart labels; set `interval` only to override it. `tickFormatter` overrides automatic formatting.
- `xAxis.tickLabelRotation` (`-45` tilts left) fixes the rotation, clamped to `-90..90`. Omit it to keep horizontal labels and the collision behavior.
-- `maxCategoryLabelWidth` truncates category labels with an ellipsis and reveals the full value on hover. It also clamps the axis margin, so a long label cannot push the plot off screen. `MAX_CATEGORY_LABEL_WIDTH` is the exported default.
+- `maxCategoryLabelWidth` truncates category labels with an ellipsis and reveals the full value on hover. It also clamps the axis margin, so a long label cannot push the plot off screen. On horizontal charts the cap grows to 30% of the chart width when that is wider, so a wider chart shows more of each label. `MAX_CATEGORY_LABEL_WIDTH` is the exported default.
## Y-axis format