-
-
Save jrichardsz/9fbdb562642802c895f3fee38b73922d to your computer and use it in GitHub Desktop.
D3.js drawing nodes and links from json structure
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> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<title>tags</title> | |
<script type="text/javascript" src="jquery.min.js"></script> | |
<script type="text/javascript" src="d3.v2.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
$(window).resize(function() {$('.resizable').resize(); }) | |
}); | |
</script> | |
<style type="text/css"> | |
circle.node { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
line.link { | |
stroke: #999; | |
stroke-opacity: .6; | |
} | |
</style> | |
</head> | |
<body> | |
<svg id="tags" width="100%" height="1600"> | |
<defs> | |
<marker id="arrow" viewbox="0 -5 10 10" refX="15" refY="-0.5" | |
markerWidth="6" markerHeight="6" orient="auto"> | |
<path d="M0,-5L10,0L0,5Z"> | |
</marker> | |
</defs> | |
</svg> | |
<div id="text"></div> | |
</body> | |
</html> |
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
var w = $('#tags').width(), h = $('#tags').height(); | |
var fill = d3.scale.category10(); | |
var vis = d3.select('#tags').append("svg:g"); | |
d3.json("tags.json", function(json) { | |
var force = d3.layout.force() | |
.charge(-3000) | |
.distance(200) | |
.gravity(0.2) | |
.nodes(json.nodes) | |
.links(json.links) | |
.size([700, 1500]) | |
.start(); | |
var link = vis.selectAll('line.link') | |
.data(json.links) | |
.enter().append('svg:line') | |
.attr("class", "link") | |
.attr("marker-end", "url(#arrow)") | |
.attr("stroke", "red") | |
.style('stroke-width', 2) | |
.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; }); | |
var node = vis.selectAll("circle.node") | |
.data(json.nodes) | |
.enter().append("svg:circle") | |
.attr("class", "node") | |
.attr("name", function(d) { return d.name; }) | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }) | |
.attr("r", 40) | |
.style("fill", function(d) { return fill(d.color); }) | |
.on('dblclick', function(d) { | |
alert(d.name);} | |
) | |
.call(force.drag); | |
var text = vis.selectAll("text") | |
.data(json.nodes) | |
.enter().append("svg:text") | |
.attr("x", function(d) { return d.x ; }) | |
.attr("y", function(d) { return d.y -10; }) | |
.attr("stroke", function(d) { return fill(d.color); }) | |
.text(function(d) { return d.name; }) | |
.attr("font-size","40") | |
.call(force.drag); | |
node.append('title') | |
.text(function(d) { return d.name; }); | |
force.on("tick", function(e) { | |
node.each(function(d) { | |
d.x += ((5-d.group) * 350 - d.x) ; | |
d.x = 1500 - d.x; | |
}); | |
text.each(function(d) { | |
d.x += ((5-d.group) * 350 - d.x) ; | |
d.x = 1500 - d.x; | |
}); | |
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("cx", function(d) { return d.x ; }) | |
.attr("cy", function(d) { return d.y; }); | |
text.attr("x", function(d) { return d.x ; }) | |
.attr("y", function(d) { return d.y; }); | |
}); | |
}); | |
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
var express = require("express"), | |
app = express(), | |
port = parseInt(process.env.PORT, 10) || 4567; | |
app.get("/", function(req, res) { | |
res.redirect("/index.html"); | |
}); | |
app.configure(function(){ | |
app.use(express.methodOverride()); | |
app.use(express.bodyParser()); | |
app.use(express.static(__dirname + '/public')); | |
app.use(express.errorHandler({ | |
dumpExceptions: true, | |
showStack: true | |
})); | |
app.use(app.router); | |
}); | |
app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment