-
Notifications
You must be signed in to change notification settings - Fork 0
/
sankey.html
389 lines (289 loc) · 10.8 KB
/
sankey.html
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE html>
<meta charset="utf-8">
<title>Olin Sankey</title>
<style>
.node rect
{
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text
{
pointer-events: none;
/*text-shadow: 0 1px 0 #fff;*/
font-size: 10pt;
font-family: sans-serif;
/*-webkit-transform: rotate(-30deg)*/
}
.link
{
fill: none;
stroke: #000;
stroke-opacity: .2;
}
/*,ENGGRD,1?2?3?4?5?6?&?8?9?.?1?2?3?4?5?6?7?8?9?0?,001002*/
.link:hover
{
stroke-opacity: .5;
}
/*<script type="javascript">
var file_name = prompt("Target data file, i.e.:\n[SCHOOL][Women/Men]ENG.json",
"lol.json");
</script>*/
</style>
<header type="text/javascript">
<p id="head">Olin Sankey Chart.<br>
Please select the file you wish to visualize.</p>
</header>
<body>
<p id="chart"></p>
<script type="text/javascript" src="d3/d3.js"></script>
<script src="sankey.js"></script>
<script src="sankey_supplement.js"></script>
<script>
console.log("here we go");
var file_name = prompt("Target data file, i.e.:\n[year]_all, olin, men, women", "olin");
// OS-apprpriate path to the file...
var sep = "/";
if (navigator.appVersion.indexOf("Win")!=-1) // are we on windows?
{
sep = "\\"; // you're special, you know that, right?
}
file_name = "." + sep + "output" + sep + "sankey" + sep + file_name;
var units = "students";
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width = 1190,//window.innerWidth * 0.95 - margin.left - margin.right,
legend_pad = 150;
height = 700;//(window.innerHeight - 50) * 0.95 - margin.top - margin.bottom;
group_pad = 15;
var formatNumber = d3.format(",.0f"), // zero decimal places
format = function(d) { return formatNumber(d) + " " + units; },
color = d3.scale.category20();
// append the svg canvas to the page
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right + legend_pad)
.attr("height", height + margin.top + margin.bottom) //////
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Set the sankey diagram properties
var sankey = d3.sankey()
.nodeWidth(7)
.nodePadding(10)
.size([width, height / 4]); /////
var path = sankey.link();
// load the data
console.log("loading that file...");
d3.json(file_name + ".json", function(error, graph)
{
sankey
.nodes(graph.nodes)
.links(graph.links);
console.log('nodes and links exist. time to lay them out...');
sankey.layout(1);
var header = graph.header;
document.getElementById("head").innerHTML = header;
// figure out where the nodes need to go...
// find the max sizes of the nodes
var maxes = {'ECE':0, 'ME ':0, 'E ':0, 'U ':0, 'LOA':0};
for (n in graph.nodes)
{
node = graph.nodes[n];
maxes = find_max(maxes, node);
}
// find the max terms of the groups
var mt = {'ECE':0, 'ME ':0, 'E ':0, 'U ':0, 'LOA':0};
for (n in graph.nodes)
{
node = graph.nodes[n];
mt = find_max_terms(mt, node);
}
// console.log(mt);
// "fix" the existing layout
var sort_order = ['ME ', 'ECE', 'E ', 'U ', 'LOA'];
for (n in graph.nodes)
{
graph.nodes[n].y = auto_y(graph.nodes[n], maxes, sort_order, group_pad);
// graph.nodes[n].dy = graph.nodes[n].value; // pixel-perfect
}
sankey.relayout();
// <<<<<<< HEAD
// add in the links
var link = svg.append("g").selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) { return b.dy - a.dy; });
// add the link titles
link.append("title")
.text(function(d) {
return d.source.name + " → " +
d.target.name + "\n" + format(d.value); });
// add in the nodes
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
// console.log("d", d); ///////
return "translate(" + d.x + "," + d.y + ")"; })
.call(
d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() {
this.parentNode.appendChild(this);
})
.on("drag", dragmove));
// add the rectangles for the nodes
node.append("rect")
.attr("height", function(d) { return d.dy; }) ///////
.attr("width", sankey.nodeWidth())
.style("fill", function(d)
{
return d.color = choose_color(d.name);//color(d.name.slice(3).replace(/ .*/, ""));
})
.style("stroke", function(d) {
return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) {
return d.name + "\n" + format(d.value); });
// // add in the title for the nodes
// 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; })
// .filter(function(d) { return d.x < width / 2; })
// .attr("x", 6 + sankey.nodeWidth())
// .attr("text-anchor", "start");
// Set the sankey diagram properties ///////////
sankey
.size([width + legend_pad, height]);
// add in the legend. repurpose the "text" field from before
node.append("text")
.attr("x", function(d){return calculate_legend_x(d, sankey.nodeWidth(), width)})// 6 + sankey.nodeWidth())
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "start") // is "end" for the other one
.attr("transform", null)
.text(function(d) { return write_legend(d, mt)});
// sankey.layout(1);
// the function for moving the nodes
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();
}
// =======
// add in the links
var link = svg.append("g").selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) { return b.dy - a.dy; });
// add the link titles
link.append("title")
.text(function(d) {
return d.source.name + " → " +
d.target.name + "\n" + format(d.value); });
// add in the nodes
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
// console.log("d", d); ///////
return "translate(" + d.x + "," + d.y + ")"; })
.call(
d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() {
this.parentNode.appendChild(this);
})
.on("drag", dragmove));
// add the rectangles for the nodes
node.append("rect")
.attr("height", function(d) { return d.dy; }) ///////
.attr("width", sankey.nodeWidth())
.style("fill", function(d)
{
return d.color = choose_color(d.name);
})
.style("stroke", function(d) {
return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) {
return d.name + "\n" + format(d.value); });
// // add in the title for the nodes
// 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; })
// .filter(function(d) { return d.x < width / 2; })
// .attr("x", 6 + sankey.nodeWidth())
// .attr("text-anchor", "start");
// Set the sankey diagram properties ///////////
sankey
.size([width + legend_pad, height]);
// add in the legend. repurpose the "text" field from before
node.append("text")
.attr("x", function(d){return calculate_legend_x(d, sankey.nodeWidth(), width)})// 6 + sankey.nodeWidth())
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "start") // is "end" for the other one
.attr("transform", null)
.text(function(d) { return write_legend(d, mt)});
// sankey.layout(1);
// the function for moving the nodes
function dragmove(d)
{
d3.select(this).attr("transform",
"translate(" + d.x + "," + (
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
// >>>>>>> playing with everything
) + ")");
sankey.relayout();
link.attr("d", path);
}
//var window_name = file_name;//'MIDFIELD Sankey chart: '.concat(file_name)
window.document.title = file_name;//window_name
// window.onresize = function(event) {
// var width = window.innerWidth * 0.95 - margin.left - margin.right,
// height = (window.innerHeight - 50) * 0.95 - margin.top - margin.bottom;
// console.log('resized!')
// var svg = d3.select("#chart");
// svg.attr("width", width + margin.left + margin.right)
// svg.attr("height", height + margin.top + margin.bottom) //////
// svg.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// sankey
// .size([width, height / 2]);
// sankey.layout(1);
// sankey
// .size([width, height]);
// }
// function on_resize() {
// var width = window.innerWidth * 0.95 - margin.left - margin.right,
// height = (window.innerHeight - 50) * 0.95 - margin.top - margin.bottom;
// console.log('resized')
// sankey
// .size([width, height]);
// }
// function on_resize(c,t){onresize=function(){clearTimeout(t);t=setTimeout(c,100)};return c};
}
);
// these parenthesis does the trick
</script>
</body>
</html>