Line generator (using .curve()).
From D3 in Depth book by Peter Cook.
license: gpl-3.0 | |
height: 120 | |
border: no |
Line generator (using .curve()).
From D3 in Depth book by Peter Cook.
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<head> | |
<title>Line (using curveCardinal)</title> | |
</head> | |
<style> | |
path { | |
fill: none; | |
stroke: #999; | |
} | |
circle { | |
fill: none; | |
stroke: #aaa; | |
} | |
</style> | |
<body> | |
<svg width="700" height="110"> | |
<path></path> | |
</svg> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script> | |
<script> | |
var lineGenerator = d3.line() | |
.curve(d3.curveCardinal); | |
var points = [ | |
[0, 80], | |
[100, 100], | |
[200, 30], | |
[300, 50], | |
[400, 40], | |
[500, 80] | |
]; | |
var pathData = lineGenerator(points); | |
d3.select('path') | |
.attr('d', pathData); | |
// Also draw points for reference | |
d3.select('svg') | |
.selectAll('circle') | |
.data(points) | |
.enter() | |
.append('circle') | |
.attr('cx', function(d) { | |
return d[0]; | |
}) | |
.attr('cy', function(d) { | |
return d[1]; | |
}) | |
.attr('r', 3); | |
</script> | |
</body> | |
</html> |