Skip to content

Instantly share code, notes, and snippets.

@trinary
Created June 11, 2013 20:48
Show Gist options
  • Save trinary/5760526 to your computer and use it in GitHub Desktop.
Save trinary/5760526 to your computer and use it in GitHub Desktop.
Tributary inlet
{"description":"Tributary inlet","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":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
var n = 243,
duration = 750,
now = new Date(Date.now() - duration),
count = 0,
data = d3.range(n).map(function() { return 0; });
data1 = d3.range(n).map(function() { return 0; });
var margin = {top: 6, right: 0, bottom: 20, left: 40},
width = 960 - margin.right,
height = 120 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([now - (n - 2) * duration, now - duration])
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var line = d3.svg.line()
.interpolate("basis")
.x(function(d, i) { return x(now - (n - 1 - i) * duration); })
.y(function(d, i) { return y(d); });
var line1 = d3.svg.line()
.interpolate("basis")
.x(function(d, i) { return x(now - (n - 1 - i) * duration); })
.y(function(d, i) { return y(d); });
var svg = d3.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("margin-left", -margin.left + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var axis = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.data([data])
.attr("class", "line");
var path1 = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.data([data1])
.attr("class", "line1");
tick();
function tick() {
console.log("tick", duration);
// update the domains
now = new Date();
x.domain([now - (n - 2) * duration, now - duration]);
y.domain([0, d3.max(data.concat(data1))]);
// push the accumulated count onto the back, and reset the count
data.push(Math.random());
data1.push(Math.random());
count = 0;
// redraw the line
svg.select(".line")
.attr("d", line)
.style({
fill: "none",
stroke: "blue",
"stroke-width":"2px"
})
.attr("transform", null);
// redraw the line
svg.select(".line1")
.attr("d", line)
.style({
fill: "none",
stroke: "blue",
"stroke-width":"2px"
})
.attr("transform", null);
// slide the x-axis left
axis.transition()
.duration(duration)
.ease("linear")
.call(x.axis);
// slide the line left
var x_val = x(now - (n - 1) * duration)
path.transition()
.duration(duration)
.ease("linear")
.attr("transform", "translate(" + x_val + ")");
path1.transition()
.duration(duration)
.ease("linear")
.attr("transform", "translate(" + x_val + ")")
// pop the old data point off the front
data.shift();
data1.shift();
}
setInterval(tick,duration);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment