/
food.js
322 lines (300 loc) · 11.4 KB
/
food.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
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
/*This software is released under the MIT License
MIT License 2014 Denes Csala http://www.csaladen.es
The following software uses the javascript frameworks below,
all of which are distributed under the MIT or GNU/GPL license:
D3.js http://d3js.org/ data-oriented javascript framework.
- Sankey plugin http://bost.ocks.org/mike/sankey/ for D3.js (modified) by Mike Bostock,
which is based on the initial version http://tamc.github.io/Sankey/ by Thomas Counsell.
I have incorporated the ability to render Sankey cycles, as pioneered by https://github.com/cfergus
- Dragdealer.js href="http://skidding.github.io/dragdealer/ by Ovidiu Chereches
*/
//<!--DATA INIT-->
var data={"nodes": [], "links": []}
//<!--DATA ENTRY-->
nodesform=d3.select("#nodes-form");
function addnode() {
var a=nodesform.append("div");
a.text(nodesform[0][0].children.length-1+' ');
a.append("input").attr("value",'{"name":"New Node"}');
}
function removenode() {
nodesform[0][0].children[nodesform[0][0].children.length-1].remove("div")
}
linksform=d3.select("#links-form");
function addlink() {
linksform.append("div").append("input").attr("value",'{"source":0,"target":1,"value":0.52}');
}
function removelink() {
linksform[0][0].children[linksform[0][0].children.length-1].remove("div")
}
function draw() {
data={"nodes": [], "links": []}
for (i = 0; i < nodesform[0][0].children.length; i++) {
data.nodes.push(JSON.parse(nodesform[0][0].children[i].children[0].value));
}
for (i = 0; i < linksform[0][0].children.length; i++) {
data.links.push(JSON.parse(linksform[0][0].children[i].children[0].value));
}
change(data);
}
function save(){
d3.select('#save').style('z-index',100).transition().style('opacity',0.9);
st='{"sankey":{"nodes":['
for (i = 0; i < nodesform[0][0].children.length; i++) {
st=st+nodesform[0][0].children[i].children[0].value+',';
}
st=st.substring(0, st.length - 1)+'],"links":[';
for (i = 0; i < linksform[0][0].children.length; i++) {
st=st+linksform[0][0].children[i].children[0].value+',';
}
st = st.substring(0, st.length - 1)+']},"params":['+densityslider.value.current[0]+','+opacityslider.value.current[0]+','+labelformat+','+labeltextformat+','+showlinkcount+']';
if (document.getElementById("fixedlayout").checked){
var coords=[]
sankey.nodes().forEach(function(d){
coords.push([d.x,d.y])
})
st=st+',"fixedlayout":'+JSON.stringify(coords);
}
st=st+'}';
d3.select('#savetext').text(st);
}
function load(){
d3.select('#load').style('z-index',100).transition().style('opacity',0.9);
}
function loadsubmit(){
d3.select('#load').transition().style('opacity',0).style('z-index',-1);
var rawtext=d3.select('#load')[0][0].children[1].value;
if (rawtext!="") {
//parse data
var rawdata=JSON.parse(rawtext);
if ("sankey" in rawdata) {
var newdata=rawdata.sankey;
}
else {
var newdata=rawdata;
}
var loadtext=JSON.stringify(newdata)
//remove existing node entry boxes
var n=nodesform[0][0].children.length;
for (i = 0; i < n; i++) {
nodesform[0][0].children[0].remove("div");
}
//remove existing link entry boxes
var n=linksform[0][0].children.length;
for (i = 0; i < n; i++) {
linksform[0][0].children[0].remove("div");
}
//add new node entry boxes
var newdata2=JSON.parse(loadtext.substring(loadtext.indexOf('"nodes":[')+8, loadtext.indexOf('"links":[')-1));
for (i = 0; i < newdata2.length; i++) {
var a=nodesform.append("div");
a.text(nodesform[0][0].children.length-1+' ');
a.append("input").attr("value",JSON.stringify(newdata2[i]));
}
//add new link entry boxes
var newdata2=JSON.parse(loadtext.substring(loadtext.indexOf('"links":[')+8, loadtext.length - 1))
for (i = 0; i < newdata2.length; i++) {
linksform.append("div").append("input").attr("value",JSON.stringify(newdata2[i]));
}
//set parameters
if ("fixedlayout" in rawdata) {
fixedlayout=document.getElementById("ignorelayout").checked?[]:rawdata.fixedlayout;
} else {
fixedlayout=[];
}
if ("params" in rawdata) {
labelformat=rawdata.params[2];
labeltextformat=rawdata.params[3];
if (rawdata.params.length>4) showlinkcount=rawdata.params[4];
else showlinkcount=0;
document.getElementById("vlabel").checked=(labelformat==0)?true:false;
document.getElementById("tlabel").checked=(labeltextformat==0)?true:false;
document.getElementById("clabel").checked=(showlinkcount==1)?true:false;
densityslider.setValue(rawdata.params[0]);
opacityslider.setValue(rawdata.params[1]);
}
else {
change(newdata);
}
}
}
//<!--SANKEY DIAGRAM-->
var parallelrendering=false;
var minnodewidth = 50;
var padding = 28;
var labelformat = 0;
var labeltextformat = 0;
var showlinkcount = 0;
var paddingmultiplier = 100;
var lowopacity = 0.3;
var highopacity = 0.7;
var fixedlayout=[];
var format2Number = d3.format(",.2f"),
format1Number = d3.format(",.1f"),
format3Number = d3.format(",.3f"),
formatNumber = d3.format(",.0f"),
format = function(a) {
return formatNumber(a)
},color = d3.scale.category20();
linkformat= function(a) {
if (d3.select("#ldec").node().value==0) return formatNumber(a);
if (d3.select("#ldec").node().value==1) return format1Number(a);
if (d3.select("#ldec").node().value==2) return format2Number(a);
return format3Number(a);
},
nodeformat= function(a) {
if (d3.select("#ndec").node().value==0) return formatNumber(a);
if (d3.select("#ndec").node().value==1) return format1Number(a);
if (d3.select("#ndec").node().value==2) return format2Number(a);
return format3Number(a);
};
d3.select("#ndec")
.on("change",draw);
d3.select("#ldec")
.on("change",draw);
d3.select("#chart").style("width", document.getElementById("chart").offsetWidth - sizecorrection)
d3.select("#titlebar").style("width", document.getElementById("titlebar").offsetWidth - sizecorrection)
var margin = {
top: 70,
right: 10,
bottom: 30,
left: 40
},
width = document.getElementById("chart").offsetWidth - margin.left - margin.right,
height = document.getElementById("chart").offsetHeight - margin.bottom - 90;
var svg = d3.select("#chart").append("svg")
svg.append("rect").attr("x",0).attr("y",0).attr("width","100%").attr("height","100%").attr("fill","white")
svg=svg.attr("width", width + margin.left + margin.right).attr("height", height + margin.top + margin.bottom).append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//set svg background color via rectangle trick
d3.select("#chart").select("svg")
var sankey = d3.sankey().nodeWidth(30).nodePadding(padding).size([width, height]);
var path = sankey.reversibleLink();
var change = function() {};
change = function(d) {
labelformat = document.getElementById("vlabel").checked?0:1;
labeltextformat = document.getElementById("tlabel").checked?0:1;
showlinkcount = document.getElementById("clabel").checked?1:0;
padding = paddingmultiplier * (1 - densityslider.getValue()[0]) + 3
svg.selectAll("g").remove();
sankey = d3.sankey().nodeWidth(30).nodePadding(padding).size([width, height]);
sankey.nodes(d.nodes).links(d.links).layout(500);
var g = svg.append("g") //link
.selectAll(".link").data(d.links).enter().append("g").attr("class", "link").sort(function(j, i) {
return i.dy - j.dy
});
var h = g.append("path") //path0
.attr("d", path(0));
var f = g.append("path") //path1
.attr("d", path(1));
var e = g.append("path") //path2
.attr("d", path(2));
g.attr("fill", function(i) {
if (i.fill) return i.fill;
else if (i.source.fill) return i.source.fill;
else return i.source.color = color(i.source.name.replace(/ .*/, ""))
}).attr("opacity", lowopacity).on("mouseover", function(d) {
d3.select(this).style('opacity', highopacity);
}).on("mouseout", function(d) {
d3.select(this).style('opacity', lowopacity);
}).append("title") //link
.text(function(i) {
return i.source.name + " → " + i.target.name + "\n" + linkformat(i.value)
});
var c = svg.append("g") //node
.selectAll(".node").data(d.nodes).enter().append("g").attr("class", "node").attr("transform", function(i) {
return "translate(" + i.x + "," + i.y + ")"
}).call(d3.behavior.drag().origin(function(i) {
return i
}).on("dragstart", function() {
this.parentNode.appendChild(this)
}).on("drag", b));
c.append("rect") //node
.attr("height", function(i) {
return i.dy
}).attr("width", sankey.nodeWidth()).style("fill", function(i) {
if (i.fill) return i.color = i.fill;
else return i.color = color(i.name.replace(/ .*/, ""))
}).style("stroke", function(i) {
return d3.rgb(i.color).darker(2)
}).on("mouseover", function(d) {
svg.selectAll(".link").filter(function(l) {
return l.source == d || l.target == d;
}).transition().style('opacity', highopacity);
}).on("mouseout", function(d) {
svg.selectAll(".link").filter(function(l) {
return l.source == d || l.target == d;
}).transition().style('opacity', lowopacity);
}).on("dblclick", function(d) {
svg.selectAll(".link").filter(function(l) {
return l.target == d;
}).attr("display", function() {
if (d3.select(this).attr("display") == "none") return "inline"
else return "none"
});
}).append("title").text(function(i) {
return i.name + "\n" + nodeformat(i.value)
});
c.append("text") //node
.attr("x", -6).attr("y", function(i) {
return i.dy / 2
}).attr("dy", ".35em").attr("text-anchor", "end").attr("font-size","16px")
.text(function(i) {
if (labeltextformat<1){
return i.name;
} else {
return "";
}
}).filter(function(i) {
return i.x < width / 2
}).attr("x", 6 + sankey.nodeWidth()).attr("text-anchor", "start")
if (showlinkcount>0) c.append("text") //node
.attr("x", -6).attr("y", function(i) {
return i.dy / 2 + 20
}).attr("dy", ".35em").attr("text-anchor", "end").attr("font-size","16px")
.text(function(i) {
return "→ "+(i.targetLinks.length)+" | "+(i.sourceLinks.length)+" →";
}).filter(function(i) {
return i.x < width / 2
}).attr("x", 6 + sankey.nodeWidth()).attr("text-anchor", "start")
c.append("text") //node
.attr("x", function(i) {return -i.dy / 2})
.attr("y", function(i) {return i.dx / 2 + 9})
.attr("transform", "rotate(270)").attr("text-anchor", "middle").attr("font-size","23px").text(function(i) {
if ((i.dy>minnodewidth)&&(labelformat<1)){
return nodeformat(i.value);
}
}).attr("fill",function(d){
return d3.rgb(d["color"]).brighter(2)
}).attr("stroke",function(d){
return d3.rgb(d["color"]).darker(2)
}).attr("stroke-width","1px");
function b(i) { //dragmove
if (document.getElementById("ymove").checked) {
if (document.getElementById("xmove").checked) {
d3.select(this).attr("transform", "translate(" + (i.x = Math.max(0, Math.min(width - i.dx, d3.event.x))) + "," + (i.y = Math.max(0, Math.min(height - i.dy, d3.event.y))) + ")")
} else {
d3.select(this).attr("transform", "translate(" + i.x + "," + (i.y = Math.max(0, Math.min(height - i.dy, d3.event.y))) + ")")
}
} else {
if (document.getElementById("xmove").checked) {
d3.select(this).attr("transform", "translate(" + (i.x = Math.max(0, Math.min(width - i.dx, d3.event.x))) + "," + i.y + ")")
}
}
sankey.relayout();
f.attr("d", path(1));
h.attr("d", path(0));
e.attr("d", path(2))
};
};
draw();
//<!-- SAVE FUNCTION-->
d3.select("#pngdownloadwrapper")
.on("click",function(){
seturl();
setTimeout(function(){d3.select("#pngdownload").node().click();},500);
})
function seturl(){
exportInlineSVG(d3.select("#chart").select("svg").node(), function(data) {
d3.select("#pngdownload").node().href=data;
});
}