{
const svg = d3.create("svg").attr("width", width).attr("height", height);
const rects = svg
.selectAll("rects")
.data(data)
.enter()
.append("rect")
.attr("x", (d) => xScale(d.year))
.attr("y", margin.top)
.attr("width", xScale.bandwidth())
.attr("height", height - margin.top - margin.bottom)
.attr("fill", (d) => colorScale(d.avg));
const legendRects = svg
.selectAll("legend-rects")
.data(legendData)
.enter()
.append("rect")
.attr("x", (d, i) => xLegendScale(i))
.attr("y", height - margin.bottom + 50)
.attr("width", xLegendScale.bandwidth())
.attr("height", 20)
.attr("fill", (d) => colorScale(d));
const legendLabels = svg
.selectAll("legend-labels")
.data(legendData)
.enter()
.append("text")
.attr("x", (d, i) => xLegendScale(i) + xLegendScale.bandwidth() / 2)
.attr("y", height - margin.bottom + 50 + 15)
.text((d) => d3.format(".1f")(d))
.style("fill", (d) => (Math.abs(d) >= 0.5 ? "#fff" : "#111"))
.attr("class", "legend-labels");
const unit = svg
.append("text")
.text("(°C)")
.attr("x", xLegendScale(legendData.length - 1) + 60)
.attr("y", height - margin.bottom + 50 + 15)
.attr("class", "legend-labels");
svg
.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.attr("class", "x-axis")
.call(xAxis);
return svg.node();
}