forked from barryclark/jekyll-now
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.js
More file actions
141 lines (108 loc) · 3.89 KB
/
graph.js
File metadata and controls
141 lines (108 loc) · 3.89 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
var width = window.screen.availWidth,
height = window.screen.availHeight,
root;
var force = d3.layout.force()
.size([width, height])
.on("tick", tick);
var svg = d3.select("div#example").append("svg")
.attr("id", "graph")
.attr("viewBox", "0 0 " + width + " " + height )
.attr("preserveAspectRatio", "xMidYMid meet");
// .attr("width", width)
// .attr("height", height)
var link = svg.selectAll(".link"),
node = svg.selectAll(".node").append("g");
// I want to make the node object a group.
// This will have a circle and icon
// this doesn't work. In future, look here http://stackoverflow.com/questions/16265123/resize-svg-when-window-is-resized-in-d3-js
function updateWindow(){
width = w.innerWidth || e.clientWidth || g.clientWidth;
height = w.innerHeight|| e.clientHeight|| g.clientHeight;
svg.attr("width", width).attr("height", height);
}
window.onresize = updateWindow;
d3.json("/flare.json", function(json) {
root = json;
update();
});
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.linkDistance(150)
.friction(0.4)
.charge(-1500)
.start();
// Update the links…
link = link.data(links, function(d) { return d.target.id; });
// Exit any old links.
link.exit().remove();
// Enter any new links.
link.enter().insert("line", ".node")
.attr("class", "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; });
// Update the nodes…
node = node.data(nodes, function(d) { return d.id; }).style("fill", color);
// Exit any old nodes.
node.exit().remove();
// Enter any new nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.on("mouseover", function(d) { d3.select(this).style("opacity", "0.9").select('.hover').style("visibility", "visible"); })
.on("mouseout", function(d) { d3.select(this).style("opacity", "1").select('.hover').style("visibility", "hidden"); })
.on("click", click)
.call(force.drag);
nodeEnter.append("circle") // append icon instead?
.attr("r", function(d) { return Math.sqrt(d.size) / 9 || 7; }) //
nodeEnter.append("text")
.attr('text-anchor', 'middle')
.attr('dominant-baseline', 'central')
.attr('font-family', 'FontAwesome')
.attr('font-size', function(d) { return Math.sqrt(d.size) / 8 || 6.5;})
.text(function(d) { return d.icon})
.style('fill', '#fff');
//.attr("dy", ".35em"
//.text(function(d) { return d.name; });
nodeEnter.append("text")
.attr('class','hover')
.attr('baseline-shift','200%')
.text(function(d) { return d.name})
.style("visibility","hidden");
node.select("circle")
.style("fill", function(d) { return d.color; })
}
function tick() {
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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
//UNUSED FUNCTION
// Color leaf nodes orange, and packages white or blue.
function color(d) {
//return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c";
}
// Toggle children on click.
function click(d) {
if (!d3.event.defaultPrevented) {
window.location.href = d.url;
}
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) node.children.forEach(recurse);
if (!node.id) node.id = ++i;
nodes.push(node);
}
recurse(root);
return nodes;
}