-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
262 lines (214 loc) · 7.85 KB
/
index.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
<!DOCTYPE html>
<meta charset="utf-8">
<title>SANKEY Experiment</title>
<style>
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
fill: #000;
font: 14px sans-serif;
pointer-events: none;
}
text {
fill: #000;
font: 14px sans-serif;
pointer-events: none;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .1;
}
.link:hover {
stroke-opacity: .5;
}
</style>
<body>
<p id="chart">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="https://cdn.rawgit.com/d3/d3-plugins/master/sankey/sankey.js"></script>
<script>
var units = "Widgets";
var margin = {top: 50, right: 150, bottom: 10, left: 10},
width = 1200 - margin.left - margin.right,
height = 740 - margin.top - margin.bottom;
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)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
/* svg.append("text")
.attr("transform", "translate(" + 0 + "," + -20 + ")")
.attr("text-anchor", "start")
.style("fill", "black")
.text("Driver");
svg.append("text")
.attr("transform", "translate(" + 280 + "," + -20 + ")")
.attr("text-anchor", "start")
.style("fill", "black")
.text("CWMT");
svg.append("text")
.attr("transform", "translate(" + 520 + "," + -20 + ")")
.attr("text-anchor", "start")
.style("fill", "black")
.text("Primary Process");
svg.append("text")
.attr("transform", "translate(" + 780 + "," + -20 + ")")
.attr("text-anchor", "start")
.style("fill", "black")
.text("Secondary Process");
svg.append("text")
.attr("transform", "translate(" + 1020 + "," + -20 + ")")
.attr("text-anchor", "start")
.style("fill", "black")
.text("Ecosystem Service");*/
// Set the sankey diagram properties
var sankey = d3.sankey()
.nodeWidth(36)
.nodePadding(20)
.size([width, height]);
var path = sankey.link();
// load the data
//d3.json("https://dl.dropboxusercontent.com/u/38371278/HelenFig/Fig2.json", function(error, graph) {
//d3.json("WetlandTraitDriver.json", function(error, graph) {
d3.json("https://raw.githubusercontent.com/zahachtah/WetlandTraitDriver/master/WetlandTraitDriver.json", function(error, graph) {
console.log(graph)
function highlight_node_links(node,i){
var remainingNodes=[],
nextNodes=[];
var stroke_opacity = 0;
if( d3.select(this).attr("data-clicked") == "1" ){
d3.select(this).attr("data-clicked","0");
stroke_opacity = 0.2;
}else{
d3.select(this).attr("data-clicked","1");
stroke_opacity = 0.7;
}
var traverse = [{
linkType : "sourceLinks",
nodeType : "target"
},{
linkType : "targetLinks",
nodeType : "source"
}];
traverse.forEach(function(step){
node[step.linkType].forEach(function(link) {
remainingNodes.push(link[step.nodeType]);
highlight_link(link.id, stroke_opacity);
});
while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
node[step.linkType].forEach(function(link) {
nextNodes.push(link[step.nodeType]);
highlight_link(link.id, stroke_opacity);
});
});
remainingNodes = nextNodes;
}
});
}
function highlight_link(id,opacity){
d3.select("#link-"+id).style("stroke-opacity", opacity);
}
var nodeMap = {};
graph.nodes.forEach(function(x) { nodeMap[x.name] = x; });
graph.nodes.forEach(function(x) { nodeMap[x.description] = x; });
graph.links = graph.links.map(function(x) {
return {
source: nodeMap[x.source],
target: nodeMap[x.target],
value: x.value,//1,//
sign: x.sign,
description: x.description,
certainty: x.certainty,
reference: x.reference
};
});
sankey
.nodes(graph.nodes)
.links(graph.links)
.layout(32);
// add in the links
var link = svg.append("g").selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.attr("id", function(d,i){
d.id = i;
return "link-"+i;
})
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.style("stroke",function(d){if (d.sign=="positive"){return "#007";} else if (d.sign=="undetermined"){return "#444";} else {return "#700"}})
.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 \n"+ "Interaction: " + d.sign +
"\n \n"+ "Certainty: " + d.certainty +
"\n \n" + d.description + "\n \n" + d.reference; });
//d.target.name + "\n" + format(d.value); });
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click",highlight_node_links)
.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 = color(d.colorID.replace(/ .*/, "")); })
.style("stroke", function(d) {
return d3.rgb(d.color).darker(2); })
// return d.color = colorM(d.name.replace(/ .*/, "")); })
.append("title")
.text(function(d) {
return d.name + "\n \n" + d.description; });
//return d.name + "\n" + format(d.value); });
// add in the title for the nodes
node.append("text")
.attr("x", 6 + sankey.nodeWidth())//.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "start")//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");
// the function for moving the nodes
function dragmove(d) {
d3.select(this).attr("transform",
"translate(" + (
d.x = d.x
) + "," + (
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", path);
function highlight_link(id,opacity){
d3.select("#link-"+id).style("stroke-opacity", opacity);
}
}
});
</script>
<p>
Fig. S1 (Supporting information, Moor et al. 2017). Interactive summary graph of (nodes, left to right) regional and local drivers (blue), plant traits (orange), ecosystem properties and processes (green) and ecosystem services (red) in three wetland types (bogs, fens and marshes), as well as suggested relationships between them (links). Red links are negative relationships, blue links are positive, and grey links are context dependent. Link thickness indicates the strength of the relationship (in 3 qualitative levels: weak, intermediate, strong). Holding the mouse over a node or link will display a description, direction of the interaction (positive, negative, undetermined), estimated degree of scientific consensus about the interaction, explanation and selected bibliographic references. Double click on a node to highlight its dependencies. Click again on the node to cancel the dependencies of that node.
</body>
</html>