-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaybeTitle.ts
50 lines (47 loc) · 1.29 KB
/
maybeTitle.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
import { deepMix } from '@antv/util';
import { isUnset } from '../utils/helper';
import { TransformComponent as TC } from '../runtime';
import { dynamicFormatDateTime } from '../utils/dateFormat';
import { columnOf } from './utils/helper';
export type MaybeTitleOptions = {
channel?: string;
};
/**
* Infer title channel from x-position channel.
*/
export const MaybeTitle: TC<MaybeTitleOptions> = (options = {}) => {
const { channel = 'x' } = options;
return (I, mark) => {
const { encode } = mark;
const { tooltip } = mark;
if (isUnset(tooltip)) return [I, mark];
const { title } = tooltip;
if (title !== undefined) return [I, mark];
const titles = Object.keys(encode)
.filter((key) => key.startsWith(channel))
.filter((key) => !encode[key].inferred)
.map((key) => columnOf(encode, key))
.filter(([T]) => T)
.map((d) => d[0]);
if (titles.length === 0) return [I, mark];
const T = [];
for (const i of I) {
T[i] = {
value: titles
.map((t) =>
t[i] instanceof Date ? dynamicFormatDateTime(t[i] as Date) : t[i],
)
.join(', '),
};
}
return [
I,
deepMix({}, mark, {
tooltip: {
title: T,
},
}),
];
};
};
MaybeTitle.props = {};