Skip to content

Instantly share code, notes, and snippets.

@meveritt
Created November 15, 2014 00:14
Show Gist options
  • Save meveritt/67318ab15a740442f97f to your computer and use it in GitHub Desktop.
Save meveritt/67318ab15a740442f97f to your computer and use it in GitHub Desktop.
Simple Spline from Data
{"description":"Simple Spline from Data","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true}
var svg = d3.select('svg')
var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
var width = tributary.sw;
var height = 100;
var x = d3.scale.linear()
.domain([0, 48])
.range([-15, width]);
var y = d3.scale.linear()
.domain([0, 10])
.range([0, height]);
var line = d3.svg.line()
.x(function(d,i) { return x(i) })
.y(function(d,i) { return y(d) })
.interpolate('basis');
var g = svg.append('g')
.attr('transform', 'translate(0, 250)');
g.append('path').attr('d', line(data))
.style({
fill: 'none',
stroke: 'steelblue',
'stroke-width': 1
});
function redrawWithAnimation() {
g.selectAll('path')
.data([data])
.attr('transform', 'translate(' + x(1) + ')')
.attr('d', line)
.transition()
.ease('linear')
.duration(1000)
.attr('transform', 'translate(' + x(0) + ')')
}
setInterval(function() {
var v = data.shift();
data.push(v);
redrawWithAnimation();
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment