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("h1")
.style("color", d => color_scale(d))
.text(d => d);
const vis = container.append("div").attr("class", "vis-container");
const svg = vis
.append("svg")
.attr("width", width)
.attr("height", height);
const xAxis = svg
.append("g")
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(x).tickFormat(d3.format("")));
const yAxis = svg
.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(d3.format("")));
const sample_data = race_year_data.filter(d => d.race == "All Races");
const line = svg
.selectAll("path.line")
.data([sample_data])
.join("path")
.attr("class", "line")
.attr("d", line_fn)
.attr("fill", "none")
.attr("stroke", "black");
const rects = svg
.selectAll("rect")
.data(sample_data)
.join("rect")
.attr("height", d => height - y(d.avg_earning) - margin.bottom)
.attr("y", d => y(d.avg_earning))
.attr("width", x_bar.bandwidth())
.attr("x", d => x_bar(d.year));
const update = section => {
if (section === mutable currentSection) return;
mutable currentSection = section;
const line_data = race_year_data.filter(
d => d.race == race_categories[section]
);
svg
.selectAll("path.line")
.data([line_data])
.join("path")
.transition()
.attr("d", line_fn);
svg
.selectAll("rect")
.data(line_data)
.transition()
.delay(d => x(d.year))
.attr("fill", color_scale(race_categories[section]))
.attr("height", d => height - y(d.avg_earning) - margin.bottom)
.attr("y", d => y(d.avg_earning))
.attr("width", x_bar.bandwidth())
.attr("x", d => x_bar(d.year));
};
narrativeContainer.on("scroll", () => {
const totalHeight = narrativeContainer.node().getBoundingClientRect().top;
const offset = narrative.node().getBoundingClientRect().y;
const section = Math.floor((totalHeight - offset) / sectionHeight);
update(section);
});
return container.node();
}