-
Notifications
You must be signed in to change notification settings - Fork 592
/
offset.ts
65 lines (59 loc) · 1.72 KB
/
offset.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
56
57
58
59
60
61
62
63
64
65
/**
* Utility files for producing Vega ValueRef for marks
*/
import type {SignalRef} from 'vega';
import {PolarPositionChannel, PositionChannel} from '../../../channel';
import {Encoding} from '../../../encoding';
import {Mark, MarkDef} from '../../../mark';
import {VgValueRef} from '../../../vega.schema';
import {signalOrValueRef} from '../../common';
import {UnitModel} from '../../unit';
import {midPoint} from './valueref';
export interface Offset {
offsetType?: 'visual' | 'encoding';
offset?: number | VgValueRef;
}
export function positionOffset({
channel: baseChannel,
markDef,
encoding = {},
model,
bandPosition
}: {
channel: PositionChannel | PolarPositionChannel;
markDef: MarkDef<Mark, SignalRef>;
encoding?: Encoding<string>;
model?: UnitModel;
bandPosition?: number | SignalRef;
}): Offset {
const channel = `${baseChannel}Offset` as
| 'xOffset'
| 'yOffset'
| 'x2Offset'
| 'y2Offset'
| 'thetaOffset'
| 'radiusOffset'
| 'theta2Offset'
| 'radius2Offset'; // Need to cast as the type can't be inferred automatically
const defaultValue = markDef[channel];
const channelDef = encoding[channel];
if ((channel === 'xOffset' || channel === 'yOffset') && channelDef) {
const ref = midPoint({
channel: channel,
channelDef,
markDef,
config: model?.config,
scaleName: model.scaleName(channel),
scale: model.getScaleComponent(channel),
stack: null,
defaultRef: signalOrValueRef(defaultValue),
bandPosition
});
return {offsetType: 'encoding', offset: ref};
}
const markDefOffsetValue = markDef[channel];
if (markDefOffsetValue) {
return {offsetType: 'visual', offset: markDefOffsetValue};
}
return {};
}