: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import clsx from 'clsx'
import { useCallback } from 'react'
import {
BarChart,
ReferenceLines,
TimeSeriesBarChart,
ValueLabels,
type PointClickData,
type ValueLabelContext,
} from '@posthog/quill-charts'
import { makeChartErrorHandler } from 'lib/charts/chartErrorHandler'
import { AnnotationsLayer } from 'lib/components/AnnotationsOverlay/AnnotationsLayer'
import { ChartDisplayType } from '~/types'
import { type SqlChartProps } from './SqlChart'
import { SqlChartTooltip } from './SqlChartTooltip'
import {
type SqlBarGraphConfig,
type SqlLineSeriesMeta,
buildBarChartConfig,
buildBarValueChartConfig,
formatSqlSeriesValue,
hasValueLabels,
} from './sqlLineGraphAdapter'
import { useSqlChartModel } from './useSqlChartModel'
const handleChartError = makeChartErrorHandler('sql-bar-chart')
export const SqlBarGraph = (props: SqlChartProps): JSX.Element => {
const { onPointClick: onPointClickProp } = props
const isHorizontal = props.visualizationType === ChartDisplayType.ActionsBarValue
const model = useSqlChartModel<SqlBarGraphConfig>(
props,
isHorizontal ? buildBarValueChartConfig : buildBarChartConfig
)
const onPointClick = useCallback(
(data: PointClickData<SqlLineSeriesMeta>) => {
onPointClickProp?.(data.series.key, data.dataIndex, String(props.xData?.data[data.dataIndex] ?? data.label))
},
[onPointClickProp, props.xData]
)
const series = model?.series
const valueLabelFormatter = useCallback(
(_value: number, seriesIndex: number, _dataIndex: number, context: ValueLabelContext): string =>
formatSqlSeriesValue(context.rawValue, series?.[seriesIndex]?.meta?.settings),
[series]
)
return (
<div
className={clsx(props.className, 'rounded bg-surface-primary w-full grow relative flex flex-col', {
'h-[60vh]': props.presetChartHeight,
'h-full': !props.presetChartHeight,
'overflow-y-auto': isHorizontal && !props.embedded,
'overflow-hidden': !isHorizontal || props.embedded,
})}
>
{model &&
(isHorizontal ? (
<BarChart
series={model.series}
labels={model.labels}
theme={model.theme}
config={model.config}
onPointClick={onPointClickProp ? onPointClick : undefined}
tooltip={
onPointClickProp
? (context) => (
<SqlChartTooltip
context={context}
config={model.config.tooltip}
onPointClick={onPointClickProp}
hint={props.pointClickHint}
/>
)
: undefined
}
onError={handleChartError}
>
<ReferenceLines lines={model.config.referenceLines ?? []} />
{props.children}
{hasValueLabels(props.chartSettings, props.yData) && (
<ValueLabels
valueFormatter={valueLabelFormatter}
position={props.chartSettings.valueLabelPosition}
background={props.chartSettings.valueLabelBackground}
fontSize={props.chartSettings.valueLabelFontSize}
rotation={props.chartSettings.valueLabelRotation}
/>
)}
</BarChart>
) : (
<TimeSeriesBarChart
series={model.series}
labels={model.labels}
theme={model.theme}
config={model.config}
onPointClick={onPointClickProp ? onPointClick : undefined}
tooltip={
onPointClickProp
? (context) => (
<SqlChartTooltip
context={context}
config={model.config.tooltip}
onPointClick={onPointClickProp}
hint={props.pointClickHint}
/>
)
: undefined
}
onError={handleChartError}
>
{props.children}
{props.showAnnotations && props.insightNumericId && (
<AnnotationsLayer
insightNumericId={props.insightNumericId}
dates={model.labels}
annotationsScope={props.chartSettings.annotationsScope}
/>
)}
</TimeSeriesBarChart>
))}
</div>
)
}