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(hog-charts): polish bar chart overlays and gridlines (#57989)

Sam Penningtona375c2bfeff7
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 25a810d24c80..745f7c24ee0e 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -440,6 +440,10 @@ snapshots:
hash: v1.k794b7964.2e57b5eb67fe61cb0d615db8e602b5867b9965271f930576572486f5c3a381cf.rQJgmmPBwH0Jl_NALrmNhCrkHPrw3zfvQwW7NaUfCSU
components-hogcharts-barchart--horizontal--light:
hash: v1.k794b7964.c2d37efbb5f1a595f2ad8004b5b532ed557f5ef0cf8d66c6889c9ba60120d197.68oC5-4T8-zOosLcakG-rWYkDFsG_-GG6G60H69pI4g
+ components-hogcharts-barchart--horizontal-aggregated-single--dark:
+ hash: v1.k794b7964.8608593ebfba3ffae5144e5be62baeed502949152b7ae382ec2529c870205aa7.fmgEKz_TwFIitbSD0dhjQKNyOgxmLegrv-xPzbZ22x0
+ components-hogcharts-barchart--horizontal-aggregated-single--light:
+ hash: v1.k794b7964.e3aeccafc937e33a26348c2760b8c27c4bcdfb93d1b52638bd3514b482d18ba6.AwaxzDrNr0PXQe5iu8yrhQmTf1ib0qe1FUsYp5M1I8s
components-hogcharts-barchart--horizontal-grouped--dark:
hash: v1.k794b7964.f68e09df33fe1a42fa38a4c49b714f9f309995f5a9a76c45082cbfeefca19150.lfLphoQUP9BMClRf4sT9t3HD1DzyVZb99whBGWaVfqU
components-hogcharts-barchart--horizontal-grouped--light:
diff --git a/frontend/src/lib/hog-charts/charts/BarChart/BarChart.stories.tsx b/frontend/src/lib/hog-charts/charts/BarChart/BarChart.stories.tsx
index 0718c71810b5..c7119e297ed1 100644
--- a/frontend/src/lib/hog-charts/charts/BarChart/BarChart.stories.tsx
+++ b/frontend/src/lib/hog-charts/charts/BarChart/BarChart.stories.tsx
@@ -82,6 +82,19 @@ export const HorizontalGrouped: Story = {
},
}
+export const HorizontalAggregatedSingle: Story = {
+ render: () => {
+ const theme = useReactiveTheme()
+ const config: BarChartConfig = { barLayout: 'stacked', showGrid: true, axisOrientation: 'horizontal' }
+ const series: Series[] = [{ key: 'all', label: 'All events', color: '', data: [103000] }]
+ return (
+ <Stage height={320}>
+ <BarChart series={series} labels={['All events']} config={config} theme={theme} />
+ </Stage>
+ )
+ },
+}
+
export const SingleSeries: Story = {
render: () => {
const theme = useReactiveTheme()
diff --git a/frontend/src/lib/hog-charts/charts/BarChart/BarChart.test.tsx b/frontend/src/lib/hog-charts/charts/BarChart/BarChart.test.tsx
index d1a772012506..2382f3eb619b 100644
--- a/frontend/src/lib/hog-charts/charts/BarChart/BarChart.test.tsx
+++ b/frontend/src/lib/hog-charts/charts/BarChart/BarChart.test.tsx
@@ -40,6 +40,35 @@ describe('BarChart', () => {
})
})
+ it.each<[string, Series[], string[]]>([
+ ['single sparse bar', [{ key: 'all', label: 'All events', data: [103000] }], ['All events']],
+ [
+ 'single result with 90-day labels (mismatched)',
+ [{ key: 'all', label: 'All events', data: [103000] }],
+ Array.from({ length: 90 }, (_, i) => `d${i}`),
+ ],
+ [
+ 'two sparse-stacked results',
+ [
+ { key: 'a', label: 'A', data: [103000, 0] },
+ { key: 'b', label: 'B', data: [0, 50000] },
+ ],
+ ['A', 'B'],
+ ],
+ ])('horizontal: %s anchors value axis at 0', (_name, series, labels) => {
+ const { chart } = renderHogChart(
+ <BarChart
+ series={series}
+ labels={labels}
+ theme={THEME}
+ config={{ barLayout: 'stacked', axisOrientation: 'horizontal' }}
+ />
+ )
+ const ticks = chart.xTicks().map((t) => Number(t.replace(/,/g, '')))
+ // All cases must start at 0 — the value axis should be anchored.
+ expect(ticks[0]).toBe(0)
+ })
+
it('forwards `dataAttr` to the chart wrapper for product-test selection', () => {
const { chart } = renderHogChart(
<BarChart series={SERIES} labels={LABELS} theme={THEME} dataAttr="bar-chart-instance" />
diff --git a/frontend/src/lib/hog-charts/charts/BarChart/BarChart.tsx b/frontend/src/lib/hog-charts/charts/BarChart/BarChart.tsx
index 8344521c40da..885e1d7f6cc2 100644
--- a/frontend/src/lib/hog-charts/charts/BarChart/BarChart.tsx
+++ b/frontend/src/lib/hog-charts/charts/BarChart/BarChart.tsx
@@ -1,3 +1,4 @@
+import * as d3 from 'd3'
import React, { useCallback, useMemo } from 'react'
import { type BarChartPrivate, computeBarAtIndex, computeSeriesBars } from '../../core/bar-layout'
@@ -243,20 +244,11 @@ function BarChartInner<Meta = unknown>({
)
const drawHover = useCallback(
- ({
- ctx,
- scales,
- series: coloredSeries,
- labels: drawLabels,
- hoverIndex,
- hoverPosition,
- theme,
- }: ChartDrawArgs) => {
+ ({ ctx, scales, series: coloredSeries, labels: drawLabels, hoverIndex, hoverPosition }: ChartDrawArgs) => {
const d3Scales = (scales._private as BarChartPrivate | undefined)?.__barChart
if (!d3Scales || hoverIndex < 0) {
return
}
- const highlightColor = theme.crosshairColor ?? 'rgba(0, 0, 0, 0.2)'
const hoveredLabel = drawLabels[hoverIndex]
// For grouped, narrow to the bars under the cursor. If the cursor sits in a gap
// (no hits), fall back to highlighting all — matches the tooltip narrower's
@@ -299,6 +291,7 @@ function BarChartInner<Meta = unknown>({
isTopOfStack: isTop,
})
if (bar) {
+ const highlightColor = d3.color(s.color)?.darker(0.6).toString() ?? s.color
drawBarHighlight(ctx, bar, highlightColor, barCornerRadius)
}
}
diff --git a/frontend/src/lib/hog-charts/core/canvas-renderer.ts b/frontend/src/lib/hog-charts/core/canvas-renderer.ts
index 3b3204a2cadf..68fec0ecc8a5 100644
--- a/frontend/src/lib/hog-charts/core/canvas-renderer.ts
+++ b/frontend/src/lib/hog-charts/core/canvas-renderer.ts
@@ -319,6 +319,11 @@ export function drawGrid(drawCtx: DrawContext, options: DrawGridOptions = {}): v
ctx.lineWidth = 1
ctx.setLineDash([])
+ // Skip the first category tick when it falls right next to the axis baseline
+ // (left edge in vertical mode, top edge in horizontal) — otherwise it renders
+ // as a faint second line hugging the axis.
+ const AXIS_BASELINE_GAP = 4
+
if (orientation === 'horizontal') {
for (const tick of valueTicks) {
const x = Math.round(yScale(tick)) + 0.5
@@ -328,7 +333,7 @@ export function drawGrid(drawCtx: DrawContext, options: DrawGridOptions = {}): v
ctx.stroke()
}
for (const coord of categoryTicks) {
- if (!isFinite(coord)) {
+ if (!isFinite(coord) || coord - dimensions.plotTop < AXIS_BASELINE_GAP) {
continue
}
const y = Math.round(coord) + 0.5
@@ -354,7 +359,7 @@ export function drawGrid(drawCtx: DrawContext, options: DrawGridOptions = {}): v
}
for (const coord of categoryTicks) {
- if (!isFinite(coord)) {
+ if (!isFinite(coord) || coord - dimensions.plotLeft < AXIS_BASELINE_GAP) {
continue
}
const x = Math.round(coord) + 0.5
diff --git a/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx b/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx
index da152ec846c4..c38fd0d4d5b3 100644
--- a/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx
+++ b/frontend/src/lib/hog-charts/overlays/ValueLabels.tsx
@@ -16,6 +16,10 @@ export interface ValueLabelsProps {
const LABEL_FONT =
'600 12px -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", "Roboto", Helvetica, Arial, sans-serif'
const LABEL_HEIGHT = 22
+const LABEL_PADDING_X = 4
+const LABEL_PADDING_Y = 2
+const LABEL_BORDER = 2
+const LABEL_HORIZONTAL_CHROME = (LABEL_PADDING_X + LABEL_BORDER) * 2
const STACK_TOTAL_KEY = '__stack_total__'
let measureCtx: CanvasRenderingContext2D | null = null
@@ -69,7 +73,8 @@ function pushCandidate(
valueCoord: number,
above: boolean
): void {
- const width = ctx ? ctx.measureText(text).width : text.length * 6
+ const textWidth = ctx ? ctx.measureText(text).width : text.length * 6
+ const width = textWidth + LABEL_HORIZONTAL_CHROME
out.push({
key,
seriesIndex,
@@ -273,9 +278,9 @@ const LABEL_STYLE_BASE: React.CSSProperties = {
fontSize: 12,
fontWeight: 600,
lineHeight: 1.2,
- padding: '2px 4px',
+ padding: `${LABEL_PADDING_Y}px ${LABEL_PADDING_X}px`,
borderRadius: 4,
- borderWidth: 2,
+ borderWidth: LABEL_BORDER,
borderStyle: 'solid',
pointerEvents: 'none',
whiteSpace: 'nowrap',