Skip to content

Commit

Permalink
fix(label): support date label (#5077)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini authored May 23, 2023
1 parent 1ee4b1f commit 826e000
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion __tests__/integration/spec-static.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions __tests__/plots/static/aapl-interval-date-encode-x.ts
Original file line number Diff line number Diff line change
@@ -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;
};
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
7 changes: 6 additions & 1 deletion src/component/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 826e000

Please sign in to comment.