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.

chore(hog-charts): move generic utils + AnomalyPointsLayer to package level (#57766)

Sam Penningtond484c3d9b094
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsx b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsx
index 6b2ee07f5145..93c7164f46a2 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsx
+++ b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.test.tsx
@@ -1,8 +1,8 @@
import { cleanup } from '@testing-library/react'
import type { ChartTheme, Series } from '../../core/types'
+import type { AnomalyMarker } from '../../overlays/AnomalyPointsLayer'
import { renderHogChart, setupJsdom, setupSyncRaf } from '../../testing'
-import type { AnomalyMarker } from './overlays/AnomalyPointsLayer'
import { TimeSeriesLineChart } from './TimeSeriesLineChart'
const THEME: ChartTheme = { colors: ['#111', '#222', '#333'], backgroundColor: '#ffffff' }
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx
index 41ee3fb75698..5f23377615e3 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx
+++ b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx
@@ -8,13 +8,18 @@ import type {
TooltipConfig,
TooltipContext,
} from '../../core/types'
+import { AnomalyPointsLayer, type AnomalyMarker } from '../../overlays/AnomalyPointsLayer'
import { ReferenceLines } from '../../overlays/ReferenceLine'
import { ValueLabels } from '../../overlays/ValueLabels'
+import { buildGoalLineReferenceLines, type GoalLineConfig } from '../../utils/goal-lines'
+import {
+ useXTickFormatter,
+ useYTickFormatter,
+ type XAxisConfig,
+ type YAxisConfig,
+} from '../../utils/use-axis-formatters'
import { LineChart } from '../LineChart'
-import { AnomalyPointsLayer, type AnomalyMarker } from './overlays/AnomalyPointsLayer'
-import { buildGoalLineReferenceLines, type GoalLineConfig } from './utils/goal-lines'
import { applyInProgressToSeries, type InProgressConfig } from './utils/in-progress'
-import { useXTickFormatter, useYTickFormatter, type XAxisConfig, type YAxisConfig } from './utils/use-axis-formatters'
import {
useDerivedSeries,
type ConfidenceIntervalConfig,
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.test.ts b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.test.ts
index 8bed88c55ee4..1d21c9fb8702 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.test.ts
+++ b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.test.ts
@@ -1,10 +1,5 @@
import type { Series } from '../../../core/types'
-import {
- applyComparisonDimming,
- buildConfidenceIntervalSeries,
- buildMovingAverageSeries,
- buildTrendLineSeries,
-} from './derived-series'
+import { buildConfidenceIntervalSeries, buildMovingAverageSeries, buildTrendLineSeries } from './derived-series'
const SOURCE: Series = {
key: 'visits',
@@ -93,30 +88,3 @@ describe('buildTrendLineSeries', () => {
expect(tl.data[data.length - 1]).toBeCloseTo(expectedLast, 2)
})
})
-
-describe('applyComparisonDimming', () => {
- const A: Series = { key: 'a', label: 'A', data: [1, 2], color: '#112233' }
- const A_PREV: Series = { key: 'a-prev', label: 'A (prev)', data: [1, 2], color: '#112233' }
- const B: Series = { key: 'b', label: 'B', data: [3, 4], color: '#445566' }
-
- it.each([
- ['undefined', undefined],
- ['empty', {}],
- ] as const)('returns the same reference when comparisonOf is %s', (_label, comparisonOf) => {
- const series = [A, B]
- expect(applyComparisonDimming(series, comparisonOf)).toBe(series)
- })
-
- it('rewrites comparison series to a dimmed rgba colour, leaves primaries alone', () => {
- const result = applyComparisonDimming([A, A_PREV, B], { 'a-prev': 'a' })
- expect(result[0]).toBe(A)
- expect(result[2]).toBe(B)
- expect(result[1].color).toMatch(/^rgba\([^)]*,\s*0\.5\)$/)
- })
-
- it('leaves non-hex colours untouched (no double-wrapping)', () => {
- const rgbaSource: Series = { key: 'a-prev', label: '', data: [], color: 'rgba(0,0,0,1)' }
- const result = applyComparisonDimming([rgbaSource], { 'a-prev': 'a' })
- expect(result[0].color).toBe('rgba(0,0,0,1)')
- })
-})
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.ts b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.ts
index 08922a9c6bcf..bb133c4a6317 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.ts
+++ b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/derived-series.ts
@@ -1,9 +1,8 @@
import { linearRegression, movingAverage, trendLine } from 'lib/statistics'
-import { hexToRGBA } from 'lib/utils'
import type { Series } from '../../../core/types'
+import { dimHex } from '../../../utils/comparison-dimming'
-const COMPARISON_DIM_OPACITY = 0.5
const TREND_LINE_DIM_OPACITY = 0.5
const CI_FILL_OPACITY = 0.2
const MA_DASH_PATTERN = [10, 3]
@@ -111,31 +110,3 @@ function exponentialTrend(values: number[], fitUpTo?: number): number[] {
const { m, b } = linearRegression(coords)
return values.map((_, x) => Math.exp(m * x + b))
}
-
-/** Re-render comparison series at reduced opacity so they read as subordinate to their
- * primary. Series whose colour is missing or already an `rgba(...)` string are left as-is
- * — `hexToRGBA` only handles hex inputs. */
-export function applyComparisonDimming<Meta = unknown>(
- series: Series<Meta>[],
- comparisonOf: Record<string, string> | undefined
-): Series<Meta>[] {
- if (!comparisonOf || Object.keys(comparisonOf).length === 0) {
- return series
- }
- return series.map((s) => {
- if (!(s.key in comparisonOf)) {
- return s
- }
- const dimmed = dimHex(s.color, COMPARISON_DIM_OPACITY)
- return dimmed === s.color ? s : { ...s, color: dimmed }
- })
-}
-
-/** Apply alpha dimming to a hex colour. Returns the input unchanged for non-hex inputs
- * (CSS variables, `rgba(...)`, undefined) since `hexToRGBA` only handles hex. */
-function dimHex(color: string | undefined, alpha: number): string | undefined {
- if (!color || !color.startsWith('#')) {
- return color
- }
- return hexToRGBA(color, alpha)
-}
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/use-derived-series.ts b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/use-derived-series.ts
index 914489c74ba4..8a8995e7e27d 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/use-derived-series.ts
+++ b/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/use-derived-series.ts
@@ -1,12 +1,8 @@
import { useMemo } from 'react'
import type { Series } from '../../../core/types'
-import {
- applyComparisonDimming,
- buildConfidenceIntervalSeries,
- buildMovingAverageSeries,
- buildTrendLineSeries,
-} from './derived-series'
+import { applyComparisonDimming } from '../../../utils/comparison-dimming'
+import { buildConfidenceIntervalSeries, buildMovingAverageSeries, buildTrendLineSeries } from './derived-series'
export interface ConfidenceIntervalConfig {
seriesKey: string
diff --git a/frontend/src/lib/hog-charts/index.ts b/frontend/src/lib/hog-charts/index.ts
index e32271302774..150ab3ca2a59 100644
--- a/frontend/src/lib/hog-charts/index.ts
+++ b/frontend/src/lib/hog-charts/index.ts
@@ -64,16 +64,15 @@ export type { ValueLabelsProps } from './overlays/ValueLabels'
// Helper for adapters that need to align with the same x-axis tick selection the chart draws.
export { computeVisibleXLabels } from './overlays/AxisLabels'
-// Timeseries overlays
-export { AnomalyPointsLayer } from './charts/TimeSeriesLineChart/overlays/AnomalyPointsLayer'
-export type { AnomalyMarker } from './charts/TimeSeriesLineChart/overlays/AnomalyPointsLayer'
+export { AnomalyPointsLayer } from './overlays/AnomalyPointsLayer'
+export type { AnomalyMarker } from './overlays/AnomalyPointsLayer'
export { movingAverageKey } from './charts/TimeSeriesLineChart/utils/derived-series'
// Timeseries utils
-export { createXAxisTickCallback, parseDateForAxis } from './charts/TimeSeriesLineChart/utils/dates'
-export type { TimeInterval } from './charts/TimeSeriesLineChart/utils/dates'
-export { buildYTickFormatter } from './charts/TimeSeriesLineChart/utils/y-formatters'
-export type { YAxisFormat, YFormatterConfig } from './charts/TimeSeriesLineChart/utils/y-formatters'
-export type { XAxisConfig, YAxisConfig } from './charts/TimeSeriesLineChart/utils/use-axis-formatters'
-export { buildGoalLineReferenceLines, computeSeriesNonZeroMax } from './charts/TimeSeriesLineChart/utils/goal-lines'
-export type { GoalLineConfig } from './charts/TimeSeriesLineChart/utils/goal-lines'
+export { createXAxisTickCallback, parseDateForAxis } from './utils/dates'
+export type { TimeInterval } from './utils/dates'
+export { buildYTickFormatter } from './utils/y-formatters'
+export type { YAxisFormat, YFormatterConfig } from './utils/y-formatters'
+export type { XAxisConfig, YAxisConfig } from './utils/use-axis-formatters'
+export { buildGoalLineReferenceLines, computeSeriesNonZeroMax } from './utils/goal-lines'
+export type { GoalLineConfig } from './utils/goal-lines'
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/overlays/AnomalyPointsLayer.tsx b/frontend/src/lib/hog-charts/overlays/AnomalyPointsLayer.tsx
similarity index 97%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/overlays/AnomalyPointsLayer.tsx
rename to frontend/src/lib/hog-charts/overlays/AnomalyPointsLayer.tsx
index 7601656dfa9c..a2fda782667a 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/overlays/AnomalyPointsLayer.tsx
+++ b/frontend/src/lib/hog-charts/overlays/AnomalyPointsLayer.tsx
@@ -1,7 +1,7 @@
/* eslint-disable react/forbid-dom-props -- dynamic pixel positions from d3 scales */
import React from 'react'
-import { useChartLayout } from '../../../core/chart-context'
+import { useChartLayout } from '../core/chart-context'
export interface AnomalyMarker {
/** Index along the x-axis (into the chart's `labels` array). */
diff --git a/frontend/src/lib/hog-charts/utils/comparison-dimming.test.ts b/frontend/src/lib/hog-charts/utils/comparison-dimming.test.ts
new file mode 100644
index 000000000000..67419b050bfb
--- /dev/null
+++ b/frontend/src/lib/hog-charts/utils/comparison-dimming.test.ts
@@ -0,0 +1,29 @@
+import type { Series } from '../core/types'
+import { applyComparisonDimming } from './comparison-dimming'
+
+describe('applyComparisonDimming', () => {
+ const A: Series = { key: 'a', label: 'A', data: [1, 2], color: '#112233' }
+ const A_PREV: Series = { key: 'a-prev', label: 'A (prev)', data: [1, 2], color: '#112233' }
+ const B: Series = { key: 'b', label: 'B', data: [3, 4], color: '#445566' }
+
+ it.each([
+ ['undefined', undefined],
+ ['empty', {}],
+ ] as const)('returns the same reference when comparisonOf is %s', (_label, comparisonOf) => {
+ const series = [A, B]
+ expect(applyComparisonDimming(series, comparisonOf)).toBe(series)
+ })
+
+ it('rewrites comparison series to a dimmed rgba colour, leaves primaries alone', () => {
+ const result = applyComparisonDimming([A, A_PREV, B], { 'a-prev': 'a' })
+ expect(result[0]).toBe(A)
+ expect(result[2]).toBe(B)
+ expect(result[1].color).toMatch(/^rgba\([^)]*,\s*0\.5\)$/)
+ })
+
+ it('leaves non-hex colours untouched (no double-wrapping)', () => {
+ const rgbaSource: Series = { key: 'a-prev', label: '', data: [], color: 'rgba(0,0,0,1)' }
+ const result = applyComparisonDimming([rgbaSource], { 'a-prev': 'a' })
+ expect(result[0].color).toBe('rgba(0,0,0,1)')
+ })
+})
diff --git a/frontend/src/lib/hog-charts/utils/comparison-dimming.ts b/frontend/src/lib/hog-charts/utils/comparison-dimming.ts
new file mode 100644
index 000000000000..2d0742b100c8
--- /dev/null
+++ b/frontend/src/lib/hog-charts/utils/comparison-dimming.ts
@@ -0,0 +1,33 @@
+import { hexToRGBA } from 'lib/utils'
+
+import type { Series } from '../core/types'
+
+const COMPARISON_DIM_OPACITY = 0.5
+
+/** Re-render comparison series at reduced opacity so they read as subordinate to their
+ * primary. Series whose colour is missing or already an `rgba(...)` string are left as-is
+ * — `hexToRGBA` only handles hex inputs. */
+export function applyComparisonDimming<Meta = unknown>(
+ series: Series<Meta>[],
+ comparisonOf: Record<string, string> | undefined
+): Series<Meta>[] {
+ if (!comparisonOf || Object.keys(comparisonOf).length === 0) {
+ return series
+ }
+ return series.map((s) => {
+ if (!(s.key in comparisonOf)) {
+ return s
+ }
+ const dimmed = dimHex(s.color, COMPARISON_DIM_OPACITY)
+ return dimmed === s.color ? s : { ...s, color: dimmed }
+ })
+}
+
+/** Apply alpha dimming to a hex colour. Returns the input unchanged for non-hex inputs
+ * (CSS variables, `rgba(...)`, undefined) since `hexToRGBA` only handles hex. */
+export function dimHex(color: string | undefined, alpha: number): string | undefined {
+ if (!color || !color.startsWith('#')) {
+ return color
+ }
+ return hexToRGBA(color, alpha)
+}
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/dates.test.ts b/frontend/src/lib/hog-charts/utils/dates.test.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/dates.test.ts
rename to frontend/src/lib/hog-charts/utils/dates.test.ts
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/dates.ts b/frontend/src/lib/hog-charts/utils/dates.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/dates.ts
rename to frontend/src/lib/hog-charts/utils/dates.ts
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/goal-lines.test.ts b/frontend/src/lib/hog-charts/utils/goal-lines.test.ts
similarity index 98%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/goal-lines.test.ts
rename to frontend/src/lib/hog-charts/utils/goal-lines.test.ts
index c2cc41e3fe35..f12754ce278f 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/goal-lines.test.ts
+++ b/frontend/src/lib/hog-charts/utils/goal-lines.test.ts
@@ -1,4 +1,4 @@
-import type { Series } from '../../../core/types'
+import type { Series } from '../core/types'
import { buildGoalLineReferenceLines, computeSeriesNonZeroMax, type GoalLineConfig } from './goal-lines'
const makeSeries = (data: number[], overrides: Partial<Series> = {}): Series => ({
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/goal-lines.ts b/frontend/src/lib/hog-charts/utils/goal-lines.ts
similarity index 91%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/goal-lines.ts
rename to frontend/src/lib/hog-charts/utils/goal-lines.ts
index 9a1c5f004902..1df8dfb0f117 100644
--- a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/goal-lines.ts
+++ b/frontend/src/lib/hog-charts/utils/goal-lines.ts
@@ -1,5 +1,5 @@
-import type { Series } from '../../../core/types'
-import type { ReferenceLineProps } from '../../../overlays/ReferenceLine'
+import type { Series } from '../core/types'
+import type { ReferenceLineProps } from '../overlays/ReferenceLine'
export interface GoalLineConfig {
value: number
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/use-axis-formatters.ts b/frontend/src/lib/hog-charts/utils/use-axis-formatters.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/use-axis-formatters.ts
rename to frontend/src/lib/hog-charts/utils/use-axis-formatters.ts
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/y-formatters.test.ts b/frontend/src/lib/hog-charts/utils/y-formatters.test.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/y-formatters.test.ts
rename to frontend/src/lib/hog-charts/utils/y-formatters.test.ts
diff --git a/frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/y-formatters.ts b/frontend/src/lib/hog-charts/utils/y-formatters.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/charts/TimeSeriesLineChart/utils/y-formatters.ts
rename to frontend/src/lib/hog-charts/utils/y-formatters.ts
diff --git a/frontend/src/scenes/trends/viz/trends-line-chart/trendsAxisFormat.ts b/frontend/src/scenes/trends/viz/trends-line-chart/trendsAxisFormat.ts
index d395e1feb8a4..52bd2738ee4b 100644
--- a/frontend/src/scenes/trends/viz/trends-line-chart/trendsAxisFormat.ts
+++ b/frontend/src/scenes/trends/viz/trends-line-chart/trendsAxisFormat.ts
@@ -1,5 +1,4 @@
-import type { YAxisConfig } from 'lib/hog-charts'
-import { YFormatterConfig } from 'lib/hog-charts/charts/TimeSeriesLineChart/utils/y-formatters'
+import type { YAxisConfig, YFormatterConfig } from 'lib/hog-charts'
import { CurrencyCode, TrendsFilter } from '~/queries/schema/schema-general'