{
const svg = d3.create("svg").attr("width", width).attr("height", height);
const g = svg
.append("g")
.style("font-size", 10)
.style("font-family", "sans-serif");
g.selectAll("box")
.data(global_data)
.enter()
.append("rect")
.attr("x", (d) => xScale(d.year))
.attr("y", (d) => margin.top + 30)
.attr("width", xScale.bandwidth())
.attr("height", (d) => height - margin.top - margin.bottom - 30)
.style("fill", (d) => color(d.avg));
g.append("text")
.text("Temperature (°C)")
.attr("x", margin.left)
.attr("y", margin.top + 20)
.attr("class", "label");
const legendRects = svg
.selectAll("legend-rects")
.data(legendData)
.enter()
.append("rect")
.attr("x", (d, i) => xLegendScale(i))
.attr("y", height - margin.bottom + 30)
.attr("width", xLegendScale.bandwidth())
.attr("height", 23)
.attr("fill", (d) => color(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 + 30 + 16)
.text((d) => d3.format(".1f")(d))
.style("fill", (d) => (Math.abs(d) >= 0.5 ? "#fff" : "#111"))
.attr("class", "legend-labels");
return svg.node();
}