chart = {
const container = d3.create("div").attr("class", "scroll-container");
const narrativeContainer = container
.append("div")
.attr("class", "narrative-container");
const narrative = narrativeContainer.append("div").attr("class", "narrative");
narrative
.selectAll(".section")
.data(descriptions)
.join("div")
.attr("class", "section")
.append("text")
.text(d => d);
const vis = container.append("div");
const svg = vis
.append("svg")
.attr("height", height)
.attr("width", chartWidth);
svg.append("g").call(xAxis);
svg.append("g").call(yAxis);
const update = section => {
if (section === mutable currentSection) return;
mutable currentSection = section;
const min_date = x.domain()[0];
const max_date = new Date(min_date.setMonth(min_date.getMonth() + section));
const transition = ele =>
ele
.transition()
.duration(1000)
.delay((d, i) => {
const one_day = 24 * 60 * 60 * 1000;
const days_between = (max_date.getTime() - d.x.getTime()) / one_day;
return 50 * (30 - days_between);
})
.attr("r", 3);
const points = svg
.selectAll("circle")
.data(ny_data.filter(d => d.x < max_date))
.join(enter =>
enter
.append("circle")
.attr("r", 0)
.call(transition)
)
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y));
};
narrativeContainer.on("scroll", () => {
const totalHeight = narrativeContainer.node().getBoundingClientRect().top;
const offset = narrative.node().getBoundingClientRect().y;
const section = Math.floor((totalHeight - offset + 10) / sectionHeight);
update(section);
});
return container.node();
}