diff --git a/__tests__/integration/snapshots/static/aaplIntervalDateEncodeX.png b/__tests__/integration/snapshots/static/aaplIntervalDateEncodeX.png new file mode 100644 index 0000000000..7f3781409a Binary files /dev/null and b/__tests__/integration/snapshots/static/aaplIntervalDateEncodeX.png differ diff --git a/__tests__/integration/spec-static.spec.ts b/__tests__/integration/spec-static.spec.ts index 0f83c7674b..235ef0ef75 100644 --- a/__tests__/integration/spec-static.spec.ts +++ b/__tests__/integration/spec-static.spec.ts @@ -12,8 +12,10 @@ describe('Charts', () => { it(`[Canvas]: ${name}`, async () => { try { // @ts-ignore - const { maxError = 0 } = generateOptions; + const { maxError = 0, before, after } = generateOptions; + before?.(); gCanvas = await renderSpec(generateOptions); + after?.(); const dir = `${__dirname}/snapshots/static`; await expect(gCanvas).toMatchCanvasSnapshot(dir, name, { maxError }); } finally { diff --git a/__tests__/plots/static/aapl-interval-date-encode-x.ts b/__tests__/plots/static/aapl-interval-date-encode-x.ts new file mode 100644 index 0000000000..7bf0b2bd42 --- /dev/null +++ b/__tests__/plots/static/aapl-interval-date-encode-x.ts @@ -0,0 +1,29 @@ +import { G2Spec } from '../../../src'; + +export function aaplIntervalDateEncodeX(): G2Spec { + return { + type: 'interval', + padding: 'auto', + height: 720, + data: { + type: 'fetch', + value: 'data/aapl.csv', + transform: [{ type: 'slice', start: 0, end: 10 }], + }, + encode: { + x: 'date', + y: 'close', + }, + }; +} + +let toString; + +aaplIntervalDateEncodeX.before = () => { + toString = Date.prototype.toString; + Date.prototype.toString = Date.prototype.toUTCString; +}; + +aaplIntervalDateEncodeX.after = () => { + Date.prototype.toString = toString; +}; diff --git a/__tests__/plots/static/index.ts b/__tests__/plots/static/index.ts index dd69ca2c7c..bb43d189d4 100644 --- a/__tests__/plots/static/index.ts +++ b/__tests__/plots/static/index.ts @@ -226,3 +226,4 @@ export { populationIntervalDivergingAutoPaddingUndefinedTitle } from './populati export { tranLineMultiAxesAutoPaddingTickMethod } from './train-line-multi-axes-auto-padding-tick-method'; export { vaccinesCellScaleRelationAutoPaddingTickFilter } from './vaccines-cell-scale-relation-auto-padding-tick-filter'; export { marketIntervalMarimekkoAutoPaddingFlex } from './market-interval-marimekko-auto-padding-flex'; +export { aaplIntervalDateEncodeX } from './aapl-interval-date-encode-x'; diff --git a/src/component/axis.ts b/src/component/axis.ts index 9b5e4db639..926d331748 100644 --- a/src/component/axis.ts +++ b/src/component/axis.ts @@ -148,7 +148,12 @@ function getData( const ticks = ticksOf(scale, domain, tickMethod); const filteredTicks = tickFilter ? ticks.filter(tickFilter) : ticks; - const toString = (d) => (typeof d === 'object' && !!d ? d : String(d)); + const toString = (d) => + d instanceof Date + ? String(d) + : typeof d === 'object' && !!d + ? d + : String(d); const labelFormatter = defaultTickFormatter || scale.getFormatter?.() || toString; const applyInset = createInset(position, coordinate);