-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (111 loc) · 3.73 KB
/
index.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
var d3 = require('d3')
var d3sankey = require('./sankey')
function exportData(graph) {
var txNodes = graph.getAllNodes()
var nodes = txNodes.map(function(n) {
return { name: n.id }
})
var links = txNodes.reduce(function(memo, n, i) {
n.nextNodes.forEach(function(next, j) {
if(next) {
memo.push({ source: i, target: txNodes.indexOf(next), value: 1 })
}
})
return memo
}, [])
return { nodes: nodes, links: links }
}
// Adapted from: http://bost.ocks.org/mike/sankey/
function visualizeWithData(data, containerSelector, margin) {
var margin = margin || {top: 1, right: 1, bottom: 6, left: 1}
var width = 960 - margin.left - margin.right
var height = 500 - margin.top - margin.bottom
var color = d3.scale.category20()
var svg = d3.select(containerSelector).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 + ")");
var sankey = d3sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
var path = sankey.link();
sankey
.nodes(data.nodes)
.links(data.links)
.layout(32, width);
var link = svg.append("g").selectAll(".link")
.data(data.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; });
link.append("title")
.text(function(d) { return d.source.name + " → " + d.target.name });
var node = svg.append("g").selectAll(".node")
.data(data.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(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));
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function(d) { return d.color = color(d.name.replace(/ .*/, "")); })
.style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) { return d.name; });
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.substr(0, 4); })
.filter(function(d) { return d.x < width / 2; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// css
svg.selectAll('.node rect')
.style({
cursor: "move",
"fill-opacity": .9,
"shape-rendering": "crispEdges"
})
svg.selectAll('.node text')
.style({
"pointer-events": "none",
"text-shadow": "0 1px 0 #fff"
})
svg.selectAll('.link')
.style({
fill: "none",
stroke: "#000",
"stroke-opacity": .2
})
.on('mouseover', function(){
d3.select(this).style({"stroke-opacity": .5});
})
.on('mouseout', function(){
d3.select(this).style({"stroke-opacity": .2});
})
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);
}
}
function visualize(txGraph, containerSelector, margin) {
var data = exportData(txGraph)
visualizeWithData(data, containerSelector, margin)
}
module.exports = {
exportData: exportData,
visualizeWithData: visualizeWithData,
visualize: visualize
}