-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsankey.ts
151 lines (137 loc) · 3.31 KB
/
sankey.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
import { deepMix } from '@antv/util';
import { CompositeMarkComponent as CC } from '../runtime';
import { SankeyMark } from '../spec';
import { Sankey as SankeyTransform } from '../data/sankey';
import { subObject } from '../utils/helper';
import { subTooltip, maybeAnimation } from '../utils/mark';
import { field, initializeData } from './utils';
const DEFAULT_LAYOUT_OPTIONS = {
nodeId: (d) => d.key,
nodeWidth: 0.02,
nodePadding: 0.02,
};
const DEFAULT_NODE_OPTIONS = {
type: 'polygon',
axis: false,
legend: false,
encode: {
shape: 'polygon',
x: 'x',
y: 'y',
},
scale: {
x: { type: 'identity' },
y: { type: 'identity' },
},
style: {
stroke: '#000',
},
};
const DEFAULT_LINK_OPTIONS = {
type: 'polygon',
axis: false,
legend: false,
encode: {
shape: 'ribbon',
x: 'x',
y: 'y',
},
style: {
fillOpacity: 0.5,
stroke: undefined,
},
};
const DEFAULT_LABEL_OPTIONS = {
textAlign: (d) => (d.x[0] < 0.5 ? 'start' : 'end'),
position: (d) => (d.x[0] < 0.5 ? 'right' : 'left'),
fontSize: 10,
};
export type SankeyOptions = Omit<SankeyMark, 'type'>;
/**
* @todo Add interaction
* @todo Add source-link color mode
*/
export const Sankey: CC<SankeyOptions> = (options) => {
const {
data,
encode = {},
scale,
style = {},
layout = {},
nodeLabels = [],
linkLabels = [],
animate = {},
tooltip = {},
interaction,
} = options;
// Initialize data, generating nodes by link if is not specified.
const { links, nodes } = initializeData(data, encode);
// Extract encode for node and link.
const nodeEncode = subObject(encode, 'node');
const linkEncode = subObject(encode, 'link');
const { key: nodeKey = (d) => d.key, color = nodeKey } = nodeEncode;
// Transform data, using nodeKey as nodeId.
const { links: linkData, nodes: nodeData } = SankeyTransform({
...DEFAULT_LAYOUT_OPTIONS,
nodeId: field(nodeKey),
...layout,
})({ links, nodes });
// Extract label style and apply defaults.
const {
text = nodeKey,
spacing = 5,
...labelStyle
} = subObject(style, 'label');
const key1 = field(nodeKey);
const nodeTooltip = subTooltip(
tooltip,
'node',
{
title: key1,
items: [{ field: 'value' }],
},
true,
);
const linkTooltip = subTooltip(tooltip, 'link', {
title: '',
items: [
(d) => ({ name: 'source', value: key1(d.source) }),
(d) => ({ name: 'target', value: key1(d.target) }),
],
});
return [
deepMix({}, DEFAULT_NODE_OPTIONS, {
data: nodeData,
encode: { ...nodeEncode, color },
scale,
style: subObject(style, 'node'),
labels: [
{
...DEFAULT_LABEL_OPTIONS,
text,
dx: (d) => (d.x[0] < 0.5 ? spacing : -spacing),
...labelStyle,
},
...nodeLabels,
],
tooltip: nodeTooltip,
animate: maybeAnimation(animate, 'node'),
axis: false,
interaction,
}),
deepMix({}, DEFAULT_LINK_OPTIONS, {
data: linkData,
encode: linkEncode,
labels: linkLabels,
style: {
fill: linkEncode.color ? undefined : '#aaa',
lineWidth: 0,
...subObject(style, 'link'),
},
tooltip: linkTooltip,
animate: maybeAnimation(animate, 'link'),
interaction,
}),
];
};
Sankey.props = {};