JSFiddle - React, Tailwind, and code Playground
CSS
svg {
font: 10px sans-serif;
}
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
JavaScript
var n = 40,
random = d3.random.normal(0, .2),
data = []
var margin = {
top: 20,
right: 20,
bottom: 20,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0, n - 1])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, 60])
.range([height, 0]);
var line = d3.svg.line()
.x(function (d, i) {
return x(i);
})
.y(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);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y(0) + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));
var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
socket = new WebSocket("wss://wss.redditmedia.com/thebutton?h=61e3651cd8fac82c0c03d8d9cc5f583e2200d8d7&e=1427996771")
socket.onmessage = function (data2) {
data.push(JSON.parse(data2.data).payload.seconds_left)
// redraw the line, and slide it to the left
path.attr("d", line)
.attr("transform", null)
if (data.length > 40) {
data.shift()
path.transition()
.duration(500)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ",0)");
}
}