chart = () => {
const svg = d3.create("svg")
.attr("width", chartwidth + margin.left + margin.right)
.attr("height", chartheight + margin.top + margin.bottom);
svg.append("style").html(css);
const g = svg.append("g")
.attr("transform", `translate(${[margin.left, margin.top]})`);
g.append("g")
.attr("class", "axis y-axis")
.call(
d3.axisLeft(y)
.tickFormat(d => `${d > 0 ? "+" : ""}${+d.toFixed(1)}°C`)
.tickSize(8)
.tickValues(d3.range(-1, 3))
)
g.append("g")
.attr("class", "axis x-axis")
.attr("transform", `translate(0, ${chartheight})`)
.call(
d3.axisBottom(x)
.tickFormat(d => d)
.tickSize(8)
.tickValues(d3.range(1850, 2050, 50))
)
g.append("line")
.attr("class", "baseline")
.attr("transform", `translate(0, ${y(0)})`)
.attr("x2", chartwidth);
g.append("line")
.attr("class", "baseline")
.attr("y1", y(2))
.attr("y2", y(-1));
g.append("path")
.datum(temperature_sources)
.attr("fill", colors.aerBg)
.attr("d", areaAer);
g.append("path")
.datum(temperature_sources)
.attr("fill", colors.natBg)
.attr("d", areaNat);
g.append("path")
.datum(temperature_sources)
.attr("fill", colors.ghgBg)
.attr("d", areaGhg);
g.append("path")
.datum(temperature_sources)
.attr("fill", "#666")
.attr("fill-opacity", 0.1)
.attr("d", areaSsp245);
g.append("path")
.datum(temperature_sources)
.attr("stroke", colors.aer)
.attr("fill", "none")
.attr("d", lineAer);
g.append("path")
.datum(temperature_sources)
.attr("stroke", colors.nat)
.attr("fill", "none")
.attr("d", lineNat);
g.append("path")
.datum(temperature_sources)
.attr("stroke", colors.ghg)
.attr("fill", "none")
.attr("d", lineGhg);
g.append("path")
.datum(temperature_sources)
.attr("stroke", "#aaa")
.attr("fill", "none")
.attr("d", lineSsp245);
g.append("path")
.datum(temperature_sources)
.attr("stroke", "#2a2a2a")
.attr("stroke-width", 2)
.attr("fill", "none")
.attr("d", lineObs);
g.append("g").attr("transform", "translate(4)").selectAll(".label")
.data(labelData)
.join("text")
.attr("class", "label")
.attr("fill", d => colors[d.key])
.attr("font-weight", d => d.key === "obs" ? "bold" : "normal")
.attr("x", x(x.domain()[1]))
.attr("y", d => y(d.value))
.html(d => d.label)
return svg.node();
}