forked from TimeBandit/graph-sub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathramzaneh.html
More file actions
137 lines (113 loc) · 3.62 KB
/
ramzaneh.html
File metadata and controls
137 lines (113 loc) · 3.62 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var buildLinksFromNodeIds = function(nodeIds, links) {
var indexById = _.reduce(nodeIds, function(m, v, i) { m[v] = i; return m; }, {})
return _.map(links, function(v) {
return {source: indexById[v.source], target: indexById[v.target]};
});
};
// Inputs:
// nodes . . [{id: ###, ..}, ..]
// links . . [{target: node.id, ..}, ..]
//
// Output:
// nodes . . <minimal subset of nodes>
// links . . [{source: i, target: j}, ..] (unique links)
var formatGraphForD3 = function(nodes, links) {
links = _.uniq(links);
var linkedNodeIds = _.chain(links)
.map(function(v) { return [v.source, v.target]; })
.flatten()
.uniq()
.value();
links = buildLinksFromNodeIds(linkedNodeIds, links);
var indexById = _.reduce(nodes, function(m, v, i) { m[v.id] = i; return m; }, {});
var linkedNodes = _.map(linkedNodeIds, function(i) {
return nodes[indexById[i]];
});
return { nodes: linkedNodes, links: links };
};
// Build out a D3-ready subgraph
//
// Inputs:
// nodes . . . [{id: ###, ..}, ..]
// links . . . [{target: node.id, ..}, ..]
// rootId . . . root node id
// distance . . number of connections to build out
//
// Output:
// nodes . . <minimal subset of nodes>
// links . . [{source: i, target: j}, ..] (unique links)
var buildSubGraph = function(nodes, links, rootId, distance) {
var sourceLinks = [{source: rootId, target: rootId}];
// A really naive algorithm: we're not yet worrying
// too much about optimising for already-visited links.
var subGraphLinks = [];
for (var i = 0; i < distance; ++i) {
var targetLinks = _.reduce(sourceLinks, function(m, v) {
m = m.concat(_.where(links, {source: v.target}));
return m;
}, []);
subGraphLinks = subGraphLinks.concat(targetLinks);
sourceLinks = targetLinks;
}
return formatGraphForD3(nodes, subGraphLinks);
};
var test = function(n, i){
console.log('Node: ' + JSON.stringify({id: n.id, label: n.label}), i);
};
d3.json("data/ramzaneh.json", function(error, graph) {
var subGraph = buildSubGraph(graph.nodes, graph.edges, '27', 3);
force
.nodes(subGraph.nodes)
.links(subGraph.links)
.start();
var link = svg.selectAll(".link")
.data(subGraph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(subGraph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.on('click', test)
.call(force.drag);
node.append("title")
.text(function(d) { return d.label; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>