chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("defs")
.append("linearGradient")
.attr("id", "line-gradient")
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", width)
.attr("y2", 0)
.selectAll("stop")
.data([
{ offset: "0%", color: "blue" },
{ offset: "50%", color: "white" },
{ offset: "100%", color: "red" }
])
.enter()
.append("stop")
.attr("offset", function (d) {
return d.offset;
})
.attr("stop-color", function (d) {
return d.color;
});
svg.append("g").call(xGrid);
binnedData.forEach((month, index) => {
const g = svg
.append("g")
.attr("transform", `translate(0,${index * curveHeight * 1.1})`);
g.append("path")
.attr("fill", "url('#line-gradient')")
.attr("d", area(month));
g.append("path")
.attr("fill", "none")
.attr("stroke", "black")
.attr("d", line(month));
g.append("text")
.attr("x", x((month[0].x0 + month[0].x1) / 2))
.attr("y", curveHeight + 5)
.attr("alignment-baseline", "hanging")
.attr("font-family", "sans-serif")
.attr("opacity", 0.6)
.attr("font-size", 10)
.text(`${d3.timeFormat("%b %Y")(dataByMonth[index][1][0].date)}`);
g.append("text")
.attr("text-anchor", "end")
.attr("font-family", "sans-serif")
.attr("font-size", 12)
.attr("opacity", 0.75)
.attr("x", x((month[0].x0 + month[0].x1) / 2) - 10)
.attr("y", curveHeight)
.text(`${month[0].x0}°C`);
g.append("text")
.attr("font-size", 12)
.attr("opacity", 0.75)
.attr("font-family", "sans-serif")
.attr(
"x",
x((month[month.length - 1].x1 + month[month.length - 1].x0) / 2) + 10
)
.attr("y", curveHeight)
.text(`${month[month.length - 1].x1}°C`);
});
return svg.node();
}