chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg.append("g").call(xAxis);
svg.append("g").call(yAxis);
svg.append("g").call(grid);
const trail = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-width", 0.5)
.attr("stroke-miterlimit", 1);
const path = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 2);
const yearTitle = svg
.append("text")
.attr("font-size", 80)
.attr("font-family", "sans")
.attr("x", (width * 3.5) / 5)
.attr("y", (height * 1.5) / 5)
.attr("opacity", 0.2);
const dot = svg.append("circle").attr("fill", "red").attr("r", 4);
return Object.assign(svg.node(), {
update(date) {
const year = date.getFullYear();
yearTitle.text(year);
if (year > 2017) {
svg
.selectAll(`.path-${year - 1}`)
.data([1])
.enter()
.append("path")
.classed(`path-${year - 1}`, true)
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-width", 0.5)
.attr("stroke-miterlimit", 1)
.attr("d", line(finalData.get(year - 1)));
}
const yearData = finalData.get(year);
const i = bisectDate(yearData, d3.utcMonth.offset(date, -3), 0);
const j = bisectDate(yearData, date, 0);
trail.attr("d", line(yearData.slice(0, i)));
path.attr("d", line(yearData.slice(Math.max(0, i - 1), j)));
dot.attr("cx", x(date.getDOY())).attr("cy", y(yearData[j - 1].moving));
}
});
}