Skip to content

Instantly share code, notes, and snippets.

@ralexrdz
Created October 1, 2019 01:34
Show Gist options
  • Save ralexrdz/3f8a2c0f77c75678256ddb7c456dc490 to your computer and use it in GitHub Desktop.
Save ralexrdz/3f8a2c0f77c75678256ddb7c456dc490 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
cursor: pointer;
}
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node text {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
//original code see fiddle http://jsfiddle.net/Nivaldo/CbGh2/
var pubs = {
name: "Tracker Project",
children: [{
name: "Bioregions"
},
{
name: "Water",
children: [{
name: "Water in agriculture",
children: [{
name: "Systems for Watering",
children: [{
name: "Drip systems"
},
{
name: "Inundation"
},
{
name: "Sprinklers"
},
{
name: "Gravity"
},
{
name: "Test"
}
]
},
{
name: "Microbes"
},
{
name: "Drought remediation",
children: [{
name: "Reservoirs"
}]
},
{
name: "Water tables"
},
{
name: "Structured water"
},
{
name: "Drought"
},
{
name: "Transport"
},
{
name: "Water harvesting"
},
{
name: "Flood"
},
{
name: "Toilets"
},
{
name: "Hygiene"
},
{
name: "Potable"
},
{
name: "Cleaning"
},
{
name: "Pollution"
}
]
},
{
name: "Seas",
children: [{
name: "Nuclear radiation"
},
{
name: "Plastic contamination"
},
{
name: "Sea life",
children: [{
name: "Fishing"
},
{
name: "Species dieoff"
},
]
},
{
name: "Oil contamination"
},
{
name: "Acidification"
},
{
name: "Glacier melt",
children: [{
name: "Methane release"
},
{
name: "Rising sea levels"
}
]
}
]
},
{
name: "Lakes",
children: [{
name: "Pollution"
},
{
name: "Water levels"
},
{
name: "Underwater life"
}
]
},
{
name: "Rivers",
children: [{
name: "Dams",
children: [{
name: "Ecosystem destruction"
}]
},
{
name: "Contamination"
},
{
name: "Life in river"
},
]
},
{
name: "Pollution",
children: [
{
name: "Untreated sewage"
},
{
name: "Fertilizer"
},
{
name: "Pesticides"
},
{
name: "Animal waste"
},
{
name: "Salts from evaporated irrigation water"
},
{
name: "Silt from deforestation"
},
{
name: "Agricultural runoff"
},
{
name: "Industry",
children: [{
name: "Toxic chemicals and heavy metals"
},
{
name: "Power plant emissions",
children: [{
name: "Acid rain"
}]
}
]
}
]
},
{
name: "Water Problems",
children: [{
name: "Shortages of water",
children: [{
name: "Drought"
}]
},
{
name: "Shortages of clean water",
children: [{
name: "Growing populations "
},
{
name: "Expanding agriculture "
},
{
name: "Industrialization "
},
{
name: "High living standards "
}
]
}
]
},
{
name: "Wetlands"
},
{
name: "Groundwater",
children: [{
name: "Megacities"
},
{
name: "Agriculture"
},
{
name: "Fertilizer runoff"
},
{
name: "Pollution",
children: [{
name: "Water quality"
},
{
name: "Health"
}
]
}
]
},
{
name: "Water Disputes"
},
{
name: "Water Wastes",
children: [{
name: "Bad drainage"
},
{
name: "Leaking pipes"
},
{
name: "Evaporation from reservoirs and canals"
},
{
name: "Poor irrigation"
},
{
name: "Industrial practices"
},
{
name: "Drinking water treatment"
}
]
},
{
name: "Plastic bottles and Distribution"
},
{
name: "Water Shortages",
children: [
{
name: "Water tables declining"
},
{
name: "Drilling"
},
{
name: "Well building"
},
{
name: "Rich countries",
children: [{
name: "Building dams"
},
{
name: "Importing food"
},
{
name: "Tapping deep water aquifers"
},
{
name: "Recycling wastewater"
},
{
name: "Desalinated seawater"
},
{
name: "Overpopulation"
}
]
}
]
},
{
name: "Solutions",
children: [{
name: "Ultraviolet radiation"
},
{
name: "Filtering through cloth"
},
{
name: "Reusing and recycling water"
}
]
},
{
name: "Water Conservation",
children: [{
name: "Transporting and storing rain water"
},
{
name: "Catchments"
},
{
name: "Gutters and other channels"
},
{
name: "Storage tanks"
},
{
name: "Gravity or pump driven delivery system"
}
]
},
{
name: "Water Extracting Methods"
}
]
},
{
name: "Air"
},
{
name: "Earth (soil)"
},
{
name: "Government Projects"
},
{
name: "City Projects"
},
{
name: "Food"
},
{
name: "Agriculture"
},
{
name: "Environmental Technologies"
},
{
name: "Construction"
},
{
name: "Sustainability Communities"
},
{
name: "Transportation"
},
{
name: "Energy"
},
{
name: "Problem & Solutions"
},
{
name: "Indigenous"
},
{
name: "Legal"
},
{
name: "Waste"
},
{
name: "Economy"
},
{
name: "Land Use"
},
{
name: "Species Dieoff"
}
]
}
var diameter = 800;
var margin = {top: 20, right: 120, bottom: 20, left: 120},
width = diameter,
height = diameter;
var i = 0,
duration = 350,
root;
var tree = d3.layout.tree()
.size([360, diameter / 2 - 80])
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 10) / a.depth; });
var diagonal = d3.svg.diagonal.radial()
.projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });
var svg = d3.select("body").append("svg")
.attr("width", width )
.attr("height", height )
.append("g")
.attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
root = pubs;
root.x0 = height / 2;
root.y0 = 0;
root.children.forEach(collapse); // start with all children collapsed
update(root);
d3.select(self.frameElement).style("height", "800px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 80; });
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
//.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
.on("click", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeEnter.append("text")
.attr("x", 10)
.attr("dy", ".35em")
.attr("text-anchor", "start")
//.attr("transform", function(d) { return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length * 8.5) + ")"; })
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1)
.attr("transform", function(d) { return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length + 50) + ")"; });
// TODO: appropriate transform
var nodeExit = node.exit().transition()
.duration(duration)
//.attr("transform", function(d) { return "diagonal(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
// Collapse nodes
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment