Skip to content

Instantly share code, notes, and snippets.

@BTKY
Last active September 23, 2017 17:03
Show Gist options
  • Save BTKY/9e7a82a1c3b9be6aa67b2c2e8b31a878 to your computer and use it in GitHub Desktop.
Save BTKY/9e7a82a1c3b9be6aa67b2c2e8b31a878 to your computer and use it in GitHub Desktop.
Weighted nodes and curved links
license: gpl-3.0

This is a quick hack to draw two parallel links between nodes. Yes, you can scale it to more than two nodes, but it gets increasingly awkward. The alternative is to compute the paths manually, say by computing the perpendicular vector of the vector between the node centers, and offsetting the links accordingly.

forked from mbostock's block: Weighted nodes

{"nodes":[{"name":"regering","value":"237","class":"0"},{"name":"soldier","value":"259","class":"2"},{"name":"film","value":"179","class":"4"},{"name":"army","value":"179","class":"2"},{"name":"woman","value":"156","class":"3"},{"name":"department","value":"120","class":"0"},{"name":"magazines","value":"116","class":"4"},{"name":"embassy","value":"111","class":"0"},{"name":"people","value":"109","class":"1"},{"name":"lady","value":"106","class":"3"},{"name":"dollar","value":"103","class":"5"},{"name":"troops","value":"98","class":"2"},{"name":"soldiers","value":"90","class":"2"},{"name":"report","value":"80","class":"6"},{"name":"CULTURE","value":"200","class":"1"},{"name":"secretary","value":"77","class":"0"},{"name":"market","value":"71","class":"5"},{"name":"authorities","value":"66","class":"0"},{"name":"air force","value":"66","class":"2"},{"name":"navy","value":"63","class":"2"},{"name":"research","value":"56","class":"6"},{"name":"congress","value":"52","class":"0"},{"name":"tourists","value":"51","class":"1"},{"name":"show","value":"50","class":"4"},{"name":"envoy","value":"47","class":"2"},{"name":"public","value":"46","class":"1"},{"name":"senate","value":"42","class":"0"},{"name":"president","value":"41","class":"0"},{"name":"zone","value":"40","class":"2"},{"name":"auto","value":"39","class":"5"},{"name":"pilots","value":"39","class":"2"},{"name":"trusts","value":"37","class":"5"},{"name":"flag","value":"33","class":"1"},{"name":"press","value":"31","class":"4"},{"name":"petrol","value":"30","class":"5"},{"name":"way","value":"28","class":"1"},{"name":"television","value":"27","class":"4"},{"name":"men","value":"26","class":"3"},{"name":"concern","value":"25","class":"5"},{"name":"newspapers","value":"25","class":"4"},{"name":"novel","value":"25","class":"1"},{"name":"fleet","value":"25","class":"2"},{"name":"consul","value":"24","class":"0"},{"name":"officers","value":"24","class":"2"},{"name":"author","value":"24","class":"4"},{"name":"railroads","value":"24","class":"5"},{"name":"tobacco industry","value":"24","class":"5"},{"name":"side","value":"24","class":"2"},{"name":"scientists","value":"22","class":"6"},{"name":"firm","value":"21","class":"5"},{"name":"industry","value":"21","class":"5"},{"name":"corporation","value":"21","class":"5"},{"name":"origin","value":"21","class":"1"},{"name":"tanks","value":"21","class":"2"},{"name":"doctor","value":"20","class":"6"},{"name":"banks","value":"20","class":"5"},{"name":"citizens","value":"20","class":"1"},{"name":"girl","value":"20","class":"3"},{"name":"American","value":"","class":"9999"},{"name":"prisoners of war","value":"20","class":"2"},{"name":"medical","value":"20","class":"6"},{"name":"treasury","value":"20","class":"5"},{"name":"tobacco trust","value":"20","class":"5"},{"name":"MILITARY","value":"200","class":"2"},{"name":"GENDER","value":"200","class":"3"},{"name":"MEDIA","value":"200","class":"4"},{"name":"TRADE","value":"200","class":"5"},{"name":"GOVERNMENT","value":"200","class":"0"},{"name":"SCIENCE","value":"200","class":"6"}],"links":[{"source":14,"target":58},{"source":63,"target":58},{"source":64,"target":58},{"source":65,"target":58},{"source":66,"target":58},{"source":67,"target":58},{"source":68,"target":58},{"source":67,"target":0},{"source":63,"target":1},{"source":65,"target":2},{"source":63,"target":3},{"source":64,"target":4},{"source":67,"target":5},{"source":65,"target":6},{"source":67,"target":7},{"source":14,"target":8},{"source":64,"target":9},{"source":66,"target":10},{"source":63,"target":11},{"source":63,"target":12},{"source":68,"target":13},{"source":67,"target":15},{"source":66,"target":16},{"source":67,"target":17},{"source":63,"target":18},{"source":63,"target":19},{"source":68,"target":20},{"source":67,"target":21},{"source":14,"target":22},{"source":65,"target":23},{"source":63,"target":24},{"source":14,"target":25},{"source":67,"target":26},{"source":67,"target":27},{"source":63,"target":28},{"source":66,"target":29},{"source":63,"target":30},{"source":66,"target":31},{"source":14,"target":32},{"source":65,"target":33},{"source":66,"target":34},{"source":14,"target":35},{"source":65,"target":36},{"source":64,"target":37},{"source":66,"target":38},{"source":65,"target":39},{"source":14,"target":40},{"source":63,"target":41},{"source":67,"target":42},{"source":63,"target":43},{"source":65,"target":44},{"source":66,"target":45},{"source":66,"target":46},{"source":63,"target":47},{"source":68,"target":48},{"source":66,"target":49},{"source":66,"target":50},{"source":66,"target":51},{"source":14,"target":52},{"source":63,"target":53},{"source":68,"target":54},{"source":66,"target":55},{"source":14,"target":56},{"source":64,"target":57},{"source":63,"target":59},{"source":68,"target":60},{"source":66,"target":61},{"source":66,"target":62}]}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link line {
stroke: #696969;
}
.link line.separator {
stroke: #fff;
stroke-width: 2px;
}
.node circle {
stroke: #000;
stroke-width: 1.5px;
}
.node text {
font: 10px sans-serif;
pointer-events: none;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var radius = d3.scale.sqrt()
.range([0, 6]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.size([width, height])
.charge(-400)
.linkDistance(function(d) { return radius(d.source.value / 2) + radius(d.target.value / 2) + 5; });
d3.json("graph.json", function(error, graph) {
if (error) throw error;
for (var i = 0; i < graph.nodes.length; i++) {
graph.nodes[i].class = +graph.nodes[i].class;
graph.nodes[i].value = +graph.nodes[i].value;
}
force
.nodes(graph.nodes)
.links(graph.links)
.on("tick", tick)
.start();
var link = svg.append("svg:g").selectAll("path")
.data(force.links())
.enter().append("svg:path")
link.style('fill', 'none')
.style('stroke', 'black')
.style("stroke-width", '2px');
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", function(d) { return radius(d.value / 2); })
.style("fill", function(d) {console.log(d); return color(d.class);});
node.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.name; });
function tick() {
link.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" +
d.source.x + "," +
d.source.y + "A" +
dr + "," + dr + " 0 0,1 " +
d.target.x + "," +
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