-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_v4.html
77 lines (61 loc) · 2.48 KB
/
bubble_v4.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<!-- <script src="https://d3js.org/d3.v5.min.js"></script> -->
<!-- <script src="https://d3js.org/d3.v6.min.js"></script> -->
<!-- <script src="https://d3js.org/d3.v7.min.js"></script> -->
<script>
var diameter = 960,
format = d3.format(",d"),
color = d3.scaleOrdinal(d3.schemeCategory20c);
var bubble = d3.pack()
.size([diameter, diameter])
.padding(1.5);
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
d3.json("data/flare.json", function (error, data) {
if (error) throw error;
var root = d3.hierarchy(classes(data))
.sum(function (d) { return d.value; })
.sort(function (a, b) { return b.value - a.value; });
bubble(root);
var node = svg.selectAll(".node")
.data(root.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.data.className + ": " + format(d.value); });
node.append("circle")
.attr("r", function (d) { return d.r; })
.style("fill", function (d) {
return color(d.data.packageName);
});
node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function (d) { return d.data.className.substring(0, d.r / 3); });
});
// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
console.log(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);
console.log(classes);
return { children: classes };
}
d3.select(self.frameElement).style("height", diameter + "px");
</script>
</body>