Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created May 16, 2013 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/5592180 to your computer and use it in GitHub Desktop.
Save tmcw/5592180 to your computer and use it in GitHub Desktop.
var d3 = require('d3'),
fs = require('fs');
var width = 4000,
height = 4000;
var bubbles = JSON.parse(fs.readFileSync('bubbles.json'));
var treemap = d3.layout.treemap()
.size([width, height])
.sort(function(a, b) {
return a.value - b.value;
}).mode('squarify');
var color = d3.scale.category20c();
var data = {
value:0,
children: bubbles.map(function(r) {
return { value: r };
})
};
var map = treemap.nodes(data);
// fs.writeFileSync('treed', JSON.stringify(tree));
var Canvas = require('canvas'),
canvas = new Canvas(width, height),
ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, width, height);
function position(d, i) {
if (d.children) return;
ctx.fillStyle = (d.children) ? '#fff' : color(i);
ctx.fillRect(~~d.x, ~~d.y,
~~Math.max(0, d.dx),
~~Math.max(0, d.dy));
}
map.forEach(position);
fs.writeFileSync('tree.png', canvas.toBuffer());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment