Skip to content

Instantly share code, notes, and snippets.

@valex
Last active November 8, 2017 20:55
Show Gist options
  • Save valex/ad5ce30a196193ea18ea53b64eeb636f to your computer and use it in GitHub Desktop.
Save valex/ad5ce30a196193ea18ea53b64eeb636f to your computer and use it in GitHub Desktop.
Links (or diagonals) in d3.js version 4
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="http://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
<script>
var svg = d3.select('body')
.append('svg')
.attrs({
width: 1000,
height: 200
});
var source = [500, 50];
var targets = [
[100, 150],
[300, 150],
[500, 150],
[700, 150],
[900, 150],
];
var links = targets.map(function (target) {
return { source: source, target: target };
});
linksGenerator = d3.linkHorizontal() // d3.linkVertical()
.source(function(d) {return d.source;})
.target(function(d) {return d.target;});
svg.selectAll('path')
.data(links)
.enter()
.append('path')
.attrs({
d: linksGenerator,
fill: 'none',
stroke: '#ccc'
});
var nodes = targets.concat([source]);
console.log(nodes);
svg.selectAll('circle')
.data(nodes)
.enter().append('circle')
.attrs({
r: 20,
cx: function (d) { return d[0]; },
cy: function (d) { return d[1]; },
fill: 'orange',
stroke: '#333'
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment