-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordCloud.ts
69 lines (61 loc) · 1.28 KB
/
wordCloud.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
import { deepMix } from '@antv/util';
import { CompositeMarkComponent as CC } from '../runtime';
import { WordCloudMark } from '../spec';
export type WordCloudOptions = Omit<WordCloudMark, 'type'>;
function initializeData(data, encode) {
const { text = 'text', value = 'value' } = encode;
return data.map((d) => ({
...d,
text: d[text],
value: d[value],
}));
}
const GET_DEFAULT_OPTIONS = () => ({
axis: false,
type: 'text',
encode: {
x: 'x',
y: 'y',
text: 'text',
rotate: 'rotate',
fontSize: 'size',
shape: 'tag',
},
scale: {
x: { range: [0, 1] },
y: { range: [0, 1] },
},
style: {
fontFamily: (d) => d.fontFamily,
},
});
export const WordCloud: CC<WordCloudOptions> = async (options, context) => {
const { width, height } = context;
const {
data,
encode = {},
scale,
style = {},
layout = {},
...resOptions
} = options;
const initializedData = initializeData(data, encode);
return deepMix({}, GET_DEFAULT_OPTIONS(), {
data: {
value: initializedData,
transform: [
{
type: 'wordCloud',
size: [width, height],
...layout,
},
],
},
encode,
scale,
style,
...resOptions,
axis: false,
});
};
WordCloud.props = {};