Skip to content

Instantly share code, notes, and snippets.

@houkensjtu
Created November 5, 2017 13:41
Show Gist options
  • Save houkensjtu/e5aba266fb6287c091f435eb97df3eca to your computer and use it in GitHub Desktop.
Save houkensjtu/e5aba266fb6287c091f435eb97df3eca to your computer and use it in GitHub Desktop.
Line generator // source https://jsbin.com/layara
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>Line generator</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ['\\(','\\)'] ],
processEscapes: true
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_CHTML">
</script>
</head>
<body>
<h1>The cardinal sine function</h1>
<h2>$f(x)=\frac{\sin(x)}{x}$</h2>
<svg width="900" height="400"></svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var lineGenerator = d3.line();
sineCurve = [];
for (var i=0;i<1000;i++){
sineCurve.push([i-120, 80*Math.sin(i/30)/((i-110)*0.007+0.0001)+200]);
}
var pathData = lineGenerator(sineCurve);
// Select the path element and set its d attribute
d3.select('svg').append('path')
.attr('d', pathData)
.attr('stroke', 'magenta')
.attr('stroke-width', '6')
.attr('fill','none')
.style("mix-blend-mode", "darken");
sineCurve = [];
for (var i=0;i<1000;i++){
sineCurve.push([i-140, 80*Math.sin(i/30)/((i-110)*0.006+0.0001)+200]);
}
var pathData = lineGenerator(sineCurve);
d3.select('svg').append('path')
.attr('d', pathData)
.attr('stroke', 'cyan')
.attr('stroke-width', '6')
.attr('fill','none')
.style("mix-blend-mode", "darken");
sineCurve = [];
for (var i=0;i<1000;i++){
sineCurve.push([i-160, 80*Math.sin(i/30)/((i-110)*0.005+0.0001)+200]);
}
var pathData = lineGenerator(sineCurve);
d3.select('svg').append('path')
.attr('d', pathData)
.attr('stroke', 'yellow')
.attr('stroke-width', '6')
.attr('fill','none')
.style("mix-blend-mode", "darken");
</script>
</body>
</html>
@houkensjtu
Copy link
Author

A first-try of plotting math function with D3.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment