-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.ts
55 lines (51 loc) · 1.47 KB
/
text.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { MarkComponent as MC, Vector2, Mark } from '../runtime';
import { TextMark } from '../spec';
import { TextShape, TextBadge, TextTag } from '../shape';
import { MaybeTuple, MaybeVisualPosition } from '../transform';
import {
baseGeometryChannels,
basePostInference,
basePreInference,
createBandOffset,
tooltip2d,
visualMark,
} from './utils';
const shape = {
text: TextShape,
badge: TextBadge,
tag: TextTag,
};
export type TextOptions = Omit<TextMark, 'type'>;
export const Text: MC<TextOptions> = (options) => {
const { cartesian = false } = options;
if (cartesian) return visualMark as Mark;
return ((index, scale, value, coordinate) => {
const { x: X, y: Y } = value;
const offset = createBandOffset(scale, value, options);
const P = Array.from(index, (i) => {
const p: Vector2 = [+X[i], +Y[i]];
return [coordinate.map(offset(p, i))] as Vector2[];
});
return [index, P];
}) as Mark;
};
Text.props = {
defaultShape: 'text',
defaultLabelShape: 'label',
composite: false,
shape,
channels: [
...baseGeometryChannels({ shapes: Object.keys(shape) }),
{ name: 'x', required: true },
{ name: 'y', required: true },
{ name: 'text', scale: 'identity' },
{ name: 'fontSize', scale: 'identity' },
{ name: 'rotate', scale: 'identity' },
],
preInference: [
...basePreInference(),
{ type: MaybeTuple },
{ type: MaybeVisualPosition },
],
postInference: [...basePostInference(), ...tooltip2d()],
};