Skip to content

Instantly share code, notes, and snippets.

@edsu
Last active February 2, 2016 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edsu/211a6ff1ecd28f7ba802 to your computer and use it in GitHub Desktop.
Save edsu/211a6ff1ecd28f7ba802 to your computer and use it in GitHub Desktop.
MITH Projects Graph
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
body {
text-align: center;
}
svg {
border: thin solid gray;
}
</style>
<body>
<script src="//d3js.org/d3.v3.js"></script>
<script>
var width = 1000,
height = 1000;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-200)
.linkDistance(20)
.gravity(.20)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("http://mith.umd.edu/wp-content/mu-plugins/mith-research-explorer-data/projects.json", function(error, projects) {
if (error) throw error;
nodes = [];
links = [];
nodeIds = {};
function nodeId(s, g) {
if (! nodeIds[s]) {
nodeIds[s] = nodes.length;
nodes.push({name: s, group: g});
}
return nodeIds[s];
}
projects.forEach(function(p) {
if (p.member.length == 0) return;
var projectId = nodeId(p.title, 1);
p.member.forEach(function(m) {
links.push({
"source": nodeId(m.name, 2),
"target": projectId
});
if (! m.affiliation.match(/University of Maryland/)) {
links.push({
"source": nodeId(m.name, 2),
"target": nodeId(m.affiliation, 3)
});
}
});
});
force
.nodes(nodes)
.links(links)
.start();
var link = svg.selectAll(".link")
.data(links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment