A force-directed graph using images as nodes, with accompanying text labels.
-
-
Save vvzvlad/772f0266307b455a06c0f0b07924384a to your computer and use it in GitHub Desktop.
Labeled Force Layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nodes": [ | |
{"name": "0c47:3806","group":1}, | |
{"name": "0c47:3e00","group":1}, | |
{"name": "0c47:4a85","group":1}, | |
{"name": "0c47:4a02","group":1} | |
], | |
"links": [ | |
{"source": 1, "target": 0,"value":1}, | |
{"source": 2, "target": 0,"value":1}, | |
{"source": 3, "target": 0,"value":1}, | |
{"source": 4, "target": 0,"value":1} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.link { | |
stroke: #ccc; | |
} | |
.node text { | |
pointer-events: none; | |
font: 10px sans-serif; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500 | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var force = d3.layout.force() | |
.gravity(0.05) | |
.distance(100) | |
.charge(-100) | |
.size([width, height]); | |
d3.json("graph.json", function(error, json) { | |
if (error) throw error; | |
force | |
.nodes(json.nodes) | |
.links(json.links) | |
.start(); | |
var link = svg.selectAll(".link") | |
.data(json.links) | |
.enter().append("line") | |
.attr("class", "link"); | |
var node = svg.selectAll(".node") | |
.data(json.nodes) | |
.enter().append("g") | |
.attr("class", "node") | |
.call(force.drag); | |
node.append("image") | |
.attr("xlink:href", "https://github.com/favicon.ico") | |
.attr("x", -8) | |
.attr("y", -8) | |
.attr("width", 16) | |
.attr("height", 16); | |
node.append("text") | |
.attr("dx", 12) | |
.attr("dy", ".35em") | |
.text(function(d) { return d.name }); | |
force.on("tick", function() { | |
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 + ")"; }); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment