-
-
Save leonsas/3161074 to your computer and use it in GitHub Desktop.
Bubble chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
circle { | |
stroke: #fff; | |
} | |
</style> | |
<body> | |
<button onclick="addtonodes()" text="add"/> | |
<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> | |
<script src="http://d3js.org/d3.v2.js"></script> | |
<script> | |
var nodes= [{"statuses_count": 11, "name": "Leon Sasson", "friends_count": 4, "followers_count": 6, "id": 213899968, "screen_name": "leonsass"}, {"statuses_count": 378, "name": "Jonathan Kohn", "friends_count": 63, "followers_count": 13, "id": 88363150, "screen_name": "jkohn16"}, {"statuses_count": 5748, "name": "Rhett Allain", "friends_count": 368, "followers_count": 2540, "id": 14201924, "screen_name": "rjallain"}, {"statuses_count": 1252, "name": "TEDTalks Updates", "friends_count": 71, "followers_count": 1112719, "id": 15492359, "screen_name": "tedtalks"}, {"statuses_count": 69, "name": "physicsforums", "friends_count": 1, "followers_count": 209, "id": 75086184, "screen_name": "physicsforums"}]; | |
var margin = {top: 0, right: 0, bottom: 0, left: 0}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var n = 20, | |
m = 1, | |
padding = 6; | |
nodes = nodes.map(function(obj) { | |
obj.radius = Math.sqrt(obj.followers_count)/(Math.sqrt(obj.followers_count)*0.01+ 10) + 1; | |
obj.cx=width/2; | |
obj.cy=height/2; | |
return obj; | |
}); | |
var force = d3.layout.force() | |
.nodes(nodes) | |
.size([width, height]) | |
.gravity(0) | |
.charge(0) | |
.on("tick", tick) | |
.start(); | |
var nodes = force.nodes(); | |
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; }) | |
.call(force.drag); | |
circle.append("title").text(function(d){return d.screen_name +": " + d.followers_count;}); | |
function tick(e) { | |
circle | |
.each(gravity(.2 * e.alpha)) | |
.each(collide(.5)) | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
} | |
// Move nodes toward cluster focus. | |
function gravity(alpha) { | |
return function(d) { | |
d.y += (d.cy - d.y) * alpha; | |
d.x += (d.cx - d.x) * alpha; | |
}; | |
} | |
// Resolve collisions between nodes. | |
function collide(alpha) { | |
var quadtree = d3.geom.quadtree(nodes); | |
return function(d) { | |
var r = d.radius + 12 + padding, | |
nx1 = d.x - r, | |
nx2 = d.x + r, | |
ny1 = d.y - r, | |
ny2 = d.y + r; | |
quadtree.visit(function(quad, x1, y1, x2, y2) { | |
if (quad.point && (quad.point !== d)) { | |
var x = d.x - quad.point.x, | |
y = d.y - quad.point.y, | |
l = Math.sqrt(x * x + y * y), | |
r = d.radius + quad.point.radius + (d.color !== quad.point.color) * padding; | |
if (l < r) { | |
l = (l - r) / l * alpha; | |
d.x -= x *= l; | |
d.y -= y *= l; | |
quad.point.x += x; | |
quad.point.y += y; | |
} | |
} | |
return x1 > nx2 | |
|| x2 < nx1 | |
|| y1 > ny2 | |
|| y2 < ny1; | |
}); | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment