Skip to content

Instantly share code, notes, and snippets.

@Jedius
Forked from mbostock/.block
Last active December 16, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jedius/5404923 to your computer and use it in GitHub Desktop.
Save Jedius/5404923 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<!-- <script type="text/javascript" src="../../d3/d3.min.js"></script> -->
<script src="https://raw.github.com/Jedius/d3/master/d3.min.js"></script>
<script>
var root = {
"name": "flare",
"children": [{
"name": "item0",
"children": [{
"name": "Life of Pi",
"size": 124683,
"number": 0
}
]
}, {
"name": "item1",
"children": [{
"name": "Breaking Dawn",
"size": 86446,
"number": 1
}
]
}, {
"name": "item2",
"children": [{
"name": "Red Dawn",
"size": 10340,
"number": 2
}
]
}, {
"name": "item3",
"children": [{
"name": "Skyfall",
"size": 51961,
"number": 3
}
]
}
]
}
var diameter = 960,
ratio = 1.25,
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("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
var node = svg.selectAll(".node")
.data(bubble.nodes(classes(root))
.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); });
var total = 0
node.each(function (d,i) {
total += d.value;
})
node.each(function (d,i) {
d.percent = Math.floor(d.value/total*100);
})
node.append("circle")
.attr("r", function(d) { return d.r=d.r*ratio; })
.style("fill", function(d) { return color(d.packageName); })
.fill(['black','blue','green','red'])
.strokeColor(['black','blue','green','red'])
.strokeWidth('10px')
.strokeOpacity('0.5')
var label = node.append("g")
.fontSize('11px')
.style("text-anchor", "middle")
.font(['Arial','Helvetica'])
.fontSize(['15px','30px','40px','22px','70px'])
.fill(['red','green','blue','black'])
label.append('text')
.attr("dy", "4px")
.text(function(d) { return d.className.substring(0, d.r / 3); })
.label(['label 1111','label 2222222222222222222222222', 'label 3333333333333', 'label 4444444444444444444444444444444444444'])
label
.popOut([5,4,4,6])
.percent([true,true,true,true])
node
.position([[400,250],[550,700],[200,300],[400,500]])
.overlap([0.7,0.3,0.2,0.9])
// 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");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment