-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.js
69 lines (58 loc) · 2.24 KB
/
bubble.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
// Wrap these scripts in an anonymous function to maintain local scope
(function() {
var diameter = 960,
format = d3.format(",d"),
color = d3.scale.category20c();
var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(1.5);
var svg = d3.select("#bubble").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
var tooltip = d3.select("#bubble")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.attr("class", "tooltip")
.text("a simple tooltip");
d3.json("tags.json", function(error, root) {
var node = svg.selectAll(".node")
.data(bubble.nodes(classes(root))
.filter(function(d) { return !d.children; }))
.enter().append("g")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("title")
.text(function(d) { return d.className + ": " + format(d.value); });
node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return color(d.packageName); })
.on("mouseover", function(d){return tooltip.style("visibility", "visible").text(d.notes);})
.on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px");})
.on("mouseout", function(){return tooltip.style("visibility", "hidden");});
node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function(d) { return d.className.substring(0, d.r / 3); });
});
// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
var classes = [];
function recurse(name, node) {
if (node.tags)
node.tags.forEach(function(child) { recurse(node.name, child); });
else {
// If there are notes, use those, otherwise have the notes be the name
classes.push({packageName: node.popularity, className: node.name, value: node.series_count, notes: node.notes});
if (classes[classes.length - 1].notes == "") {
classes[classes.length - 1].notes = node.name;
}
}
}
recurse(null, root);
return {children: classes};
}
d3.select(self.frameElement).style("height", diameter + "px");
})();