Skip to content

Instantly share code, notes, and snippets.

@elipousson
Forked from d3noob/.block
Last active October 28, 2015 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elipousson/58c2ddbfbe695893f460 to your computer and use it in GitHub Desktop.
Save elipousson/58c2ddbfbe695893f460 to your computer and use it in GitHub Desktop.
Property Demolition Decision Tree (Simple vertical d3.js tree diagram)

Property Demolition Decision Tree based on Figure 13.1 from Bringing Buildings Back, Alan Mallach.


This is a simple d3.js tree diagram, arranged so that the tree is vertical, as used as an example in the book D3 Tips and Tricks.

It is derived from the Mike Bostock Collapsible tree example but it is a VERY cut down version without the ability to update (collapse).

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text { font: 12px sans-serif; }
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// ************** Generate the tree diagram *****************
var margin = {top: 40, right: 120, bottom: 20, left: 120},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.x, d.y]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// load the external data
d3.json("property-demolition-decision-tree.json", function(error, treeData) {
root = treeData[0];
update(root);
});
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 100; });
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; });
nodeEnter.append("circle")
.attr("r", 10)
.style("fill", "#fff");
nodeEnter.append("text")
.attr("y", function(d) {
return d.children || d._children ? -18 : 18; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.name; })
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
}
</script>
</body>
</html>
[
{
"name": "City has substantial housing surplus",
"criteria": "Housing supply",
"parent": "null",
"children": [
{
"name": "Obsolete by size or physical character",
"criteria": "Quality of housing",
"children": [
{
"name": "Largely intact",
"criteria": "Neighborhood fabric",
"children": [
{
"name": "Low",
"criteria": "Nuisance Level",
"children": [
{
"name": "Demolish",
"criteria": "Decision"
}
]
},
{
"name": "High",
"criteria": "Nuisance Level",
"children": [
{
"name": "Demolish",
"criteria": "Decision"
}
]
}
]
},
{
"name": "Not intact",
"criteria": "Neighborhood fabric",
"children": [
{
"name": "Low",
"criteria": "Nuisance Level",
"children": [
{
"name": "Probably demolish unless reuse potential exists",
"criteria": "Decision"
}
]
},
{
"name": "High",
"criteria": "Nuisance Level",
"children": [
{
"name": "Demolish",
"criteria": "Decision"
}
]
}
]
},
]
},
{
"name": "Unit of adequate quality and size",
"children": [
{
"name": "Largely intact",
"criteria": "Neighborhood fabric",
"children": [
{
"name": "Low",
"criteria": "Nuisance Level",
"children": [
{
"name": "Preserve if potential reuse exists",
"criteria": "Decision"
}
]
},
{
"name": "High",
"criteria": "Nuisance Level",
"children": [
{
"name": "Probably demolish unless potential reuse is available in the short term",
"criteria": "Decision"
}
]
}
]
},
{
"name": "Not intact",
"criteria": "Neighborhood fabric",
"children": [
{
"name": "Low",
"criteria": "Nuisance Level",
"children": [
{
"name": "Probably demolish unless potential reuse is available in the short term.",
"criteria": "Decision"
}
]
},
{
"name": "High",
"criteria": "Nuisance Level",
"children": [
{
"name": "Demolish",
"criteria": "Decision"
}
]
}
]
},
]
},
{
"name": "High quality or historically or architecturally valuable unit",
"criteria": "Quality of building",
"children": [
{
"name": "Largely intact",
"criteria": "Neighborhood fabric",
"children": [
{
"name": "Low",
"criteria": "Nuisance Level",
"children": [
{
"name": "Preserve if potential reuse exists",
"criteria": "Decision"
}
]
},
{
"name": "High",
"criteria": "Nuisance Level",
"children": [
{
"name": "Probably demolish unless potential reuse is available in the short term",
"criteria": "Decision"
}
]
}
]
},
{
"name": "Not intact",
"criteria": "Neighborhood fabric",
"children": [
{
"name": "Low",
"criteria": "Nuisance Level",
"children": [
{
"name": "Probably demolish unless potential reuse is available in the short term.",
"criteria": "Decision"
}
]
},
{
"name": "High",
"criteria": "Nuisance Level",
"children": [
{
"name": "Demolish",
"criteria": "Decision"
}
]
}
]
},
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment