PieBubbles
by Avinashullal
HTML
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<!-- Original from From http://bl.ocks.org/d/1747543/ -->
<script src="http://mbostock.github.com/d3/d3.js?2.7.4"></script>
<script src="http://mbostock.github.com/d3/d3.geom.js?2.7.4"></script>
<script src="http://mbostock.github.com/d3/d3.layout.js?2.7.4"></script>
</body>
</html>
CSS
circle {
stroke: #fff;
}
JavaScript
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var n = 200,
m = 4,
padding = 6,
radius = d3.scale.sqrt().range([0, 12]),
color = d3.scale.category10().domain(d3.range(m));
var nodes = d3.range(n).map(function() {
var i = Math.floor(Math.random() * m),
v = (i + 1) / m * -Math.log(Math.random());
return {
radius: radius(v),
color: color(i),
name: "Theme " + i
};
});
var force = d3.layout.force()
.nodes(nodes)
.size([width, height])
.gravity(.02)
.charge(0)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
/*
var circle = svg.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", function(d) { return d.radius; })
.style("fill", function(d) { return d.color; })
*/
var circle = svg.selectAll("circle")
.data(nodes)
.enter()
.append("a")
.attr("class", "bubble-node")
.attr("xlink:href", function(d) { return 'Javascript:alert(\"' + d.name+ '\")'; })
.append("circle")
.attr("r", function(d) { return d.radius; })
.style("fill", function(d) { return d.color; })
.text(function(d) { return d.name; })
.style("font-size", "24px")
.style("font-color", "#fff")
.attr("dy", ".3em")
.call(force.drag)
function tick(e) {
circle
.each(cluster(10 * e.alpha * e.alpha))
.each(collide(.5))
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
// Move d to be adjacent to the cluster node.
function cluster(alpha) {
var max = {};
// Find the largest node for each cluster.
nodes.forEach(function(d) {
if (!(d.color in max) || (d.radius >...