: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
feat(quill-charts): let a chart mark which points are clickable (#111237)
Sam Penningtonf21a8ccd340c
diff --git a/packages/quill/packages/charts/src/README.md b/packages/quill/packages/charts/src/README.mdindex 9b15654229e3..843415e38505 100644--- a/packages/quill/packages/charts/src/README.md+++ b/packages/quill/packages/charts/src/README.md@@ -93,8 +93,8 @@ what to do with the range (typically updating a date filter)./>```-The cursor switches to `crosshair` while enabled, except over an-actionable point (`onPointClick` is set) where it stays `pointer`. A+The cursor switches to `crosshair` while enabled, except over a+clickable point (`onPointClick` set, and `isPointClickable` passes) where it stays `pointer`. Aplain click without movement still pins the tooltip or fires `onPointClick`.## Custom overlaysdiff --git a/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsx b/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsxindex 6e2bad6dea6b..0265489aa916 100644--- a/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsx+++ b/packages/quill/packages/charts/src/charts/BarChart/BarChart.test.tsx@@ -348,6 +348,27 @@ describe('BarChart', () => {expect(tooltip.series.b.value).toBeCloseTo(15 / 35, 5)})+ it.each([+ { index: 0, calls: 0, cursor: 'cursor-crosshair' },+ { index: 1, calls: 1, cursor: 'cursor-pointer' },+ ])('isPointClickable gates click and pointer at index $index', async ({ index, calls, cursor }) => {+ const onPointClick = jest.fn()+ const { chart } = renderHogChart(+ <BarChart+ series={SERIES}+ labels={LABELS}+ theme={THEME}+ onPointClick={onPointClick}+ onDateRangeZoom={jest.fn()}+ isPointClickable={(i) => i === 1}+ />+ )+ chart.hoverAtIndex(index)+ expect(chart.element.className).toContain(cursor)+ await chart.clickAtIndex(index)+ expect(onPointClick).toHaveBeenCalledTimes(calls)+ })+it('stacked onPointClick reports each series own value, not the cumulative stack total', async () => {const onPointClick = jest.fn()const { chart } = renderHogChart(diff --git a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsxindex f1ca998c660c..80c0817eea06 100644--- a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx+++ b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx@@ -49,6 +49,8 @@ export interface BarChartProps<Meta = unknown> {theme: ChartThemetooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void+ /** See `ChartProps.isPointClickable`. */+ isPointClickable?: (dataIndex: number) => boolean/** Enables x-axis drag-to-zoom. Vertical bars only — horizontal bars interact along y,* where the gesture is disabled by the core. See `ChartProps.onDateRangeZoom`. */onDateRangeZoom?: (data: DateRangeZoomData) => void@@ -74,6 +76,7 @@ function BarChartInner<Meta = unknown>({theme,tooltip,onPointClick,+ isPointClickable,onDateRangeZoom,className,dataAttr,@@ -444,6 +447,7 @@ function BarChartInner<Meta = unknown>({/>)}onPointClick={onPointClick}+ isPointClickable={isPointClickable}onDateRangeZoom={onDateRangeZoom}wrapClickData={onPointClick ? wrapClickData : undefined}resolveHoverIndex={seriesHasTrackCeiling ? resolveHoverIndex : undefined}diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsxindex dc8afe6055b7..3ec4a104e6f1 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx@@ -81,6 +81,8 @@ export interface TimeSeriesBarChartProps<Meta = unknown> {config?: TimeSeriesBarChartConfigtooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void+ /** See `ChartProps.isPointClickable`. */+ isPointClickable?: (dataIndex: number) => boolean/** Enables x-axis drag-to-zoom. See `BarChartProps.onDateRangeZoom`. */onDateRangeZoom?: (data: DateRangeZoomData) => voiddataAttr?: string@@ -96,6 +98,7 @@ export function TimeSeriesBarChart<Meta = unknown>({config,tooltip,onPointClick,+ isPointClickable,onDateRangeZoom,dataAttr,className,@@ -188,6 +191,7 @@ export function TimeSeriesBarChart<Meta = unknown>({theme={theme}tooltip={tooltip}onPointClick={onPointClick}+ isPointClickable={isPointClickable}onDateRangeZoom={onDateRangeZoom}className={className}dataAttr={dataAttr}diff --git a/packages/quill/packages/charts/src/core/Chart.tsx b/packages/quill/packages/charts/src/core/Chart.tsxindex 9b48a486102a..4e31aa65d12b 100644--- a/packages/quill/packages/charts/src/core/Chart.tsx+++ b/packages/quill/packages/charts/src/core/Chart.tsx@@ -70,6 +70,8 @@ export interface ChartProps<Meta = unknown> {drawHover: (args: ChartDrawArgs) => DrawHoverResulttooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void+ /** Limits `onPointClick` and the pointer cursor to the points it returns true for. */+ isPointClickable?: (dataIndex: number) => boolean/** Enables x-axis drag-to-zoom. Fired with the label range the user dragged across.* x-axis only — has no effect on charts with a vertical (`interactionAxis: 'y'`) interaction. */onDateRangeZoom?: (data: DateRangeZoomData) => void@@ -126,6 +128,7 @@ export function Chart<Meta = unknown>({drawHover,tooltip: renderTooltipProp,onPointClick,+ isPointClickable,onDateRangeZoom,onAreaSelect,className,@@ -335,6 +338,7 @@ export function Chart<Meta = unknown>({pinnable: pinnableTooltip,resolveClickToNearestSeries,onPointClick,+ isPointClickable,onDateRangeZoom,onAreaSelect,resolveValue,@@ -435,7 +439,12 @@ export function Chart<Meta = unknown>({overlayCanvasRef={overlayCanvasRef}className={className}dataAttr={dataAttr}- pointer={hoverIndex >= 0 && !!onPointClick}+ pointer={+ hoverIndex >= 0 &&+ hoverIndex < labels.length &&+ !!onPointClick &&+ (isPointClickable?.(hoverIndex) ?? true)+ }crosshair={!!onDateRangeZoom || !!onAreaSelect}ariaLabel={ariaLabel}handlers={handlers}diff --git a/packages/quill/packages/charts/src/core/hooks/useChartInteraction.ts b/packages/quill/packages/charts/src/core/hooks/useChartInteraction.tsindex dfe6aaf0f663..477c81f8dd3c 100644--- a/packages/quill/packages/charts/src/core/hooks/useChartInteraction.ts+++ b/packages/quill/packages/charts/src/core/hooks/useChartInteraction.ts@@ -45,6 +45,8 @@ interface UseChartInteractionOptions<Meta> {/** See `TooltipConfig.resolveClickToNearestSeries`. */resolveClickToNearestSeries?: booleanonPointClick?: (data: PointClickData<Meta>) => void+ /** See `ChartProps.isPointClickable`. */+ isPointClickable?: (dataIndex: number) => booleanonDateRangeZoom?: (data: DateRangeZoomData) => void/** 2D brush — see `ChartProps.onAreaSelect`. Receives the committed `scales` so chart-type* adapters can map the y pixel range onto their own bands. */@@ -124,6 +126,7 @@ export function useChartInteraction<Meta = unknown>({pinnable,resolveClickToNearestSeries = false,onPointClick,+ isPointClickable,onDateRangeZoom,onAreaSelect,resolveValue = defaultResolveValue,@@ -354,6 +357,8 @@ export function useChartInteraction<Meta = unknown>({return}+ const canClick = (index: number): boolean =>+ !!onPointClick && index < labels.length && (isPointClickable?.(index) ?? true)let currentIndex = hoverIndexRef.currentlet clickPosition = hoverPositionRef.current@@ -381,7 +386,7 @@ export function useChartInteraction<Meta = unknown>({ctx &&pinnable &&resolveClickToNearestSeries &&- onPointClick &&+ canClick(index) &&ctx.seriesData.length > 1) {const clickData = resolveNearestSeriesClickData(@@ -393,7 +398,7 @@ export function useChartInteraction<Meta = unknown>({position)if (clickData) {- onPointClick(wrapClickData && scales ? wrapClickData(clickData, scales) : clickData)+ onPointClick?.(wrapClickData && scales ? wrapClickData(clickData, scales) : clickData)return}}@@ -421,7 +426,7 @@ export function useChartInteraction<Meta = unknown>({if (pinnable && tooltipCtx && tooltipCtx.seriesData.length > 1) {// Opt-in: a click nearer one series than the others is unambiguous, so resolve it// and fire onPointClick directly instead of making the user pin then pick a row.- if (resolveClickToNearestSeries && onPointClick && clickPosition) {+ if (resolveClickToNearestSeries && canClick(currentIndex) && clickPosition) {const clickData = resolveNearestSeriesClickData(currentIndex,series,@@ -431,7 +436,7 @@ export function useChartInteraction<Meta = unknown>({clickPosition)if (clickData) {- onPointClick(wrapClickData && scales ? wrapClickData(clickData, scales) : clickData)+ onPointClick?.(wrapClickData && scales ? wrapClickData(clickData, scales) : clickData)return}}@@ -439,15 +444,16 @@ export function useChartInteraction<Meta = unknown>({return}- if (onPointClick) {+ if (canClick(currentIndex)) {const clickData = buildPointClickData(currentIndex, series, labels, resolveValue, clickPosition)if (clickData) {- onPointClick(wrapClickData && scales ? wrapClickData(clickData, scales) : clickData)+ onPointClick?.(wrapClickData && scales ? wrapClickData(clickData, scales) : clickData)}}},[onPointClick,+ isPointClickable,series,labels,resolveValue,diff --git a/packages/quill/packages/charts/src/docs/interactions.md b/packages/quill/packages/charts/src/docs/interactions.mdindex 9b2672b5eaa7..71b35479f8c1 100644--- a/packages/quill/packages/charts/src/docs/interactions.md+++ b/packages/quill/packages/charts/src/docs/interactions.md@@ -8,6 +8,9 @@ Read the domain object back from `series.meta`, not from array position.On a `pinnable` multi-series chart, a click pins the tooltip first and the user then clicks a row (`DefaultTooltip.onRowClick`).`config.tooltip.resolveClickToNearestSeries` skips the pin and fires `onPointClick` for the nearest series directly; see [tooltips.md](./tooltips.md) for when that is safe.+`isPointClickable(dataIndex)` (on `BarChart`, `TimeSeriesBarChart`, and the base `Chart`) limits `onPointClick` and the pointer cursor to some points.+Other points keep hover and tooltip, and show the drag crosshair if the chart has one.+Chart-specific variants: `FunnelChart.onStepClick` reports `{ stepIndex, converted }`, where `converted: false` means the hatched drop-off track was clicked; `PieChart.onSliceClick`; `BoxPlot.onBoxClick`; `Heatmap.onCellClick` reports `{ xIndex, yIndex, value }`; `ScatterChart.onPointClick` receives the resolved `ScatterPointDatum`.## Drag-to-zoom: `onDateRangeZoom`@@ -18,7 +21,7 @@ The chart does not manage zoom state; the parent decides what to do with the ran- Despite the name it is label-generic: it resolves the drag against label positions, so it works on categorical labels (weekdays, duration buckets) as on dates.- A drag whose edges both snap to the same label (common on sparse charts, a three-bar monthly chart) selects that single bucket, provided the drag spans enough distance to read as intentional.-- The cursor switches to a crosshair when set, except over an actionable point (`onPointClick` set), where it stays a pointer. A plain click without movement still pins the tooltip or fires `onPointClick`.+- The cursor switches to a crosshair when set, except over a clickable point, where it stays a pointer. A plain click without movement still pins the tooltip or fires `onPointClick`.- X-axis only. No effect on charts with a vertical interaction axis (`axisOrientation: 'horizontal'` bars), where the core disables the gesture.- Both emitted values are bucket starts. Widening the end to the last bucket's end is the host's job.diff --git a/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.test.tsx b/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.test.tsxindex e1e27249f722..ff908a535880 100644--- a/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.test.tsx+++ b/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.test.tsx@@ -152,8 +152,6 @@ describe('VolumeSparkline', () => {it('does not fire onSpikeClick for an ordinary (non-spike) bucket', () => {const onSpikeClick = jest.fn()- // A spike elsewhere enables the handler (`hasSpikes`); the click lands on an- // unflagged bucket.const data = buildData({ 2: { isSpike: true, color: 'var(--brand-red)' } })const wrapper = renderChart({ data, onSpikeClick })@@ -163,6 +161,18 @@ describe('VolumeSparkline', () => {expect(onSpikeClick).not.toHaveBeenCalled()})+ it.each([+ { index: 2, cursor: 'cursor-pointer' },+ { index: 0, cursor: 'cursor-crosshair' },+ ])('shows $cursor over bucket $index when only spikes are clickable', ({ index, cursor }) => {+ const data = buildData({ 2: { isSpike: true, color: 'var(--brand-red)' } })+ const wrapper = renderChart({ data, onSpikeClick: jest.fn(), onRangeSelect: jest.fn() })++ hoverAtIndex(wrapper, index, data.length)++ expect(wrapper.classList.contains(cursor)).toBe(true)+ })+it('never fires when no bucket in the data is flagged as a spike', () => {const onSpikeClick = jest.fn()const data = buildData()diff --git a/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.tsx b/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.tsxindex ca7838b3a763..c733f913811d 100644--- a/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.tsx+++ b/products/error_tracking/frontend/components/VolumeSparkline/VolumeSparkline.tsx@@ -1,5 +1,5 @@import { useActions, useValues } from 'kea'-import { useEffect, useMemo, useRef } from 'react'+import { useCallback, useEffect, useMemo, useRef } from 'react'import {type ChartMargins,@@ -146,31 +146,30 @@ export function VolumeSparkline({const hasSpikes = useMemo(() => data.some((datum) => datum.isSpike), [data])+ const isPointClickable = useCallback(+ (dataIndex: number): boolean =>+ (!!onSpikeClick && !!data[dataIndex].isSpike) || (!!onBucketClick && !!bucketEnd(data, dataIndex)),+ [data, onBucketClick, onSpikeClick]+ )+const onPointClick = useMemo(() => {- if (!onBucketClick && !(onSpikeClick && hasSpikes)) {+ if (!onBucketClick && !onSpikeClick) {return undefined}return ({ dataIndex }: PointClickData) => {const datum = data[dataIndex]- if (!datum) {- return- }- const adjacentDate = data[dataIndex + 1]?.date- const previousDate = data[dataIndex - 1]?.date- const endDate =- adjacentDate ??- (previousDate ? new Date(datum.date.getTime() + datum.date.getTime() - previousDate.getTime()) : null)// A flagged spike takes precedence: it opens the spike details popover rather than// filtering to the bucket, so callers passing both handlers still reach the popover.if (datum.isSpike && onSpikeClick) {onSpikeClick(datum, cursorRef.current.x, cursorRef.current.y)return}- if (onBucketClick && endDate && endDate.getTime() > datum.date.getTime()) {+ const endDate = bucketEnd(data, dataIndex)+ if (onBucketClick && endDate) {onBucketClick(datum.date, endDate)}}- }, [data, hasSpikes, onBucketClick, onSpikeClick])+ }, [data, onBucketClick, onSpikeClick])return (<div@@ -186,6 +185,7 @@ export function VolumeSparkline({config={config}onDateRangeZoom={onDateRangeZoom}onPointClick={onPointClick}+ isPointClickable={isPointClickable}dataAttr="error-tracking-volume-sparkline"><HoverReporter sparklineKey={sparklineKey} data={data} />@@ -202,6 +202,13 @@ export function VolumeSparkline({)}+function bucketEnd(data: SparklineData, index: number): Date | null {+ const start = data[index].date+ const previous = data[index - 1]?.date+ const end = data[index + 1]?.date ?? (previous ? new Date(2 * start.getTime() - previous.getTime()) : null)+ return end && end > start ? end : null+}+/** A chart child because `useChartHover` only works inside the chart. Yields to an event marker's* hover (which writes the same logic field), reading it here rather than in the chart host so* mousemove-driven re-renders stay contained to this null-rendering component. */