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.0 KB
Raw Blame
import clsx from 'clsx'

import { ProportionBar } from '@posthog/quill-charts'
import type { ProportionBarConfig } from '@posthog/quill-charts'

import { makeChartErrorHandler } from 'lib/charts/chartErrorHandler'
import { useChartConfig } from 'lib/charts/hooks'

import { SqlChartProps } from './SqlChart'
import { useSqlPartOfWholeChart } from './useSqlPartOfWholeChart'

const handleChartError = makeChartErrorHandler('sql-proportion-bar')

export const SqlProportionBar = ({
    xData,
    yData,
    chartSettings,
    presetChartHeight,
    className,
    labelFormatter,
    onSliceClick,
}: SqlChartProps): JSX.Element => {
    const { theme, series, legendConfig, total, showTotal, valueFormatter } = useSqlPartOfWholeChart(
        { xData, yData, chartSettings, labelFormatter },
        true
    )
    const config: ProportionBarConfig = useChartConfig(() => ({ legend: legendConfig }), [legendConfig])

    if (!series.length) {
        return (
            <div className={clsx(className, 'rounded bg-surface-primary flex flex-1 items-center justify-center p-6')}>
                <span className="text-secondary text-sm">Proportion bars require at least one positive value.</span>
            </div>
        )
    }

    return (
        <div
            className={clsx(className, 'rounded bg-surface-primary flex flex-col flex-1 min-h-0 p-4 justify-center', {
                'h-[60vh]': presetChartHeight,
                'h-full': !presetChartHeight,
            })}
        >
            <ProportionBar
                series={series}
                theme={theme}
                config={config}
                valueFormatter={valueFormatter}
                dataAttr="sql-proportion-bar"
                onError={handleChartError}
                onSliceClick={onSliceClick}
            />
            {showTotal && (
                <div className="pt-4 text-center shrink-0">
                    <div className="text-5xl font-bold">{valueFormatter(total)}</div>
                </div>
            )}
        </div>
    )
}