An open source design system that's fully customizable and agent ready
TypeScript
fix(charts): ignore xDomain for categorical x (#10182)
Cindy Zhang3fc5e0d704f7
diff --git a/packages/charts/src/Chart.test.tsx b/packages/charts/src/Chart.test.tsxindex dcee800e83..4523799f71 100644--- a/packages/charts/src/Chart.test.tsx+++ b/packages/charts/src/Chart.test.tsx@@ -16,6 +16,7 @@ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';import {render, screen, act, within, fireEvent} from '@testing-library/react';import {Chart} from './Chart';import {bar, line} from './marks';+import {computeLayout} from './layout';import {InternationalizationProvider} from '@astryxdesign/core';const data = [@@ -83,6 +84,58 @@ describe('Chart accessible name', () => {).toBeInTheDocument();});+ it('ignores xDomain for a categorical x-axis', () => {+ const {container} = render(+ <Chart+ data={data}+ xKey="month"+ series={[bar('revenue')]}+ xDomain={[0, 10]}+ />,+ );+ reportWidth(600);++ // xDomain is documented as ignored for band scales: the categories must+ // still drive the axis and the bars must still render.+ expect(screen.getByText('Jan')).toBeInTheDocument();+ expect(screen.getByText('Mar')).toBeInTheDocument();+ const plot = container.querySelector('g[clip-path]');+ expect(plot).not.toBeNull();+ const bars = Array.from(plot!.querySelectorAll('path'));+ expect(bars).toHaveLength(3);+ for (const path of bars) {+ expect(path.getAttribute('d')).not.toContain('NaN');+ }+ });++ it('keeps xDomain as a linear window for Date and numeric-string x', () => {+ const start = Date.UTC(2026, 0, 1);+ const hour = 60 * 60 * 1000;+ for (const toX of [+ (ms: number) => new Date(ms),+ (ms: number) => String(ms),+ ]) {+ const {xScale} = computeLayout({+ data: [+ {t: toX(start), v: 1},+ {t: toX(start + hour), v: 2},+ {t: toX(start + 3 * hour), v: 3},+ ],+ xKey: 't',+ series: [line('v')],+ width: 600,+ height: 300,+ xDomain: [start, start + 3 * hour],+ });+ // A band scale would space the readings evenly; the window places+ // them by time.+ expect('bandwidth' in xScale).toBe(false);+ expect((xScale as (value: number) => number)(start + hour)).toBeCloseTo(+ 200,+ );+ }+ });+it('prefers the title prop as the accessible name', () => {render(<Chartdiff --git a/packages/charts/src/layout.ts b/packages/charts/src/layout.tsindex 2856eb940f..58604d2a79 100644--- a/packages/charts/src/layout.ts+++ b/packages/charts/src/layout.ts@@ -113,11 +113,19 @@ export function computeLayout({const isNumericX =xValues.length > 0 && xValues.every(v => typeof v === 'number');+ // Strings that are not numbers are categories. Dates and numeric strings+ // still place on a linear scale.+ const isCategoricalX = xValues.some(+ v => typeof v === 'string' && Number.isNaN(Number(v)),+ );+let xScale: ChartScale;- if (xDomain && isFiniteDomain(xDomain)) {+ if (xDomain && isFiniteDomain(xDomain) && !isCategoricalX) {// Explicit domain is authoritative (no .nice()) — enables a stable// streaming window and a valid scale even when `data` is empty. Trusted// only when finite; a NaN/Infinity domain would collapse the scale.+ // A linear scale cannot place category strings: bars then draw nothing,+ // and other marks get no x position.xScale = scaleLinear().domain(xDomain).range([0, width]);} else if (isNumericX) {// NaN passes `typeof === 'number'`, so derive the extent defensively.