D3 Realtime Areaspline
by JP Obley
HTML
<div id="time"></div>
CSS
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: dodgerblue;
}
text {
font-size: 12px;
fill: #5a5a5a;
}
JavaScript
(function () {
var n = 10,
duration = 10000,
now = new Date(Date.now() - duration),
count = 0,
data = d3.range(n).map(function () {
return 0;
});
var margin = {
top: 20,
right: 0,
bottom: 20,
left: 40
},
width = 600 - margin.right - margin.left,
height = 400 - margin.top;
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.area()
.interpolate("basis")
.x(function (d, i) {
return x(now - (n - 1 - i) * duration);
})
.y0(height)
.y1(function (d, i) {
return y(d);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.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 xAxis = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y(0) + ")")
.call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
var yAxis = svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"));
var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.data([data])
.attr("class", "line");
var text = svg.select("text").attr("font-size", 10);
var timeDiv = d3.select("#time");
tick();
function tick() {
// update the domains
now = new Date();
timeDiv.html(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds());
x.domain([now - (n - 2) * duration, now...