Display and interact with collapsible tree structure
HTML
<html>
<head>
<style>
/* HTML styles */
html{ width: 100%; }
body{
width: 100%;
margin: 0; padding: 0;
display: flex;
font-family: sans-serif; font-size: 75%; }
.controls {
flex-basis: 200px;
padding: 0 5px;
}
.controls .force {
background-color:#eee;
border-radius: 3px;
padding: 5px;
margin: 5px 0;
}
.controls .force p label { margin-right: .5em; font-size: 120%; font-weight: bold;}
.controls .force p { margin-top: 0;}
.controls .force label { display: inline-block; }
.controls input[type="checkbox"] { transform: scale(1.2, 1.2); }
.controls input[type="range"] { margin: 0 5% 0.5em 5%; width: 90%; }
/* alpha viewer */
.controls .alpha p { margin-bottom: .25em; }
.controls .alpha .alpha_bar { height: .5em; border: 1px #777 solid; border-radius: 2px; padding: 1px; display: flex; }
.controls .alpha .alpha_bar #alpha_value { background-color: #555; border-radius: 1px; flex-basis: 100% }
.controls .alpha .alpha_bar:hover { border-width: 2px; margin:-1px; }
.controls .alpha .alpha_bar:active #alpha_value { background-color: #222 }
/* SVG styles */
svg {
flex-basis: 100%;
min-width: 200px;
}
.links line {
stroke: #aaa;
}
.nodes circle {
pointer-events: all;
}
</style>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<div class="controls">
<div class="force alpha">
<p><label>alpha</label> Simulation activity</p>
<div class="alpha_bar" onclick="updateAll();"><div id="alpha_value"></div></div>
</div>
<div class="force">
<p><label>center</label> Shifts the view, so the graph is centered at this location.</p>
<label>
x
<output id="center_XSliderOutput">.5</output>
<input type="range" min="0" max="1" value=".5" step="0.01" oninput="d3.select('#center_XSliderOutput').text(value); forceProperties.center.x=value; updateAll();">
</label>
<label>
y
<output id="center_YSliderOutput">.5</output>
<input type="range" min="0" max="1" value=".5" step="0.01"...
JavaScript
var svg = d3.select("svg"),
width = +svg.node().getBoundingClientRect().width,
height = +svg.node().getBoundingClientRect().height
// values for all forces
const forceProperties = {
center: {
x: 0.5,
y: 0.5,
},
charge: {
enabled: true,
strength: -30,
distanceMin: 1,
distanceMax: 2000,
},
collide: {
enabled: true,
strength: 0.7,
iterations: 1,
radius: 5,
},
forceX: {
enabled: false,
strength: 0.1,
x: 0.5,
},
forceY: {
enabled: false,
strength: 0.1,
y: 0.5,
},
link: {
enabled: true,
distance: 30,
iterations: 1,
},
}
// svg objects
var link, node
// the data - an object with nodes and links
var graph = {
nodes: [
{ id: "Myriel", group: 1 },
{ id: "Napoleon", group: 1 },
{ id: "Mlle.Baptistine", group: 1 },
{ id: "Mme.Magloire", group: 1 },
{ id: "CountessdeLo", group: 1 },
{ id: "Geborand", group: 1 },
{ id: "Champtercier", group: 1 },
{ id: "Cravatte", group: 1 },
{ id: "Count", group: 1 },
{ id: "OldMan", group: 1 },
{ id: "Labarre", group: 2 },
{ id: "Valjean", group: 2 },
{ id: "Marguerite", group: 3 },
{ id: "Mme.deR", group: 2 },
{ id: "Isabeau", group: 2 },
{ id: "Gervais", group: 2 },
{ id: "Tholomyes", group: 3 },
{ id: "Listolier", group: 3 },
{ id: "Fameuil", group: 3 },
{ id: "Blacheville", group: 3 },
{ id: "Favourite", group: 3 },
{ id: "Dahlia", group: 3 },
{ id: "Zephine", group: 3 },
{ id: "Fantine", group: 3 },
{ id: "Mme.Thenardier", group: 4 },
{ id: "Thenardier", group: 4 },
{ id: "Cosette", group: 5 },
{ id: "Javert", group: 4 },
{ id: "Fauchelevent", group: 0 },
{ id: "Bamatabois", group: 2 },
{ id: "Perpetue", group: 3 },
{ id: "Simplice", group: 2 },
{ id: "Scaufflaire", group: 2 },
{ id: "Woman1", group: 2 },
{ id: "Judge", group: 2 },
{ id: "Champmathieu", group: 2 },
{ id: "Brevet",...