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.

2.8 KB
Raw Blame
import type { ReactNode } from 'react'

import type { XAxisConfig, ChartMargins, ValueDomain, RadialSlicePayload } from '@posthog/quill-charts'

import { ChartSettings, GoalLine } from '~/queries/schema/schema-general'
import { ChartDisplayType } from '~/types'

import { AxisSeries } from '../../dataVisualizationLogic'
import { AxisBreakdownSeries } from '../seriesBreakdownLogic'
import { SqlBarGraph } from './SqlBarGraph'
import { SqlComboGraph } from './SqlComboGraph'
import { SqlLineGraph } from './SqlLineGraph'
import { sqlChartKind } from './sqlLineGraphAdapter'

const SQL_CHART_VISUALIZATION_TYPES: ChartDisplayType[] = [
    ChartDisplayType.ActionsLineGraph,
    ChartDisplayType.ActionsBar,
    ChartDisplayType.ActionsBarValue,
    ChartDisplayType.ActionsAreaGraph,
    ChartDisplayType.ActionsStackedBar,
]

export const isSqlChartVisualizationType = (visualizationType: ChartDisplayType): boolean =>
    SQL_CHART_VISUALIZATION_TYPES.includes(visualizationType)

export type SqlChartProps = {
    xData: AxisSeries<string> | null
    yData: AxisSeries<number | null>[] | AxisBreakdownSeries<number | null>[]
    visualizationType: ChartDisplayType
    chartSettings: ChartSettings
    presetChartHeight?: boolean
    dashboardId?: string
    goalLines?: GoalLine[]
    insightNumericId?: number | 'new'
    showAnnotations?: boolean
    embedded?: boolean
    className?: string
    /** Called when the user clicks a data point. Receives the series key, x-axis index, and label.
     *  When provided, the SQL chart shows a "click to inspect" hint in the tooltip. */
    onPointClick?: (seriesKey: string, dataIndex: number, label: string) => void
    onSliceClick?: (payload: RadialSlicePayload) => void
    pointClickHint?: string
    directPointClick?: boolean
    labelFormatter?: (label: string) => string
    xAxis?: XAxisConfig
    margins?: Partial<ChartMargins>
    /** Shared bounds for comparable charts rendered from the same worksheet result. */
    valueDomain?: ValueDomain
    children?: ReactNode
}

/**
 * Picks the @posthog/quill-charts renderer for a SQL insight: combo for mixed bar + line/area
 * series, bar for bar-only, line/area otherwise. (Pie, donut and proportion bar have their own wrapper — see PartOfWholeChart.)
 */
export function sqlChartComponentFor(props: SqlChartProps): (props: SqlChartProps) => JSX.Element {
    switch (sqlChartKind(props)) {
        case 'combo':
            return SqlComboGraph
        case 'bar':
            return SqlBarGraph
        case 'line':
            return SqlLineGraph
    }
}

/** Entry point for rendering a non-pie SQL (DataVisualization) chart: dispatches line, area, bar,
 *  stacked-bar, and combo series to the matching @posthog/quill-charts renderer. */
export const SqlChart = (props: SqlChartProps): JSX.Element => {
    const Component = sqlChartComponentFor(props)
    return <Component {...props} />
}