Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix: Tier 1 correctness — xKey in context, bars from zero, shared utils (#1402)

Cindy Zhangaf22bb352905
diff --git a/packages/lab/src/Chart/XDSChart.tsx b/packages/lab/src/Chart/XDSChart.tsx
index e70ce1eb4c..3b8aa969f1 100644
--- a/packages/lab/src/Chart/XDSChart.tsx
+++ b/packages/lab/src/Chart/XDSChart.tsx
@@ -157,11 +157,12 @@ export function XDSChart({
width: innerWidth,
height: innerHeight,
margin,
+ xKey,
data,
xScale,
yScale,
}),
- [innerWidth, innerHeight, margin, data, xScale, yScale],
+ [innerWidth, innerHeight, margin, xKey, data, xScale, yScale],
);
return (
diff --git a/packages/lab/src/Chart/XDSChartArea.tsx b/packages/lab/src/Chart/XDSChartArea.tsx
index dc11e77983..7c738da510 100644
--- a/packages/lab/src/Chart/XDSChartArea.tsx
+++ b/packages/lab/src/Chart/XDSChartArea.tsx
@@ -7,17 +7,14 @@
import {useMemo} from 'react';
import {area, curveMonotoneX} from 'd3-shape';
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {xPixel} from './utils';
export interface XDSChartAreaProps {
/** Data key for the upper bound. If omitted, uses `baseline`. */
yUpper?: string;
/** Data key for the lower bound. If omitted, uses `baseline`. */
yLower?: string;
- /**
- * Fallback data key when only one bound is specified.
- * Acts as the other edge of the band.
- */
+ /** Fallback data key when only one bound is specified. */
baseline?: string;
/** Fill color */
color: string;
@@ -29,26 +26,14 @@ export interface XDSChartAreaProps {
strokeWidth?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Area band between two y-values. Use for confidence intervals, error ranges,
* or any min/max band around a line.
*
- * Supports full band (both bounds), upper-only, or lower-only:
- *
* @example
* ```
- * // Full band
* <XDSChartArea yUpper="upper95" yLower="lower95" color={colors[0]} />
- *
- * // Upper confidence only — shades from mean up to upper bound
* <XDSChartArea yUpper="upper95" baseline="mean" color={colors[0]} />
- *
- * // Lower confidence only — shades from lower bound up to mean
- * <XDSChartArea yLower="lower95" baseline="mean" color={colors[0]} />
* ```
*/
export function XDSChartArea({
@@ -60,24 +45,15 @@ export function XDSChartArea({
stroke = false,
strokeWidth = 1,
}: XDSChartAreaProps) {
- const {data, xScale, yScale} = useChart();
+ const {data, xKey, xScale, yScale} = useChart();
- // Resolve which keys to use for upper and lower edges
const upperKey = yUpper ?? baseline;
const lowerKey = yLower ?? baseline;
const pathD = useMemo(() => {
if (!upperKey || !lowerKey) return '';
-
const generator = area<Record<string, unknown>>()
- .x(d => {
- if (isBandScale(xScale)) {
- return (
- (xScale(String(Object.values(d)[0])) ?? 0) + xScale.bandwidth() / 2
- );
- }
- return (xScale as (v: number) => number)(Object.values(d)[0] as number);
- })
+ .x(d => xPixel(d, xKey, xScale))
.y0(d => {
const v = typeof d[lowerKey] === 'number' ? (d[lowerKey] as number) : 0;
return yScale(v);
@@ -88,7 +64,7 @@ export function XDSChartArea({
})
.curve(curveMonotoneX);
return generator(data) ?? '';
- }, [data, xScale, yScale, upperKey, lowerKey]);
+ }, [data, xKey, xScale, yScale, upperKey, lowerKey]);
if (!pathD) return null;
diff --git a/packages/lab/src/Chart/XDSChartAxis.tsx b/packages/lab/src/Chart/XDSChartAxis.tsx
index 38dc83bdbe..08494f894b 100644
--- a/packages/lab/src/Chart/XDSChartAxis.tsx
+++ b/packages/lab/src/Chart/XDSChartAxis.tsx
@@ -6,7 +6,8 @@
import {useMemo} from 'react';
import {useChart} from './ChartContext';
-import type {ScaleBand, ScaleLinear, ScaleTime} from 'd3-scale';
+import {isBandScale} from './utils';
+import type {ScaleLinear, ScaleTime} from 'd3-scale';
export interface XDSChartAxisProps {
/** Which edge to render the axis on */
@@ -17,10 +18,6 @@ export interface XDSChartAxisProps {
tickFormat?: (value: unknown) => string;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Chart axis. Renders tick marks and labels for the x or y dimension.
*
diff --git a/packages/lab/src/Chart/XDSChartBar.tsx b/packages/lab/src/Chart/XDSChartBar.tsx
index fc435919f2..37473f95de 100644
--- a/packages/lab/src/Chart/XDSChartBar.tsx
+++ b/packages/lab/src/Chart/XDSChartBar.tsx
@@ -5,7 +5,7 @@
*/
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {isBandScale} from './utils';
export interface XDSChartBarProps {
/** Which data key to visualize */
@@ -16,12 +16,9 @@ export interface XDSChartBarProps {
radius?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Bar marks. Requires a band xScale (categorical x-axis).
+ * Bars grow from the zero line — handles both positive and negative values.
*
* @example
* ```
@@ -29,20 +26,26 @@ function isBandScale(scale: unknown): scale is ScaleBand<string> {
* ```
*/
export function XDSChartBar({dataKey, color, radius = 4}: XDSChartBarProps) {
- const {data, xScale, yScale, height} = useChart();
+ const {data, xKey, xScale, yScale} = useChart();
if (!isBandScale(xScale)) return null;
+ // Zero line position — bars grow from here
+ const zeroY = yScale(0);
+
return (
<g>
{data.map((d, i) => {
- const xVal = xScale(String(Object.values(d)[0]));
+ const xVal = xScale(String(d[xKey]));
if (xVal == null) return null;
const yVal =
typeof d[dataKey] === 'number' ? (d[dataKey] as number) : 0;
- const barHeight = height - yScale(yVal);
- const barY = yScale(yVal);
+ const yPos = yScale(yVal);
+
+ // Bar grows from zero line toward the value
+ const barY = Math.min(yPos, zeroY);
+ const barHeight = Math.abs(yPos - zeroY);
return (
<rect
diff --git a/packages/lab/src/Chart/XDSChartCandlestick.tsx b/packages/lab/src/Chart/XDSChartCandlestick.tsx
index 207d4c86ad..38dd854813 100644
--- a/packages/lab/src/Chart/XDSChartCandlestick.tsx
+++ b/packages/lab/src/Chart/XDSChartCandlestick.tsx
@@ -9,7 +9,7 @@
*/
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {isBandScale} from './utils';
export interface XDSChartCandlestickProps {
/** Data key for the high/max value (top of whisker) */
@@ -42,10 +42,6 @@ export interface XDSChartCandlestickProps {
dotRadius?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Candlestick / box-whisker marks with optional Tufte variant.
*
@@ -80,7 +76,7 @@ export function XDSChartCandlestick({
radius = 2,
dotRadius = 3,
}: XDSChartCandlestickProps) {
- const {data, xScale, yScale} = useChart();
+ const {data, xKey, xScale, yScale} = useChart();
if (!isBandScale(xScale)) return null;
const bw = xScale.bandwidth();
@@ -88,7 +84,7 @@ export function XDSChartCandlestick({
return (
<g>
{data.map((d, i) => {
- const xVal = xScale(String(Object.values(d)[0]));
+ const xVal = xScale(String(d[xKey]));
if (xVal == null) return null;
const hVal = typeof d[high] === 'number' ? (d[high] as number) : 0;
diff --git a/packages/lab/src/Chart/XDSChartDot.tsx b/packages/lab/src/Chart/XDSChartDot.tsx
index 8c932ac47a..bc8b1b1363 100644
--- a/packages/lab/src/Chart/XDSChartDot.tsx
+++ b/packages/lab/src/Chart/XDSChartDot.tsx
@@ -1,11 +1,11 @@
/**
* @file XDSChartDot.tsx
- * @output Renders scatter dots for a pair of data keys
+ * @output Renders scatter dots for a data key
* @position Child of XDSChart; reads scales from context
*/
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {xPixel} from './utils';
export interface XDSChartDotProps {
/** Which data key for the y values */
@@ -16,10 +16,6 @@ export interface XDSChartDotProps {
radius?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Scatter dot marks.
*
@@ -29,19 +25,12 @@ function isBandScale(scale: unknown): scale is ScaleBand<string> {
* ```
*/
export function XDSChartDot({dataKey, color, radius = 4}: XDSChartDotProps) {
- const {data, xScale, yScale} = useChart();
+ const {data, xKey, xScale, yScale} = useChart();
return (
<g>
{data.map((d, i) => {
- let x: number;
- if (isBandScale(xScale)) {
- x =
- (xScale(String(Object.values(d)[0])) ?? 0) + xScale.bandwidth() / 2;
- } else {
- const xVal = Object.values(d)[0];
- x = (xScale as (v: number) => number)(xVal as number);
- }
+ const x = xPixel(d, xKey, xScale);
const yVal =
typeof d[dataKey] === 'number' ? (d[dataKey] as number) : 0;
return (
diff --git a/packages/lab/src/Chart/XDSChartDotGL.tsx b/packages/lab/src/Chart/XDSChartDotGL.tsx
index 0e1e50e72e..5dfbf72186 100644
--- a/packages/lab/src/Chart/XDSChartDotGL.tsx
+++ b/packages/lab/src/Chart/XDSChartDotGL.tsx
@@ -6,7 +6,7 @@
import {useRef, useEffect, useCallback} from 'react';
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {xPixel} from './utils';
export interface XDSChartDotGLProps {
/** Which data key for the y values */
@@ -19,10 +19,6 @@ export interface XDSChartDotGLProps {
opacity?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/** Parse hex color to [r, g, b] floats 0-1 */
function hexToGL(hex: string): [number, number, number] {
const n = parseInt(hex.replace('#', ''), 16);
@@ -102,7 +98,7 @@ export function XDSChartDotGL({
size = 6,
opacity = 0.8,
}: XDSChartDotGLProps) {
- const {data, xScale, yScale, width, height} = useChart();
+ const {data, xKey, xScale, yScale, width, height} = useChart();
const canvasRef = useRef<HTMLCanvasElement>(null);
const glRef = useRef<WebGLRenderingContext | null>(null);
const programRef = useRef<WebGLProgram | null>(null);
@@ -111,18 +107,12 @@ export function XDSChartDotGL({
const getPositions = useCallback((): Float32Array => {
const positions: number[] = [];
for (const d of data) {
- let x: number;
- if (isBandScale(xScale)) {
- x = (xScale(String(Object.values(d)[0])) ?? 0) + xScale.bandwidth() / 2;
- } else {
- const xVal = Object.values(d)[0];
- x = (xScale as (v: number) => number)(xVal as number);
- }
+ const x = xPixel(d, xKey, xScale);
const yVal = typeof d[dataKey] === 'number' ? (d[dataKey] as number) : 0;
positions.push(x, yScale(yVal));
}
return new Float32Array(positions);
- }, [data, dataKey, xScale, yScale]);
+ }, [data, xKey, dataKey, xScale, yScale]);
useEffect(() => {
const canvas = canvasRef.current;
diff --git a/packages/lab/src/Chart/XDSChartDotGLInteractive.tsx b/packages/lab/src/Chart/XDSChartDotGLInteractive.tsx
index c1ac2caeea..52eb53ea05 100644
--- a/packages/lab/src/Chart/XDSChartDotGLInteractive.tsx
+++ b/packages/lab/src/Chart/XDSChartDotGLInteractive.tsx
@@ -17,7 +17,7 @@ import {
type ReactNode,
} from 'react';
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {xPixel} from './utils';
export interface XDSChartDotGLInteractiveProps {
/** Which data key for the y values */
@@ -35,10 +35,6 @@ export interface XDSChartDotGLInteractiveProps {
renderTooltip?: (datum: Record<string, unknown>, index: number) => ReactNode;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
function hexToGL(hex: string): [number, number, number] {
const n = parseInt(hex.replace('#', ''), 16);
return [(n >> 16) / 255, ((n >> 8) & 0xff) / 255, (n & 0xff) / 255];
@@ -166,7 +162,7 @@ export function XDSChartDotGLInteractive({
opacity = 0.8,
renderTooltip,
}: XDSChartDotGLInteractiveProps) {
- const {data, xScale, yScale, width, height} = useChart();
+ const {data, xKey, xScale, yScale, width, height} = useChart();
const visCanvasRef = useRef<HTMLCanvasElement>(null);
const pickCanvasRef = useRef<HTMLCanvasElement>(null);
const [hoverIndex, setHoverIndex] = useState<number>(-1);
@@ -188,17 +184,12 @@ export function XDSChartDotGLInteractive({
const positions = useMemo(() => {
const pos: number[] = [];
for (const d of data) {
- let x: number;
- if (isBandScale(xScale)) {
- x = (xScale(String(Object.values(d)[0])) ?? 0) + xScale.bandwidth() / 2;
- } else {
- x = (xScale as (v: number) => number)(Object.values(d)[0] as number);
- }
+ const x = xPixel(d, xKey, xScale);
const yVal = typeof d[dataKey] === 'number' ? (d[dataKey] as number) : 0;
pos.push(x, yScale(yVal));
}
return new Float32Array(pos);
- }, [data, dataKey, xScale, yScale]);
+ }, [data, xKey, dataKey, xScale, yScale]);
// Pick colors — one unique color per point
const pickColors = useMemo(() => {
diff --git a/packages/lab/src/Chart/XDSChartErrorBar.tsx b/packages/lab/src/Chart/XDSChartErrorBar.tsx
index 753090a38e..562956df48 100644
--- a/packages/lab/src/Chart/XDSChartErrorBar.tsx
+++ b/packages/lab/src/Chart/XDSChartErrorBar.tsx
@@ -5,7 +5,7 @@
*/
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {xPixel} from './utils';
export interface XDSChartErrorBarProps {
/** Data key for the upper bound */
@@ -20,10 +20,6 @@ export interface XDSChartErrorBarProps {
capWidth?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Error bars with whisker caps. Pair with XDSChartBar or XDSChartDot.
*
@@ -40,19 +36,12 @@ export function XDSChartErrorBar({
strokeWidth = 1.5,
capWidth = 8,
}: XDSChartErrorBarProps) {
- const {data, xScale, yScale} = useChart();
+ const {data, xKey, xScale, yScale} = useChart();
return (
<g>
{data.map((d, i) => {
- let x: number;
- if (isBandScale(xScale)) {
- x =
- (xScale(String(Object.values(d)[0])) ?? 0) + xScale.bandwidth() / 2;
- } else {
- x = (xScale as (v: number) => number)(Object.values(d)[0] as number);
- }
-
+ const x = xPixel(d, xKey, xScale);
const upper = typeof d[yUpper] === 'number' ? (d[yUpper] as number) : 0;
const lower = typeof d[yLower] === 'number' ? (d[yLower] as number) : 0;
const yTop = yScale(upper);
@@ -61,7 +50,6 @@ export function XDSChartErrorBar({
return (
<g key={i}>
- {/* Vertical stem */}
<line
x1={x}
x2={x}
@@ -70,7 +58,6 @@ export function XDSChartErrorBar({
stroke={color}
strokeWidth={strokeWidth}
/>
- {/* Top cap */}
<line
x1={x - half}
x2={x + half}
@@ -79,7 +66,6 @@ export function XDSChartErrorBar({
stroke={color}
strokeWidth={strokeWidth}
/>
- {/* Bottom cap */}
<line
x1={x - half}
x2={x + half}
diff --git a/packages/lab/src/Chart/XDSChartGrid.tsx b/packages/lab/src/Chart/XDSChartGrid.tsx
index 656dd2534d..fedc2270db 100644
--- a/packages/lab/src/Chart/XDSChartGrid.tsx
+++ b/packages/lab/src/Chart/XDSChartGrid.tsx
@@ -6,7 +6,8 @@
import {useMemo} from 'react';
import {useChart} from './ChartContext';
-import type {ScaleBand, ScaleLinear, ScaleTime} from 'd3-scale';
+import {isBandScale} from './utils';
+import type {ScaleLinear, ScaleTime} from 'd3-scale';
export interface XDSChartGridProps {
/** Show horizontal grid lines (default: true) */
@@ -15,10 +16,6 @@ export interface XDSChartGridProps {
vertical?: boolean;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Grid lines behind chart marks.
*
diff --git a/packages/lab/src/Chart/XDSChartHeatmapGL.tsx b/packages/lab/src/Chart/XDSChartHeatmapGL.tsx
index 031790699a..b2a7d6b2b7 100644
--- a/packages/lab/src/Chart/XDSChartHeatmapGL.tsx
+++ b/packages/lab/src/Chart/XDSChartHeatmapGL.tsx
@@ -11,6 +11,7 @@
import {useRef, useEffect, useMemo} from 'react';
import {scaleBand} from 'd3-scale';
import {useChart} from './ChartContext';
+import {isBandScale} from './utils';
import type {ScaleBand} from 'd3-scale';
export interface XDSChartHeatmapGLProps {
@@ -31,10 +32,6 @@ export interface XDSChartHeatmapGLProps {
cellGap?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
function hexToGL(hex: string): [number, number, number] {
const n = parseInt(hex.replace('#', ''), 16);
return [(n >> 16) / 255, ((n >> 8) & 0xff) / 255, (n & 0xff) / 255];
diff --git a/packages/lab/src/Chart/XDSChartLine.tsx b/packages/lab/src/Chart/XDSChartLine.tsx
index 1a8c01503e..32a93273b1 100644
--- a/packages/lab/src/Chart/XDSChartLine.tsx
+++ b/packages/lab/src/Chart/XDSChartLine.tsx
@@ -7,7 +7,7 @@
import {useMemo} from 'react';
import {line, curveMonotoneX} from 'd3-shape';
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {xPixel} from './utils';
export interface XDSChartLineProps {
/** Which data key to visualize */
@@ -22,10 +22,6 @@ export interface XDSChartLineProps {
dotRadius?: number;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Line mark. Works with both band and linear x-scales.
*
@@ -42,21 +38,15 @@ export function XDSChartLine({
dots = false,
dotRadius = 3,
}: XDSChartLineProps) {
- const {data, xScale, yScale} = useChart();
+ const {data, xKey, xScale, yScale} = useChart();
const points = useMemo(() => {
return data.map((d, i) => {
- let x: number;
- if (isBandScale(xScale)) {
- x = (xScale(String(Object.values(d)[0])) ?? 0) + xScale.bandwidth() / 2;
- } else {
- const xVal = Object.values(d)[0];
- x = (xScale as (v: number) => number)(xVal as number);
- }
+ const x = xPixel(d, xKey, xScale);
const yVal = typeof d[dataKey] === 'number' ? (d[dataKey] as number) : 0;
return {x, y: yScale(yVal), index: i};
});
- }, [data, dataKey, xScale, yScale]);
+ }, [data, xKey, dataKey, xScale, yScale]);
const pathD = useMemo(() => {
const generator = line<{x: number; y: number}>()
diff --git a/packages/lab/src/Chart/XDSChartTooltip.tsx b/packages/lab/src/Chart/XDSChartTooltip.tsx
index 3a1e0bba84..67bb2fb3c5 100644
--- a/packages/lab/src/Chart/XDSChartTooltip.tsx
+++ b/packages/lab/src/Chart/XDSChartTooltip.tsx
@@ -6,7 +6,7 @@
import {useState, useCallback, type ReactNode} from 'react';
import {useChart} from './ChartContext';
-import type {ScaleBand} from 'd3-scale';
+import {isBandScale, xPixel} from './utils';
export interface XDSChartTooltipProps {
/**
@@ -17,10 +17,6 @@ export interface XDSChartTooltipProps {
render?: (datum: Record<string, unknown>, index: number) => ReactNode;
}
-function isBandScale(scale: unknown): scale is ScaleBand<string> {
- return typeof scale === 'function' && 'bandwidth' in scale;
-}
-
/**
* Tooltip overlay. Renders a floating card near the hovered data point.
* Place as the last child inside XDSChart so it renders on top.
@@ -32,7 +28,7 @@ function isBandScale(scale: unknown): scale is ScaleBand<string> {
* ```
*/
export function XDSChartTooltip({render}: XDSChartTooltipProps) {
- const {data, xScale, yScale, width, height} = useChart();
+ const {data, xKey, xScale, yScale, width, height} = useChart();
const [hoverIndex, setHoverIndex] = useState<number | null>(null);
const handleMouseMove = useCallback(
@@ -61,8 +57,7 @@ export function XDSChartTooltip({render}: XDSChartTooltipProps) {
let closest = 0;
let minDist = Infinity;
data.forEach((d, i) => {
- const xVal = Object.values(d)[0] as number;
- const dist = Math.abs(linearScale(xVal) - cursor.x);
+ const dist = Math.abs(xPixel(d, xKey, xScale) - cursor.x);
if (dist < minDist) {
minDist = dist;
closest = i;
@@ -83,12 +78,9 @@ export function XDSChartTooltip({render}: XDSChartTooltipProps) {
let tooltipY = 0;
if (datum && hoverIndex !== null) {
if (isBandScale(xScale)) {
- tooltipX =
- (xScale(String(Object.values(datum)[0])) ?? 0) + xScale.bandwidth() / 2;
+ tooltipX = xPixel(datum, xKey, xScale);
} else {
- tooltipX = (xScale as (v: number) => number)(
- Object.values(datum)[0] as number,
- );
+ tooltipX = xPixel(datum, xKey, xScale);
}
// Find the max y value across all numeric keys for positioning
const yVals = Object.values(datum).filter(
diff --git a/packages/lab/src/Chart/index.ts b/packages/lab/src/Chart/index.ts
index 9b7f61c375..b9d552c002 100644
--- a/packages/lab/src/Chart/index.ts
+++ b/packages/lab/src/Chart/index.ts
@@ -33,6 +33,7 @@ export {
export {useChart} from './ChartContext';
export type {ChartContext, ChartMargin, ChartScale} from './types';
export {m4Reduce, type M4Point} from './m4';
+export {isBandScale, xPixel} from './utils';
export {useXDSChartColors} from './useXDSChartColors';
export {
getXDSChartColors,
diff --git a/packages/lab/src/Chart/types.ts b/packages/lab/src/Chart/types.ts
index a1d468b8b6..7365df7edd 100644
--- a/packages/lab/src/Chart/types.ts
+++ b/packages/lab/src/Chart/types.ts
@@ -28,6 +28,8 @@ export interface ChartContext {
height: number;
/** Margin offsets */
margin: ChartMargin;
+ /** The data key used for x-axis values */
+ xKey: string;
/** The full dataset */
data: Record<string, unknown>[];
/** X scale — band for categorical, linear/time for continuous */
diff --git a/packages/lab/src/Chart/utils.ts b/packages/lab/src/Chart/utils.ts
new file mode 100644
index 0000000000..98e88f5e9f
--- /dev/null
+++ b/packages/lab/src/Chart/utils.ts
@@ -0,0 +1,29 @@
+/**
+ * @file utils.ts
+ * @output Shared utilities for chart components
+ * @position Internal helpers; consumed by mark, axis, and interaction components
+ */
+
+import type {ScaleBand} from 'd3-scale';
+import type {ChartScale} from './types';
+
+/** Type guard for d3 band scales */
+export function isBandScale(scale: ChartScale): scale is ScaleBand<string> {
+ return typeof scale === 'function' && 'bandwidth' in scale;
+}
+
+/**
+ * Get the x pixel position for a datum using the chart's xKey and xScale.
+ * For band scales, returns the center of the band.
+ */
+export function xPixel(
+ d: Record<string, unknown>,
+ xKey: string,
+ xScale: ChartScale,
+): number {
+ const raw = d[xKey];
+ if (isBandScale(xScale)) {
+ return (xScale(String(raw)) ?? 0) + xScale.bandwidth() / 2;
+ }
+ return (xScale as (v: number) => number)(raw as number);
+}