Skip to content

Instantly share code, notes, and snippets.

@tychobrailleur
Last active April 5, 2020 07:17
Show Gist options
  • Save tychobrailleur/13925d16333754da6d5530332946a508 to your computer and use it in GitHub Desktop.
Save tychobrailleur/13925d16333754da6d5530332946a508 to your computer and use it in GitHub Desktop.
Display nodes with d3
<!DOCTYPE html>
<html lang="en">
<head>
<title>Nodes</title>
<meta charset="utf-8" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-selection-multi.v1.js"></script>
<style>
#canvas {
background-color: #fff;
}
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 4px;
}
</style>
</head>
<body>
<svg id="canvas" width="960" height="600"></svg>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
const svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
function color(d) {
switch (d.group) {
case 2: return '#0498ff';
case 3: return '#f7ba00';
default: return '#61d836';
}
}
const simulation = d3.forceSimulation()
.force("link", d3.forceLink().distance(200).id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody().strength(-300))
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("./nodes.json", function(error, graph) {
if (error) throw error;
const link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("marker-end", "url(#arrowhead)")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
const node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(graph.nodes)
.enter().append("g");
const circles = node.append("circle")
.attr("r", 20)
.attr("fill", function(d) { return color(d); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("title")
.text(function(d) { return d.id; });
simulation.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.8).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
{
"nodes": [
{"id": "1", "group": 1},
{"id": "2", "group": 1},
{"id": "3", "group": 1},
{"id": "4", "group": 1},
{"id": "5", "group": 1}
],
"links": [
{"source": "3", "target": "5", "value": "1"},
{"source": "3", "target": "4", "value": "1"},
{"source": "2", "target": "4", "value": "1"},
{"source": "1", "target": "5", "value": "1"},
{"source": "1", "target": "3", "value": "1"}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment