-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
79 lines (65 loc) · 2.24 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
Sheetsee.d3BubbleChart = function(data, options) {
var tree = {name: "data", children: []}
var groups = {}
// data needs to look like this:
// var data = { name: "wahtever", children: [
// { name: "group1", children: [
// { name: 'bob', size: 3},
// { name: 'judy', size: 5}
// ]},
// { name: "group2", children: [
// { name: 'jim', size: 10},
// { name: 'bill', size: 5}
// ]}
// ]}
data.map(function(r) {
var groupName = r[options.group]
groups[groupName] = true
})
Object.keys(groups).map(function(groupName) {
var groupMembers = []
data.map(function(r) {
if (r[options.group] !== groupName) return
groupMembers.push({name: r[options.name], size: r[options.size]})
})
tree.children.push({name: groupName, children: groupMembers})
})
var diameter = options.diameter || 500,
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(options.div).append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
var flattened = classes(tree)
var node = svg.selectAll(".node")
.data(bubble.nodes(flattened)
.filter(function(d) { return !d.children; }))
.enter().append("g")
.attr("class", "node")
.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); });
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.children) node.children.forEach(function(child) { recurse(node.name, child); });
else classes.push({packageName: name, className: node.name, value: node.size});
}
recurse(null, root);
return {children: classes};
}
d3.select(self.frameElement).style("height", diameter + "px");
}