Created
November 21, 2012 19:55
-
-
Save seliopou/4127259 to your computer and use it in GitHub Desktop.
d3.js example | circle packing in a rectangle
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> | |
<html> | |
<head> | |
<title>d3.js example | circle packing in a rectangle</title> | |
<script src="http://d3js.org/d3.v2.js"></script> | |
<style type="text/css"> | |
</style> | |
</head> | |
<body> | |
<div id="canvas"></div> | |
<script type="text/javascript"> | |
var w = 640, | |
h = 480; | |
var data = { | |
name : "root", | |
children : [ | |
{ name: '1', size: 100 }, | |
{ name: '2', size: 85 }, | |
{ name: '3', size: 70 }, | |
{ name: '4', size: 55 }, | |
{ name: '5', size: 40 }, | |
{ name: '6', size: 25 }, | |
{ name: '7', size: 10 }, | |
] | |
} | |
var canvas = d3.select("#canvas") | |
.append("svg:svg") | |
.attr('width', w) | |
.attr('height', h); | |
var nodes = d3.layout.pack() | |
.value(function(d) { return d.size; }) | |
.size([w, h]) | |
.nodes(data); | |
// Get rid of root node | |
nodes.shift(); | |
canvas.selectAll('circles') | |
.data(nodes) | |
.enter().append('svg:circle') | |
.attr('cx', function(d) { return d.x; }) | |
.attr('cy', function(d) { return d.y; }) | |
.attr('r', function(d) { return d.r; }) | |
.attr('fill', 'white') | |
.attr('stroke', 'grey'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I couldn't make it work :(