Skip to content

Instantly share code, notes, and snippets.

@EfratVil
Last active September 16, 2023 02:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EfratVil/58b872b4f15a358c3a9822f5a285c5be to your computer and use it in GitHub Desktop.
Save EfratVil/58b872b4f15a358c3a9822f5a285c5be to your computer and use it in GitHub Desktop.
V4 simple network graph
{
"nodes": [
{
"id": 1,
"name": "A"
},
{
"id": 2,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 4,
"name": "D"
},
{
"id": 5,
"name": "E"
},
{
"id": 6,
"name": "F"
},
{
"id": 7,
"name": "G"
},
{
"id": 8,
"name": "H"
},
{
"id": 9,
"name": "I"
},
{
"id": 10,
"name": "J"
}
],
"links": [
{
"source_id": 1,
"target_id": 2
},
{
"source_id": 1,
"target_id": 5
},
{
"source_id": 1,
"target_id": 6
},
{
"source_id": 2,
"target_id": 3
},
{
"source_id": 2,
"target_id": 7
}
,
{
"source_id": 3,
"target_id": 4
},
{
"source_id": 8,
"target_id": 3
}
,
{
"source_id": 4,
"target_id": 5
}
,
{
"source_id": 4,
"target_id": 9
},
{
"source_id": 5,
"target_id": 10
}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {font-family: 'Architects Daughter', cursive;}
</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Architects+Daughter" rel="stylesheet">
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody().strength(-400))
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("data.json", function(error, graph) {
if (error) throw error;
graph.links.forEach(function(d){
d.source = d.source_id;
d.target = d.target_id;
});
var link = svg.append("g")
.style("stroke", "#aaa")
.selectAll("line")
.data(graph.links)
.enter().append("line");
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", 6)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var label = svg.append("g")
.attr("class", "labels")
.selectAll("text")
.data(graph.nodes)
.enter().append("text")
.attr("class", "label")
.text(function(d) { return d.name; });
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("r", 20)
.style("fill", "#d9d9d9")
.style("stroke", "#969696")
.style("stroke-width", "1px")
.attr("cx", function (d) { return d.x+6; })
.attr("cy", function(d) { return d.y-6; });
label
.attr("x", function(d) { return d.x; })
.attr("y", function (d) { return d.y; })
.style("font-size", "20px").style("fill", "#4393c3");
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart()
simulation.fix(d);
}
function dragged(d) {
simulation.fix(d, d3.event.x, d3.event.y);
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
simulation.unfix(d);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment