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.

3.4 KB
Raw Blame
import { useValues } from 'kea'
import { useMemo } from 'react'

import {
    type Series,
    TimeSeriesBarChart,
    type TimeSeriesBarChartConfig,
    TimeSeriesLineChart,
    type TimeSeriesLineChartConfig,
} from '@posthog/quill-charts'

import { useChartConfig, useChartTheme } from 'lib/charts/hooks'
import { getColorVar } from 'lib/colors'
import { dayjs } from 'lib/dayjs'
import { teamLogic } from 'scenes/teamLogic'

import type { MetricsDisplaySettings } from '~/queries/schema/schema-general'

import { buildMetricsChartConfig } from './metricsChartConfig'
import { MetricsExemplarMarkers, type MetricsExemplar } from './MetricsExemplarMarkers'
import { formatSeriesNames, type MetricsChartSeries, seriesColor } from './metricsSeries'

const AREA_FILL_OPACITY = 0.2

/** Multi-series metric time-series chart, shared by the Viewer and the dashboard/insight tile. Every
 * series shares one time grid (the backend zero-fills), so the x-axis comes from the first series.
 *
 * `display.type: 'stat'` is in the schema but has no renderer yet, so it falls through to a line
 * chart rather than blanking the tile. */
export function MetricsSeriesChart({
    series,
    fallbackName,
    display,
    exemplars,
}: {
    series: MetricsChartSeries[]
    fallbackName: string
    display?: MetricsDisplaySettings
    exemplars?: MetricsExemplar[]
}): JSX.Element {
    const { timezone } = useValues(teamLogic)
    const theme = useChartTheme()
    const isBar = display?.type === 'bar'
    const isArea = display?.type === 'area'

    const chartSeries = useMemo<Series[]>(() => {
        const names = formatSeriesNames(
            series.map((s) => ({ labels: s.labels, metric_name: s.metricName ?? undefined, clause: s.clause })),
            fallbackName
        )
        return series.map((s, index) => ({
            key: `${index}`,
            label: names[index],
            // A null value is a gap (non-representable aggregate); charted as 0 for now.
            data: s.points.map((p) => p.value ?? 0),
            color: getColorVar(seriesColor(index)),
            ...(isArea ? { fill: { opacity: AREA_FILL_OPACITY } } : {}),
        }))
    }, [series, fallbackName, isArea])
    const labels = useMemo(() => (series[0]?.points ?? []).map((p) => p.time), [series])

    const sharedConfig = useChartConfig<TimeSeriesLineChartConfig>(
        () =>
            buildMetricsChartConfig({
                display,
                xAxis: { timezone },
                seriesCount: chartSeries.length,
                labelFormatter: (label: string) => dayjs(label).tz(timezone).format('D MMM YYYY HH:mm:ss'),
            }),
        [timezone, chartSeries.length, display]
    )

    const markers = exemplars?.length ? <MetricsExemplarMarkers exemplars={exemplars} /> : null

    return (
        <div className="relative flex h-full w-full min-h-0 flex-col">
            {isBar ? (
                <TimeSeriesBarChart
                    series={chartSeries}
                    labels={labels}
                    theme={theme}
                    config={sharedConfig as TimeSeriesBarChartConfig}
                >
                    {markers}
                </TimeSeriesBarChart>
            ) : (
                <TimeSeriesLineChart series={chartSeries} labels={labels} theme={theme} config={sharedConfig}>
                    {markers}
                </TimeSeriesLineChart>
            )}
        </div>
    )
}