-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.ts
108 lines (98 loc) · 2.26 KB
/
tree.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
import { deepMix } from '@antv/util';
import { subObject } from '../utils/helper';
import { CompositeMarkComponent as CC } from '../runtime';
import { TreeMark } from '../spec';
import {
Tree as TreeTransform,
TreeOptions as TreeTransformOptions,
} from '../data/tree';
import { maybeAnimation, subTooltip } from '../utils/mark';
const DEFAULT_LAYOUT_OPTIONS: TreeTransformOptions = {
sortBy: (a, b) => b.value - a.value,
};
const DEFAULT_NODE_OPTIONS = {
axis: false,
legend: false,
type: 'point',
encode: {
x: 'x',
y: 'y',
size: 2,
shape: 'point',
},
};
const DEFAULT_LINK_OPTIONS = {
type: 'link',
encode: {
x: 'x',
y: 'y',
shape: 'smooth',
},
};
const DEFAULT_LABEL_OPTIONS = {
text: '',
fontSize: 10,
};
export type TreeOptions = Omit<TreeMark, 'type'>;
export const Tree: CC<TreeOptions> = (options) => {
const {
data,
encode = {},
scale = {},
style = {},
layout = {},
nodeLabels = [],
linkLabels = [],
animate = {},
tooltip = {},
} = options;
const valueEncode = encode?.value;
const { nodes, edges } = TreeTransform({
...DEFAULT_LAYOUT_OPTIONS,
...layout,
field: valueEncode,
})(data);
const nodeTooltip = subTooltip(
tooltip,
'node',
{
title: 'name',
items: ['value'],
},
true,
);
const linkTooltip = subTooltip(tooltip, 'link', {
title: '',
items: [
(d) => ({ name: 'source', value: d.source.name }),
(d) => ({ name: 'target', value: d.target.name }),
],
});
return [
deepMix({}, DEFAULT_LINK_OPTIONS, {
data: edges,
encode: subObject(encode, 'link'),
scale: subObject(scale, 'link'),
labels: linkLabels,
style: { stroke: '#999', ...subObject(style, 'link') },
tooltip: linkTooltip,
animate: maybeAnimation(animate, 'link'),
}),
deepMix({}, DEFAULT_NODE_OPTIONS, {
data: nodes,
scale: subObject(scale, 'node'),
encode: subObject(encode, 'node'),
labels: [
{
...DEFAULT_LABEL_OPTIONS,
...subObject(style, 'label'),
},
...nodeLabels,
],
style: { ...subObject(style, 'node') },
tooltip: nodeTooltip,
animate: maybeAnimation(animate, 'node'),
}),
];
};
Tree.props = {};