Public
Edited
Mar 11, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const div = html`<div style="position:relative;">`;

const canvas = d3.select(div)
.selectAll("canvas")
.data(data)
.enter().append(() => DOM.context2d(width, step, 1).canvas)
.style("position", "absolute")
.style("image-rendering", "pixelated")
.style("top", (d, i) => `${i * (step + 1) + margin.top}px`)
.property("context", function() { return this.getContext("2d"); })
.each(horizon);

const svg = d3.select(div.appendChild(DOM.svg(width, height)))
.style("position", "relative")
.style("font", "10px sans-serif");

const gX = svg.append("g");

svg.append("g")
.selectAll("text")
.data(data)
.join("text")
.attr("x", 4)
.attr("y", (d, i) => (i + 0.5) * (step + 1) + margin.top)
.attr("dy", "0.35em")
.text((d, i) => i);

const rule = svg.append("line")
.attr("stroke", "#000")
.attr("y1", margin.top - 6)
.attr("y2", height - margin.bottom - 1)
.attr("x1", 0.5)
.attr("x2", 0.5);

svg.on("mousemove touchmove", () => {
const x = d3.mouse(svg.node())[0] + 0.5;
rule.attr("x1", x).attr("x2", x);
});

function horizon(d) {
const {context} = this;
const {length: k} = d;
if (k < width) context.drawImage(this, k, 0, width - k, step, 0, 0, width - k, step);
context.fillStyle = "#fff";
context.fillRect(width - k, 0, k, step);
for (let i = 0; i < overlap; ++i) {
context.save();
context.translate(width - k, (i + 1) * step);
context.fillStyle = color(i);
for (let j = 0; j < k; ++j) {
context.fillRect(j, y(d[j]), 1, -y(d[j]));
}
context.restore();
}
}

div.update = data => {
canvas.data(data).each(horizon);
gX.call(xAxis);
};

return div;
}
Insert cell
height = data.length * (step + 1) + margin.top + margin.bottom
Insert cell
margin = ({top: 30, right: 10, bottom: 0, left: 10})
Insert cell
step = 29
Insert cell
color = i => d3[scheme][Math.max(3, overlap)][i + Math.max(0, 3 - overlap)]
Insert cell
x = d3.scaleTime()
.range([0, width])
Insert cell
y = d3.scaleLinear()
.rangeRound([0, -overlap * step])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(width / 80).tickSizeOuter(0))
.call(g => g.selectAll(".tick").filter(d => x(d) < margin.left || x(d) >= width - margin.right).remove())
.call(g => g.select(".domain").remove())
Insert cell
data = {
const n = 20, m = 964;
const data = new Array(n);
for (let i = 0; i < n; ++i) {
const d = data[i] = new Float64Array(m);
for (let j = 0, v = 0; j < m; ++j) {
d[j] = v = walk(v);
}
}
return data;
}
Insert cell
update = {
const period = 250, m = data[0].length;
const tail = data.map(d => d.subarray(m - 1, m));
while (true) {
const then = new Date(Math.ceil((Date.now() + 1) / period) * period);
yield Promises.when(then, then);
for (const d of data) d.copyWithin(0, 1, m), d[m - 1] = walk(d[m - 1]);
x.domain([then - period * width, then]);
chart.update(tail);
}
}
Insert cell
function walk(v) {
return Math.max(0, Math.min(1, v + (Math.random() - 0.5) * 0.05));
}
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more