Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 11:23
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 d3indepth/64be9fc39a92ef074034e9a8fb29dcce to your computer and use it in GitHub Desktop.
Save d3indepth/64be9fc39a92ef074034e9a8fb29dcce to your computer and use it in GitHub Desktop.
Line generator (curve)
license: gpl-3.0
height: 120
border: no
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment