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): keep previous-period series glyph opaque in tooltip (#65470)

Thomas Obermülleredff951c09ce
diff --git a/frontend/src/lib/utils/colors.test.ts b/frontend/src/lib/utils/colors.test.ts
index 2a997353b4b8..0ed7f282c0da 100644
--- a/frontend/src/lib/utils/colors.test.ts
+++ b/frontend/src/lib/utils/colors.test.ts
@@ -1,4 +1,4 @@
-import { hexToRGBA } from 'lib/utils/colors'
+import { hexToRGBA, toOpaqueHex } from 'lib/utils/colors'
describe('colors utils', () => {
describe('hexToRGBA()', () => {
@@ -8,4 +8,18 @@ describe('colors utils', () => {
expect(hexToRGBA('#5375ff', 1)).toEqual('rgba(83,117,255,1)')
})
})
+
+ describe('toOpaqueHex()', () => {
+ it.each([
+ ['rgba(29,74,255,0.5)', '#1d4aff'],
+ ['rgba(29, 74, 255, 0.5)', '#1d4aff'],
+ ['rgb(29,74,255)', '#1d4aff'],
+ ['rgba(0,0,0,0.5)', '#000000'],
+ ['#1d4aff80', '#1d4aff'],
+ ['#1d4aff', '#1d4aff'],
+ ['var(--data-color-1)', 'var(--data-color-1)'],
+ ])('strips alpha from %s -> %s', (input, expected) => {
+ expect(toOpaqueHex(input)).toEqual(expected)
+ })
+ })
})
diff --git a/frontend/src/lib/utils/colors.ts b/frontend/src/lib/utils/colors.ts
index 88742f7752e1..b00e56a99122 100644
--- a/frontend/src/lib/utils/colors.ts
+++ b/frontend/src/lib/utils/colors.ts
@@ -58,6 +58,39 @@ export function RGBToRGBA(rgb: string, a: number): string {
return `rgba(${[r, g, b, a].join(',')})`
}
+/**
+ * Strip any alpha channel and return an opaque `#rrggbb` hex string.
+ * Accepts `rgb(...)`, `rgba(...)`, and 8-digit `#rrggbbaa` hex; opaque hex,
+ * `var(--…)`, or anything unparseable is returned unchanged. Dimming a series
+ * color only changes its alpha, so this losslessly recovers the opaque color.
+ */
+export function toOpaqueHex(color: string): string {
+ const toHex = (r: number, g: number, b: number): string =>
+ `#${[r, g, b]
+ .map((c) =>
+ Math.max(0, Math.min(255, Math.round(c)))
+ .toString(16)
+ .padStart(2, '0')
+ )
+ .join('')}`
+
+ const rgbMatch = color.match(/^rgba?\(([^)]+)\)$/i)
+ if (rgbMatch) {
+ const channels = rgbMatch[1]
+ .split(',')
+ .slice(0, 3)
+ .map((part) => parseFloat(part.trim()))
+ if (channels.length < 3 || channels.some((c) => !Number.isFinite(c))) {
+ return color
+ }
+ return toHex(channels[0], channels[1], channels[2])
+ }
+ if (/^#[0-9a-f]{8}$/i.test(color)) {
+ return color.slice(0, 7)
+ }
+ return color
+}
+
export function RGBToHSL(r: number, g: number, b: number): { h: number; s: number; l: number } {
// Convert RGB values to the range 0-1
r /= 255
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsx b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsx
index 1374a556dada..78f36d58aa2a 100644
--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsx
+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.test.tsx
@@ -493,6 +493,67 @@ describe('TrendsBarChart (ActionsBarValue)', () => {
{ timeout: 5000 }
)
})
+
+ it('keeps the previous-period identifier glyph opaque while dimming only the row ribbon', async () => {
+ // Give the previous period the larger aggregated_value so it sorts topmost (DESC) and is the
+ // bar hovered at plotTop. Its series color arrives pre-dimmed (rgba .5); the ribbon should keep
+ // that dim to mark the period, but the SeriesLetter glyph must render at full opacity.
+ const action = { id: '$pageview', type: 'events', name: '$pageview', order: 0 }
+ const compareResults = {
+ results: [
+ {
+ action,
+ label: '$pageview',
+ count: 50,
+ aggregated_value: 50,
+ data: [50],
+ labels: ['Day 1'],
+ days: ['2024-06-10'],
+ compare: true,
+ compare_label: 'current',
+ },
+ {
+ action,
+ label: '$pageview',
+ count: 500,
+ aggregated_value: 500,
+ data: [500],
+ labels: ['Day 1'],
+ days: ['2024-06-03'],
+ compare: true,
+ compare_label: 'previous',
+ },
+ ],
+ }
+ renderInsight({
+ query: aggregatedBar({ compareFilter: { compare: true } }),
+ mocks: {
+ additionalMockResponses: [{ match: (q) => q.kind === NodeKind.TrendsQuery, response: compareResults }],
+ },
+ })
+ const canvas = await screen.findByRole('img', { name: /chart with/i }, { timeout: 5000 })
+ const wrapper = canvas.parentElement!
+ // Topmost band is the previous period (largest aggregated_value, sorted DESC). Aim at the
+ // band centre — the bar's hittable region sits inside the band's padding.
+ const previousBandY = dimensions.plotTop + dimensions.plotHeight / 4
+
+ // Re-fire the hover each tick until the chart commits its scales and the tooltip populates —
+ // a single mouseMove before the chart settles is silently dropped.
+ await waitFor(
+ () => {
+ fireEvent.mouseMove(wrapper, { clientX: dimensions.plotLeft + 30, clientY: previousBandY })
+ const glyph = chart.getTooltip()?.querySelector<HTMLElement>('.graph-series-glyph')
+ expect(glyph).toBeTruthy()
+ // The identifier glyph stays opaque — no alpha channel bled in from the dimmed color.
+ expect(glyph!.style.color).not.toMatch(/rgba/)
+ expect(glyph!.style.borderColor).not.toMatch(/rgba/)
+ // The left ribbon still carries the half-opacity previous-period color.
+ const ribbon = glyph!.closest('tr')!.style.getPropertyValue('--row-ribbon-color')
+ expect(ribbon).toMatch(/rgba\([^)]*0?\.5\)/)
+ },
+ { timeout: 8000 }
+ )
+ }, 12000)
})
describe('TrendsBarChart (ActionsUnstackedBar)', () => {
diff --git a/products/product_analytics/frontend/insights/trends/shared/TrendsTooltip.tsx b/products/product_analytics/frontend/insights/trends/shared/TrendsTooltip.tsx
index d6d308c64b6b..1b2681f74c4d 100644
--- a/products/product_analytics/frontend/insights/trends/shared/TrendsTooltip.tsx
+++ b/products/product_analytics/frontend/insights/trends/shared/TrendsTooltip.tsx
@@ -3,6 +3,7 @@ import { useCallback, useMemo } from 'react'
import type { TooltipContext } from '@posthog/quill-charts'
import { SeriesLetter } from 'lib/components/SeriesGlyph'
+import { toOpaqueHex } from 'lib/utils/colors'
import { percentage } from 'lib/utils/numbers'
import { formatAggregationAxisValue } from 'scenes/insights/aggregationAxisFormat'
import { InsightTooltip } from 'scenes/insights/InsightTooltip/InsightTooltip'
@@ -134,7 +135,10 @@ export function TrendsTooltip({
className="mr-2"
hasBreakdown={hasBreakdown}
seriesIndex={datum.action?.order ?? datum.id}
- seriesColor={datum.color}
+ // Previous-period series arrive pre-dimmed (rgba); the ribbon keeps the dim to
+ // mark the period, but the identifier glyph must stay legible — give it the
+ // opaque color.
+ seriesColor={datum.color ? toOpaqueHex(datum.color) : undefined}
/>
)}
{value}