: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): export the grid and axis line drawing for custom charts (#121653)
Sam Pennington35d14e31e5f2
diff --git a/packages/quill/packages/charts/src/core/canvas-renderer.test.ts b/packages/quill/packages/charts/src/core/canvas-renderer.test.tsindex f0fb831f59e7..3d4c8b4b3b3e 100644--- a/packages/quill/packages/charts/src/core/canvas-renderer.test.ts+++ b/packages/quill/packages/charts/src/core/canvas-renderer.test.ts@@ -965,6 +965,20 @@ describe('hog-charts canvas-renderer', () => {})})++ it('draws value lines at valueTicks for a plain value-to-pixel yScale, as a custom chart passes', () => {+ const ctx = mockCanvasContext()+ const plotBottom = dimensions.plotTop + dimensions.plotHeight+ const y = (value: number): number => plotBottom - value+ const snap = (coord: number): number => Math.round(coord) + 0.5++ drawGrid({ ctx, dimensions, yScale: y }, { frame: false, valueTicks: [0, 50, 100] })++ expect(ctx.moveTo.mock.calls).toEqual([+ [dimensions.plotLeft, snap(y(50))],+ [dimensions.plotLeft, snap(y(100))],+ ])+ })})describe('composeDrawHoverWithCrosshair', () => {diff --git a/packages/quill/packages/charts/src/core/canvas-renderer.ts b/packages/quill/packages/charts/src/core/canvas-renderer.tsindex 8dacf84e7173..9fcc40530544 100644--- a/packages/quill/packages/charts/src/core/canvas-renderer.ts+++ b/packages/quill/packages/charts/src/core/canvas-renderer.ts@@ -671,7 +671,10 @@ export interface DrawAxesOptions {/** Draws just the L-shaped axis baselines — the left value axis and the bottom category axis —* without any interior grid lines. For charts that want axis framing but a clean, grid-free plot. */-export function drawAxes(drawCtx: DrawContext, options: DrawAxesOptions = {}): void {+export function drawAxes(+ drawCtx: { ctx: CanvasRenderingContext2D; dimensions: ChartDimensions },+ options: DrawAxesOptions = {}+): void {const { ctx, dimensions } = drawCtxconst { xLine = true, yLine = true } = optionsctx.strokeStyle = options.axisColor ?? 'rgba(0, 0, 0, 0.15)'@@ -760,6 +763,8 @@ export interface DrawGridOptions {orientation?: 'vertical' | 'horizontal'/** Cross-axis grid line positions (x-pixels in vertical mode, y-pixels in horizontal). */categoryTicks?: number[]+ /** Value-axis ticks for a `yScale` with no `.ticks()`, such as the core's `ChartScales.y`. */+ valueTicks?: number[]}/** Draws the grid lines and the full plot-area frame.@@ -771,14 +776,18 @@ export interface DrawGridOptions {* In both modes, `yScale` maps a value to a pixel on the value axis — for vertical that's a y-pixel,* for horizontal that's an x-pixel. The function uses `dimensions` to size the perpendicular axis.*/-export function drawGrid(drawCtx: DrawContext, options: DrawGridOptions = {}): void {+export function drawGrid(+ drawCtx: { ctx: CanvasRenderingContext2D; dimensions: ChartDimensions; yScale: (value: number) => number },+ options: DrawGridOptions = {}+): void {const { ctx, yScale, dimensions } = drawCtxconst gridColor = options.gridColor ?? 'rgba(0, 0, 0, 0.1)'const orientation = options.orientation ?? 'vertical'const tickAxisLength = orientation === 'horizontal' ? dimensions.plotWidth : dimensions.plotHeightconst categoryTicks = options.categoryTicks ?? []- const valueTicks = (yScale as ScaleLinear<number, number>).ticks?.(yTickCountForHeight(tickAxisLength)) ?? []+ const valueTicks =+ options.valueTicks ?? (yScale as ScaleLinear<number, number>).ticks?.(yTickCountForHeight(tickAxisLength)) ?? []const frame = options.frame ?? truectx.strokeStyle = gridColordiff --git a/packages/quill/packages/charts/src/docs/CONTRIBUTING.md b/packages/quill/packages/charts/src/docs/CONTRIBUTING.mdindex 48c2077901b9..49ae7250c5ba 100644--- a/packages/quill/packages/charts/src/docs/CONTRIBUTING.md+++ b/packages/quill/packages/charts/src/docs/CONTRIBUTING.md@@ -38,18 +38,28 @@ export function BarChart({ series, labels, config, theme, ...props }: BarChartPrreturn { x, y, yTicks: () => yScale.ticks() }}, [])- const draw = useCallback(({ ctx, dimensions, scales, series, labels, hoverIndex }: ChartDrawArgs) => {+ const drawStatic = useCallback(({ ctx, dimensions, scales, series, labels, theme }: ChartDrawArgs) => {+ drawGrid(+ { ctx, dimensions, yScale: scales.y },+ { gridColor: theme.gridColor, gridDash: theme.gridDashPattern, frame: false, valueTicks: scales.yTicks() }+ )// Draw bars using canvas-renderer primitives or custom drawing+ drawAxes({ ctx, dimensions }, { axisColor: resolveAxisLineColor(theme) })+ }, [])++ const drawHover = useCallback(({ ctx, hoverIndex }: ChartDrawArgs) => {+ // Highlight the hovered bar; return false when nothing was drawn}, [])return (<Chartseries={series}labels={labels}- config={config}+ config={applyChartDefaults(config)}theme={theme}createScales={createScales}- draw={draw}+ drawStatic={drawStatic}+ drawHover={drawHover}{...props}>{/* Bar-chart-specific overlays as children */}@@ -57,3 +67,7 @@ export function BarChart({ series, labels, config, theme, ...props }: BarChartPr)}```++- `applyChartDefaults` turns on the tick marks and crosshair, as on the built-in charts. The core reads them from `config` and treats a missing switch as off.+- `drawStatic` draws the grid lines first and the axis lines last, as the built-in charts do. `valueTicks: scales.yTicks()` puts the grid lines on the y-axis labels, and `frame: false` leaves the plot edges to `drawAxes`. Code outside the library imports `drawGrid`, `drawAxes` and `resolveAxisLineColor` from the package.+- Leave `tooltip` unset for the stock `DefaultTooltip`, configured through `config.tooltip`. For other content, return `DefaultTooltip` with a `labelRenderer` or `valueFormatter`, or compose `TooltipSurface` and `TooltipSwatch`, so the tooltip still matches the built-in charts.diff --git a/packages/quill/packages/charts/src/docs/axes.md b/packages/quill/packages/charts/src/docs/axes.mdindex a972eb624756..a7656f4e389b 100644--- a/packages/quill/packages/charts/src/docs/axes.md+++ b/packages/quill/packages/charts/src/docs/axes.md@@ -19,6 +19,7 @@ The nested `tooltip` merges key by key, so setting one field keeps `placement: '- `showGrid` draws grid lines on every chart, including the time-series wrappers. Grid lines always align to the primary (left) y-axis ticks. A `showGrid` on the primary `yAxis` config wins; on secondary axes it is ignored, because two grids with unaligned ticks would be noise.- `showAxisLines` draws the L-axis alongside the grid, which then skips its own frame and category lines. A right-positioned y-axis gets its own right axis line. `{ x, y }` toggles each edge independently. With axis lines on, line strokes are trimmed at the y-axis and floored onto the x-axis so they rest against the axis instead of straddling it.- `showTickMarks` draws short canvas tick marks snapped to the same pixel grid as the axis.+- A chart type built on the `Chart` core draws the same lines by calling `drawGrid` before its marks and `drawAxes` after them. Both are exported from the package. See [CONTRIBUTING.md](./CONTRIBUTING.md#adding-a-new-chart-type).- `curve: 'monotone'` smooths line and area series through every point without overshoot.Theme-side restyle knobs, all optional: `theme.axisLineColor` strokes the baselines and tick marks without changing tick-label color (falls back `axisColor`, then `gridColor`); `theme.gridDashPattern` and `theme.crosshairDashPattern` (`[3, 3]`) dash the interior grid lines and the hover crosshair, while plot-edge frame strokes stay solid.diff --git a/packages/quill/packages/charts/src/index.ts b/packages/quill/packages/charts/src/index.tsindex 69b2ab298f7b..6c7767972c28 100644--- a/packages/quill/packages/charts/src/index.ts+++ b/packages/quill/packages/charts/src/index.ts@@ -201,6 +201,10 @@ export { themeDefaultsFromCssVars, themeFromCssVars, useChartTheme, DEFAULT_CHARexport { applyChartDefaults, DEFAULT_CHART_CONFIG } from './core/chart-config'export type { ThemeFromCssOptions } from './core/theme'+// Grid and axis lines, for chart types built on the Chart core+export { drawAxes, drawGrid, resolveAxisLineColor } from './core/canvas-renderer'+export type { DrawAxesOptions, DrawGridOptions } from './core/canvas-renderer'+// Built-in tooltip (for reference or extension)export { DefaultTooltip, type DefaultTooltipProps } from './overlays/DefaultTooltip'// Shared tooltip surface — reuse to build custom tooltips with the quill look