-
Notifications
You must be signed in to change notification settings - Fork 622
/
tooltip.ts
158 lines (137 loc) · 4.87 KB
/
tooltip.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {array, isArray, isObject, isString} from 'vega-util';
import {isBinned} from '../../../bin';
import {getMainRangeChannel, isXorY, Channel, THETA, RADIUS} from '../../../channel';
import {
defaultTitle,
getFieldDef,
getFormatMixins,
hasConditionalFieldDef,
isFieldDef,
isTypedFieldDef,
SecondaryFieldDef,
TypedFieldDef,
vgField
} from '../../../channeldef';
import {Config} from '../../../config';
import {Encoding, forEach} from '../../../encoding';
import {StackProperties} from '../../../stack';
import {entries} from '../../../util';
import {isSignalRef} from '../../../vega.schema';
import {getMarkPropOrConfig} from '../../common';
import {binFormatExpression, formatSignalRef} from '../../format';
import {UnitModel} from '../../unit';
import {wrapCondition} from './conditional';
import {textRef} from './text';
export function tooltip(model: UnitModel, opt: {reactiveGeom?: boolean} = {}) {
const {encoding, markDef, config, stack} = model;
const channelDef = encoding.tooltip;
if (isArray(channelDef)) {
return {tooltip: tooltipRefForEncoding({tooltip: channelDef}, stack, config, opt)};
} else {
const datum = opt.reactiveGeom ? 'datum.datum' : 'datum';
return wrapCondition(model, channelDef, 'tooltip', cDef => {
// use valueRef based on channelDef first
const tooltipRefFromChannelDef = textRef(cDef, config, datum);
if (tooltipRefFromChannelDef) {
return tooltipRefFromChannelDef;
}
if (cDef === null) {
// Allow using encoding.tooltip = null to disable tooltip
return undefined;
}
let markTooltip = getMarkPropOrConfig('tooltip', markDef, config);
if (markTooltip === true) {
markTooltip = {content: 'encoding'};
}
if (isString(markTooltip)) {
return {value: markTooltip};
} else if (isObject(markTooltip)) {
// `tooltip` is `{fields: 'encodings' | 'fields'}`
if (isSignalRef(markTooltip)) {
return markTooltip;
} else if (markTooltip.content === 'encoding') {
return tooltipRefForEncoding(encoding, stack, config, opt);
} else {
return {signal: datum};
}
}
return undefined;
});
}
}
export function tooltipData(
encoding: Encoding<string>,
stack: StackProperties,
config: Config,
{reactiveGeom}: {reactiveGeom?: boolean} = {}
) {
const formatConfig = {...config, ...config.tooltipFormat};
const toSkip = {};
const expr = reactiveGeom ? 'datum.datum' : 'datum';
const tuples: {channel: Channel; key: string; value: string}[] = [];
function add(fDef: TypedFieldDef<string> | SecondaryFieldDef<string>, channel: Channel) {
const mainChannel = getMainRangeChannel(channel);
const fieldDef: TypedFieldDef<string> = isTypedFieldDef(fDef)
? fDef
: {
...fDef,
type: (encoding[mainChannel] as TypedFieldDef<any>).type // for secondary field def, copy type from main channel
};
const title = fieldDef.title || defaultTitle(fieldDef, formatConfig);
const key = array(title).join(', ').replaceAll(/"/g, '\\"');
let value: string;
if (isXorY(channel)) {
const channel2 = channel === 'x' ? 'x2' : 'y2';
const fieldDef2 = getFieldDef(encoding[channel2]);
if (isBinned(fieldDef.bin) && fieldDef2) {
const startField = vgField(fieldDef, {expr});
const endField = vgField(fieldDef2, {expr});
const {format, formatType} = getFormatMixins(fieldDef);
value = binFormatExpression(startField, endField, format, formatType, formatConfig);
toSkip[channel2] = true;
}
}
if (
(isXorY(channel) || channel === THETA || channel === RADIUS) &&
stack &&
stack.fieldChannel === channel &&
stack.offset === 'normalize'
) {
const {format, formatType} = getFormatMixins(fieldDef);
value = formatSignalRef({
fieldOrDatumDef: fieldDef,
format,
formatType,
expr,
config: formatConfig,
normalizeStack: true
}).signal;
}
value ??= textRef(fieldDef, formatConfig, expr).signal;
tuples.push({channel, key, value});
}
forEach(encoding, (channelDef, channel) => {
if (isFieldDef(channelDef)) {
add(channelDef, channel);
} else if (hasConditionalFieldDef(channelDef)) {
add(channelDef.condition, channel);
}
});
const out = {};
for (const {channel, key, value} of tuples) {
if (!toSkip[channel] && !out[key]) {
out[key] = value;
}
}
return out;
}
export function tooltipRefForEncoding(
encoding: Encoding<string>,
stack: StackProperties,
config: Config,
{reactiveGeom}: {reactiveGeom?: boolean} = {}
) {
const data = tooltipData(encoding, stack, config, {reactiveGeom});
const keyValues = entries(data).map(([key, value]) => `"${key}": ${value}`);
return keyValues.length > 0 ? {signal: `{${keyValues.join(', ')}}`} : undefined;
}