Built with blockbuilder.org
xxxxxxxxxx
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>indented collapsable tree</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/d3@12.10.3/d3.min.js"></script>
<style type="text/css">
.node rect {
cursor: pointer;
fill: #fff;
fill-opacity: .5;
stroke: #3182bd;
stroke-width: 1.5px;
}
.node text {
font: 10px sans-serif;
pointer-events: none;
}
path.link {
fill: none;
stroke: #ff0000;
stroke-width: 1.5px;
</style>
</head>
<body>
<div style="overflow:hidden; font-size=11px;">
<input id="searchField" type="text" name="fname" style="float:left;">
<input type="button" value="search" onclick="search()" style="float:left; margin-left:5px;">
<div id="searching" style="float:left; margin-left:20px; margin-top:3px; color:#c9c9b5;width:200px;"></div>
<div id="clearResult" class="clearResult" onclick="clearResult()">Clear</div>
<header>
</header>
<div id="chart"></div>
<script type="text/javascript">
var w = 700,
h = 551000,
i = 0,
barHeight = 20,
barWidth = w * .8,
duration = 400,
root;
var tree = d3.layout.tree()
.size([h, 100]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(20,30)");
var nodes;
var selected = new Array();
var selection;
var scope = new Array();
var filtered = false;
function moveChildren(node) {
if(node.children) {
node.children.forEach(function(c) { moveChildren(c); });
node._children = node.children;
node.children = null;
}
}
d3.json("flare.json", function(json) {
json.x0 = 0;
json.y0 = 0;
moveChildren(json);
update(root = json);
});
function collapse(d, index) {
d.parent = this;
d.marker = this.marker + generateMarker(index);
if (d.children) {
d._children = d.children;
d._children.forEach(collapse, d);
d.children = null;
}
}
function update(source) {
// Compute the flattened node list. TODO use d3.layout.hierarchy.
var nodes = tree.nodes(root);
// Compute the "layout".
nodes.forEach(function(n, i) {
n.x = i * barHeight;
});
// Update the nodes…
var node = vis.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
var nodeEnter = node.enter().append("svg:g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.style("opacity", 1e-6);
// Enter any new nodes at the parent's previous position.
nodeEnter.append("svg:rect")
.attr("y", -barHeight / 2)
.attr("height", barHeight)
.attr("width", barWidth)
.style("fill", color)
.on("click", click);
nodeEnter.append("svg:text")
.attr("dy", 3.5)
.attr("dx", 5.5)
.text(function(d) { return d.name; });
// Transition nodes to their new position.
nodeEnter.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
.style("opacity", 1);
node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
.style("opacity", 1)
.select("rect")
.style("fill", color);
// Transition exiting nodes to the parent's new position.
node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.style("opacity", 1e-6)
.remove();
// Update the links…
var link = vis.selectAll("path.link")
.data(tree.links(nodes), function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("svg:path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
})
.transition()
.duration(duration)
.attr("d", diagonal);
// 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);
}
function color(d) {
return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c";
}
function search () {
var text = document.getElementById('searchField').value.toUpperCase();
document.getElementById('searching').innerHTML = 'Searching: ' +text;
//reset
selected = [];
scope = [];
selection = '#a';
vis.selectAll(".nodeText").attr("fill", "black").style("fill-opacity", 0.3);
vis.selectAll("path").attr("r", 4.5).style("fill", function(d) {
return d._children ? "lightsteelblue" : "#fff";
});
filtered = true;
root.children.forEach(searchChildren, text);
vis.selectAll(selected).attr("fill", "orange");
vis.selectAll(scope).style("fill-opacity", 1.0);
document.getElementById('searching').innerHTML = 'Found: ' +selected.length;
}
function clearResult() {
filtered = false;
selected = [];
scope = [];
selection = '#a';
root.children.forEach(clearNodes);
vis.selectAll(".nodeText").attr("fill", "black");
vis.selectAll(".nodeText").style("fill-opacity", 1);
vis.selectAll("path").attr("r", 4.5).style("fill", function(d) {
return d._children ? "lightsteelblue" : "#fff";
});
document.getElementById('searching').innerHTML = '';
}
function clearNodes(d) {
d.selected = 0;
if (d.children)
d.children.forEach(clearNodes);
else if (d._children)
d._children.forEach(clearNodes);
}
function searchChildren(d) {
d.selected = 0;
var patt = new RegExp("\\b"+this+"\\b", "i");
//if (d.name.toUpperCase().indexOf(this) >= 0) {
if (d.name.match(patt) != null) {
selected.push('#' + d.marker);
selection = selection + ', #' + d.marker;
d.selected = 1;
addToScope(d);
}
if (d.children)
d.children.forEach(searchChildren, this);
else if (d._children)
d._children.forEach(searchChildren, this);
}
function addToScope(d) {
var marker = '#' + d.marker;
if (scope.indexOf(marker) >= 0) return;
scope.push(marker);
if (d.parent == null) return;
addToScope(d.parent);
}
function generateMarker(index) {
if (index > 9)
return '' + index;
return '0'+index;
}
</script>
</body>
</html>
Modified http://mbostock.github.com/d3/d3.min.js to a secure url
https://mbostock.github.com/d3/d3.min.js