Network graph with node labels
started with this example
forked from EfratVil's block: V4 simple network graph
| license: mit |
Network graph with node labels
started with this example
forked from EfratVil's block: V4 simple network graph
| { | |
| "nodes": [ | |
| { | |
| "id": 1, | |
| "name": "A" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "B" | |
| }, | |
| { | |
| "id": 3, | |
| "name": "C" | |
| } | |
| ], | |
| "links": [ | |
| { | |
| "source_id": 1, | |
| "target_id": 2 | |
| }, | |
| { | |
| "source_id": 3, | |
| "target_id": 2 | |
| } | |
| ] | |
| } |
| <!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> |