Skip to content

Instantly share code, notes, and snippets.

@erikhazzard
Created September 7, 2013 06:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erikhazzard/6473231 to your computer and use it in GitHub Desktop.
Save erikhazzard/6473231 to your computer and use it in GitHub Desktop.
d3 tree by @prcweb
{"description":"d3 tree by @prcweb","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"inline-console":false,"thumbnail":"http://i.imgur.com/iCbcFE6.png"}
/* D3 Tree http://prcweb.co.uk/lab/d3-tree/ */
/* Copyright 2013 Peter Cook (@prcweb); Licensed MIT */
// Tree configuration
var branches = [];
var seed = {i: 0, x: 420, y: 600, a: 0, l: 130, d:0}; // a = angle, l = length, d = depth
var da = 0.5; // Angle delta
var dl = 0.8; // Length delta (factor)
var ar = 0.7; // Randomness
var maxDepth = 10; //lower this for faster rendering
var stroke = "#777777";
// Tree creation functions
function branch(b) {
var end = endPt(b), daR, newB;
branches.push(b);
if (b.d === maxDepth)
return;
// Left branch
daR = ar * Math.random() - ar * 0.5;
newB = {
i: branches.length,
x: end.x,
y: end.y,
a: b.a - da + daR,
l: b.l * dl,
d: b.d + 1,
parent: b.i
};
branch(newB);
// Right branch
daR = ar * Math.random() - ar * 0.5;
newB = {
i: branches.length,
x: end.x,
y: end.y,
a: b.a + da + daR,
l: b.l * dl,
d: b.d + 1,
parent: b.i
};
branch(newB);
}
function regenerate(initialise) {
branches = [];
branch(seed);
initialise ? create() : update();
}
function endPt(b) {
// Return endpoint of branch
var x = b.x + b.l * Math.sin( b.a );
var y = b.y - b.l * Math.cos( b.a );
return {x: x, y: y};
}
// D3 functions
function x1(d) {return d.x;}
function y1(d) {return d.y;}
function x2(d) {return endPt(d).x;}
function y2(d) {return endPt(d).y;}
function highlightParents(d) {
var colour = d3.event.type === 'mouseover' ? 'green' : '#777';
var depth = d.d;
for(var i = 0; i <= depth; i++) {
d3.select('#id-'+parseInt(d.i)).style('stroke', colour);
d = branches[d.parent];
}
}
function create() {
d3.select('svg')
.selectAll('line')
.data(branches)
.enter()
.append('line')
.attr('x1', x1)
.attr('y1', y1)
.attr('x2', x2)
.attr('y2', y2)
.style("stroke", stroke)
.style('stroke-width', function(d) {return parseInt(maxDepth + 1 - d.d) + 'px';})
.attr('id', function(d) {return 'id-'+d.i;})
.on('mouseover', highlightParents)
.on('mouseout', highlightParents);
}
function update() {
d3.select('svg')
.selectAll('line')
.data(branches)
.transition()
.attr('x1', x1)
.attr('y1', y1)
.attr('x2', x2)
.attr('y2', y2)
.style("stroke", stroke)
}
d3.selectAll('.regenerate')
.on('click', regenerate);
regenerate(true);
path, line {
stroke: #777;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment