Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active November 22, 2016 21:05
Show Gist options
  • Select an option

  • Save mbostock/1016220 to your computer and use it in GitHub Desktop.

Select an option

Save mbostock/1016220 to your computer and use it in GitHub Desktop.
Line Tension
<!DOCTYPE html>
<html>
<head>
<title>Line Chart</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<style type="text/css">
body {
font: 10px sans-serif;
}
.rule line {
stroke: #eee;
shape-rendering: crispEdges;
}
.rule line.axis {
stroke: #000;
}
path {
fill: none;
stroke-width: 1.5px;
}
circle {
fill: #fff;
stroke: #000;
}
</style>
</head>
<body>
<script type="text/javascript">
var data = d3.range(10).map(function(i) {
return {x: i / 9, y: (Math.sin(i / 2) + 1) / 2};
});
var w = 960,
h = 500,
p = 40,
x = d3.scale.linear().domain([0, 1]).range([p, w - p]),
y = d3.scale.linear().domain([0, 1]).range([h - p, p]);
var line = d3.svg.line()
.interpolate("cardinal")
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); });
var vis = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g");
var rules = vis.selectAll("g.rule")
.data(x.ticks(10))
.enter().append("svg:g")
.attr("class", "rule");
rules.append("svg:line")
.attr("x1", x)
.attr("x2", x)
.attr("y1", p)
.attr("y2", h - p - 1);
rules.append("svg:line")
.attr("class", function(d) { return d ? null : "axis"; })
.attr("y1", y)
.attr("y2", y)
.attr("x1", p)
.attr("x2", w - p + 1);
rules.append("svg:text")
.attr("x", x)
.attr("y", h - p + 3)
.attr("dy", ".71em")
.attr("text-anchor", "middle")
.text(x.tickFormat(10));
rules.append("svg:text")
.attr("y", y)
.attr("x", p - 3)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(y.tickFormat(10));
vis.selectAll("path")
.data([0, 0.2, 0.4, 0.6, 0.8, 1])
.enter().append("svg:path")
.attr("d", function(d) { return line.tension(d)(data); })
.style("stroke", d3.interpolateRgb("brown", "steelblue"));
vis.selectAll("circle")
.data(data)
.enter().append("svg:circle")
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.attr("r", 4.5);
</script>
</body>
</html>
@pembeci
Copy link
Copy Markdown

pembeci commented Jan 5, 2016

The corresponding block is not working. Either I am missing something or data.tsv is missing.

@iblind
Copy link
Copy Markdown

iblind commented Jan 18, 2016

Nope, pretty sure data.tsv is missing on this one

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