-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaybeTooltip.ts
38 lines (35 loc) · 1.23 KB
/
maybeTooltip.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
import { deepMix } from '@antv/util';
import { isUnset } from '../utils/helper';
import { TransformComponent as TC } from '../runtime';
export type MaybeTooltipOptions = {
channel: string | string[];
};
/**
* Infer tooltip channel from specified channel.
*/
export const MaybeTooltip: TC<MaybeTooltipOptions> = (options) => {
const { channel } = options;
return (I, mark) => {
const { encode, tooltip } = mark;
if (isUnset(tooltip)) return [I, mark];
const { items = [] } = tooltip;
if (!items || items.length > 0) return [I, mark];
const channels = Array.isArray(channel) ? channel : [channel];
const newItems = channels.flatMap((channel) =>
Object.keys(encode)
.filter((key) => key.startsWith(channel))
.map((key) => {
const { field, value, inferred = false, aggregate } = encode[key];
if (inferred) return null;
// Do not show inferred column.
if (aggregate && value) return { channel: key };
if (field) return { field };
if (value) return { channel: key };
return null;
})
.filter((d) => d !== null),
);
return [I, deepMix({}, mark, { tooltip: { items: newItems } })];
};
};
MaybeTooltip.props = {};