-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathsankeyNetwork.js
executable file
·250 lines (216 loc) · 8.73 KB
/
sankeyNetwork.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
HTMLWidgets.widget({
name: "sankeyNetwork",
type: "output",
initialize: function(el, width, height) {
d3.select(el).append("svg")
.style("width", "100%")
.style("height", "100%");
return {
sankey: d3.sankey(),
x: null
};
},
resize: function(el, width, height, instance) {
/* handle resizing now through the viewBox
d3.select(el).select("svg")
.attr("width", width)
.attr("height", height + height * 0.05);
this.renderValue(el, instance.x, instance);
*/
// with flexdashboard and slides
// sankey might be hidden so height and width 0
// in this instance re-render on resize
if( d3.min(instance.sankey.size()) <= 0 ) {
this.renderValue(el, instance.x, instance);
}
},
renderValue: function(el, x, instance) {
// save the x in our instance (for calling back from resize)
instance.x = x;
// alias sankey and options
var sankey = instance.sankey;
var options = x.options;
// convert links and nodes data frames to d3 friendly format
var links = HTMLWidgets.dataframeToD3(x.links);
var nodes = HTMLWidgets.dataframeToD3(x.nodes);
// margin handling
// set our default margin to be 20
// will override with x.options.margin if provided
var margin = {top: 20, right: 20, bottom: 20, left: 20};
// go through each key of x.options.margin
// use this value if provided from the R side
Object.keys(x.options.margin).map(function(ky){
if(x.options.margin[ky] !== null) {
margin[ky] = x.options.margin[ky];
}
// set the margin on the svg with css style
// commenting this out since not correct
// s.style(["margin",ky].join("-"), margin[ky]);
});
// get the width and height
var width = el.getBoundingClientRect().width - margin.right - margin.left;
var height = el.getBoundingClientRect().height - margin.top - margin.bottom;
var color = eval(options.colourScale);
var color_node = function color_node(d){
if (d.group){
return color(d.group.replace(/ .*/, ""));
} else {
return "#cccccc";
}
}
var color_link = function color_link(d){
if (d.group){
return color(d.group.replace(/ .*/, ""));
} else {
return "#000000";
}
}
var opacity_link = function opacity_link(d){
if (d.group){
return 0.7;
} else {
return 0.2;
}
}
var formatNumber = d3.format(",.0f"),
format = function(d) {
if (typeof d === "string") return d;
return formatNumber(d);
}
// create d3 sankey layout
sankey
.nodes(nodes)
.links(links)
.size([width, height])
.nodeWidth(options.nodeWidth)
.nodePadding(options.nodePadding)
.sinksRight(options.sinksRight)
.layout(options.iterations);
// select the svg element and remove existing children
d3.select(el).select("svg").selectAll("*").remove();
// remove any previously set viewBox attribute
d3.select(el).select("svg").attr("viewBox", null);
// append g for our container to transform by margin
var svg = d3.select(el).select("svg").append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// draw path
var path = sankey.link();
// draw links
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.style("fill", "none")
.style("stroke", color_link)
.style("stroke-opacity", opacity_link)
.sort(function(a, b) { return b.dy - a.dy; })
.on("mouseover", function(d) {
d3.select(this)
.style("stroke-opacity", function(d){return opacity_link(d) + 0.3});
})
.on("mouseout", function(d) {
d3.select(this)
.style("stroke-opacity", opacity_link);
});
// add backwards class to cycles
link.classed('backwards', function (d) { return d.target.x < d.source.x; });
svg.selectAll(".link.backwards")
.style("stroke-dasharray","9,1")
.style("stroke","#402")
// draw nodes
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" +
d.x + "," + d.y + ")"; })
.call(d3.drag()
.subject(function(d) { return d; })
.on("start", function() { this.parentNode.appendChild(this); })
.on("drag", dragmove))
.on("mouseover", function(d) {
link.filter(function(d1, i) { return d.targetLinks.includes(d1) | d.sourceLinks.includes(d1); })
.style("stroke-opacity", function(d){return opacity_link(d) + 0.3});
})
.on("mouseout", function(d) {
link.filter(function(d1, i) { return d.targetLinks.includes(d1) | d.sourceLinks.includes(d1); })
.style("stroke-opacity", opacity_link);
});
// note: u2192 is right-arrow
link.append("title")
.append("foreignObject")
.append("xhtml:body")
.html(function(d) { return "<pre>" + d.source.name + " \u2192 " + d.target.name +
"\n" + format(d.value) + " " + options.units + "</pre>"; });
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function(d) {
return d.color = color_node(d); })
.style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
.style("opacity", 0.9)
.style("cursor", "move")
.append("title")
.append("foreignObject")
.append("xhtml:body")
.html(function(d) { return "<pre>" + d.name + "<br>" + format(d.value) +
" " + options.units + "</pre>"; });
node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(function(d) { return d.name; })
.style("font-size", options.fontSize + "px")
.style("font-family", options.fontFamily ? options.fontFamily : "inherit")
.filter(function(d) { return d.x < width / 2 || !options.sinksRight; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// adjust viewBox to fit the bounds of our tree
var s = d3.select(svg.node().parentNode);
s.attr(
"viewBox",
[
d3.min(
s.selectAll('g').nodes().map(function(d){
return d.getBoundingClientRect().left
})
) - s.node().getBoundingClientRect().left - margin.right,
d3.min(
s.selectAll('g').nodes().map(function(d){
return d.getBoundingClientRect().top
})
) - s.node().getBoundingClientRect().top - margin.top,
d3.max(
s.selectAll('g').nodes().map(function(d){
return d.getBoundingClientRect().right
})
) -
d3.min(
s.selectAll('g').nodes().map(function(d){
return d.getBoundingClientRect().left
})
) + margin.left + margin.right,
d3.max(
s.selectAll('g').nodes().map(function(d){
return d.getBoundingClientRect().bottom
})
) -
d3.min(
s.selectAll('g').nodes().map(function(d){
return d.getBoundingClientRect().top
})
) + margin.top + margin.bottom
].join(",")
);
function dragmove(d) {
d3.select(this).attr("transform", "translate(" + d.x + "," +
(d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))) + ")");
sankey.relayout();
link.attr("d", path);
}
},
});