|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
|
|
<style> |
|
.node { |
|
stroke: #fff; |
|
stroke-width: 1.5px; |
|
} |
|
|
|
.link { |
|
stroke: #999; |
|
stroke-opacity: 1; |
|
} |
|
</style> |
|
|
|
<script src="//d3js.org/d3.v3.js"></script> |
|
|
|
<body> |
|
|
|
<script> |
|
var width = 960, |
|
height = 500; |
|
|
|
// var color = d3.scale.category10(); |
|
var color = "red"; |
|
|
|
var force = d3.layout.force() |
|
.charge(-120) |
|
.linkDistance(90) |
|
.size([width, height]); |
|
|
|
var svg = d3.select("body").append("svg") |
|
.attr("width", width) |
|
.attr("height", height); |
|
|
|
d3.json("contributions.json", function(error, graph) { |
|
if (error) throw error; |
|
|
|
force |
|
.nodes(graph.nodes) |
|
.links(graph.links) |
|
.start(); |
|
|
|
var scale = d3.scale.linear().domain([.5, 1]).range([1, 3]); |
|
|
|
var color_ramp = d3.scale.linear().domain([.5,1]).range(["#ccc","#333"]); |
|
|
|
var radius_scale = d3.scale.linear().domain([30, 610]).range([4, 20]); |
|
|
|
var color_scale = d3.scale.linear().domain([30, 610]).range(["#ff6666", "#cc0000"]); |
|
|
|
var link = svg.selectAll(".link") |
|
.data(graph.links) |
|
.enter().append("line") |
|
.attr("class", "link") |
|
.style("stroke-width", function(d) { return scale(d.weight); }) |
|
.style("stroke", function(d) { return color_ramp(d.weight); }); |
|
|
|
var node = svg.selectAll(".node") |
|
.data(graph.nodes) |
|
.enter().append("circle") |
|
.attr("class", "node") |
|
.attr("r", function(d) { return radius_scale(d.edits); }) |
|
.style("fill", function(d) { return color_scale(d.edits); }) |
|
.on("mouseover", function() { |
|
d3.select(this) |
|
.style("stroke", "#000") |
|
}) |
|
.on("mouseout", function() { |
|
d3.select(this) |
|
.style("stroke", "#fff"); |
|
}) |
|
.call(force.drag); |
|
|
|
node.append("title") |
|
.text(function(d) { return d.id + ": " + d.edits + " edits"; }); |
|
|
|
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> |