-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsankey.js
More file actions
executable file
·209 lines (188 loc) · 4.1 KB
/
sankey.js
File metadata and controls
executable file
·209 lines (188 loc) · 4.1 KB
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
var units = "Widgets";
var defaultNodeHeight = 15;
// set the dimensions and margins of the graph
var margin = { top: 0, right: 0, bottom: 0, left: 0 },
height = 300;
// format variables
var formatNumber = d3.format(",.0f"), // zero decimal places
format = function(d) {
return formatNumber(d) + " " + units;
};
const neurotransmitters = {
OTHR : "#121212",
GLUT : "#80828a",
GABA : "#873333",
SERO : "#8c5d5d",
DOPA : "#4f4a6b",
ACET : "#6c7356",
NORE : "#a35a00",
}
function colorLink(s){
switch(s){
case "other" || "" || undefined:
return neurotransmitters.OTHR;
break;
case "glutamate":
return neurotransmitters.GLUT;
break;
case "gaba":
return neurotransmitters.GABA;
break;
case "serotonin":
return neurotransmitters.SERO;
break;
case "dopamine":
return neurotransmitters.DOPA;
break;
case "acetylcholine":
return neurotransmitters.ACET;
break;
case "norepinephrine":
return neurotransmitters.NORE;
break;
}
}
//set the colors for the legend
$("#legend li ").each(function(){
let s = $(this).text().toLowerCase();
this.style.color = colorLink(s);
})
// append the svg object to the body of the page
var svg2 = d3
.select("#sankeyChart")
.append("svg")
.attr("id", "sankey")
.attr("width", width)
.attr("height", height + 20);
// Set the sankey diagram properties
var sankey = d3
.sankey()
.nodeWidth(5)
.nodePadding(40)
.size([width, height]);
var path = sankey.link();
// load the data
function loadSankey(graph) {
svg2.selectAll("g").remove();
//uncomment to add filters
const defs = svg2.append('defs')
.append("pattern")
.attr("id","diagonals")
.attr("width", "8")
.attr("height","8")
.attr("patternUnits","userSpaceOnUse")
.attr("patternTransform", "rotate(45)")
.append("rect")
.attr("width","7")
.attr("height","10")
.attr("transform","translate(0,0)")
.attr("fill","white");
console.log(graph);
sankey
.nodes(graph.nodes)
.links(graph.links)
.layout(32);
// add in the links
var link = svg2
.append("g")
.selectAll(".link")
.data(graph.links)
.enter()
.append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke", d => d.color)
.style("stroke-dasharray", d => d.dashed?"3 1":"")
.style("stroke-width", d => Math.max(1, d.dy))
.sort(function(a, b) {
return d3.descending(a.dy, b.dy);
});
// add the link titles
link.append("title").text(function(d) {
return d.source.name + " → " + d.target.name;
});
// add in the nodes
var node = svg2
.append("g")
.selectAll(".node")
.data(graph.nodes)
.enter()
.append("g")
.attr("class", "node")
.attr("transform", function(d) {
if (d.value == 0) {
d.x = 0.0;
d.dy = defaultNodeHeight;
if (graph.links.length == 0) {
d.dy = height / graph.nodes.length;
d.y = d.y * (height / graph.nodes.length);
}
}
return "translate(" + d.x + "," + d.y + ")";
})
.call(
d3
.drag()
.subject(function(d) {
return d;
})
.on("start", function() {
this.parentNode.appendChild(this);
})
.on("drag", dragmove)
);
// add the rectangles for the nodes
node
.append("rect")
.attr("height", function(d) {
return Math.max(1, d.dy);
})
.attr("width", sankey.nodeWidth())
.style("fill", function(d) {
colorwheel++;
d.color = colors(colorwheel);
return (d.color);
})
.style("stroke-width", 0)
.append("title")
.text(function(d) {
return d.name + "\n" + format(d.value);
});
// add in the title for the nodes
nodeText = 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;
})
.on("contextmenu", function(d) {
contextmenu(d);
})
.filter(function(d) {
return d.x < width / 2;
})
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start")
.style("fill", function(d) {
return d.value > 0 ? "black" : "white";
});
// 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();
link.attr("d", path);
}
}