Skip to content

Instantly share code, notes, and snippets.

@d3noob
Last active November 30, 2019 18:36
Show Gist options
  • Save d3noob/db249428022f0a761f6d to your computer and use it in GitHub Desktop.
Save d3noob/db249428022f0a761f6d to your computer and use it in GitHub Desktop.
Tree Demo for woei
license: mit

hello world

[{
"net_address": "10.0.0.0",
"net_masklength": 16,
"description": "CompanyX B.V.",
"type": "supernet",
"children": [
{
"net_address": "10.0.1.0",
"net_masklength": 24,
"type": "supernet",
"url": "http://localhost/10.0.1.0-24",
"description": "Internal",
"children": [
{
"net_address": "10.0.1.64",
"net_masklength": 26,
"url": "http://localhost/10.0.1.64-26",
"type": "subnet",
"description": "VOIP",
"hosts": [
{
"ip": "10.0.1.70",
"fqdn": "test.CompanyX.nl",
"url": "http://localhost/host/test.CompanyX.nl"
}
]
},
{
"net_address": "10.0.1.128",
"net_masklength": 26,
"url": "http://localhost/10.0.1.128-26",
"type": "subnet",
"description": "Clients",
"hosts": [
{
"ip": "10.0.1.150",
"fqdn": "test2.CompanyX.nl",
"url": "http://localhost/host/test2.CompanyX.nl"
}
]
}
]
},
{
"net_address": "10.0.100.0",
"net_masklength": 24,
"url": "http://localhost/10.0.100.0-24",
"type": "supernet",
"description": "Datacenter",
"children": [
{
"net_address": "10.0.100.64",
"net_masklength": 26,
"url": "http://localhost/10.0.100.64-26",
"type": "subnet",
"description": "Cloud",
"hosts": [
{
"ip": "10.0.100.70",
"fqdn": "test3.CompanyX.nl",
"url": "http://localhost/host/test3.CompanyX.nl"
}
]
},
{
"net_address": "10.0.100.128",
"net_masklength": 26,
"url": "http://localhost/10.0.100.128-26",
"type": "subnet",
"description": "Grid",
"hosts": [
{
"ip": "10.0.100.150",
"fqdn": "test4.CompanyX.nl",
"url": "http://localhost/host/test4.CompanyX.nl"
}
]
}
]
},
{
"net_address": "10.0.200.0",
"net_masklength": 24,
"url": "http://localhost/10.0.200.0-24",
"type": "supernet",
"description": "LAB",
"children": [
{
"net_address": "10.0.200.64",
"net_masklength": 26,
"url": "http://localhost/10.0.200.64-26",
"type": "subnet",
"description": "test",
"hosts": [
{
"ip": "10.0.200.70",
"fqdn": "test5.CompanyX.nl",
"url": "http://localhost/host/test5.CompanyX.nl"
}
]
},
{
"net_address": "10.0.200.128",
"net_masklength": 26,
"url": "http://localhost/10.0.200.128-26",
"type": "subnet",
"description": "equipment",
"hosts": [
{
"ip": "10.0.200.150",
"fqdn": "test6.CompanyX.nl",
"url": "http://localhost/host/test6.CompanyX.nl"
}
]
}
]
}
]
},
{
"net_address": "10.20.0.0",
"net_masklength": 16,
"description": "CompanyX B.V.",
"type": "supernet",
"children": [
{
"net_address": "10.20.200.0",
"net_masklength": 24,
"url": "http://localhost/10.20.200.0-24",
"type": "supernet",
"description": "LAB"
}
]
}]
<!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="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script>
// ************** Generate the tree diagram *****************
var margin = {top: 20, 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.y, d.x]; });
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("data.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 * 180; });
// 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.y + "," + d.x + ")"; });
nodeEnter.append("circle")
.attr("r", 10)
.style("fill", "#fff");
nodeEnter.append("text")
.attr("x", function(d) {
return d.children || d._children ? -13 : 13; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.net_address; })
.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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment