legend = () => {
const wrapper = d3.create("div")
.style("width", `${w}px`)
wrapper.append("div")
.style("font-family", franklinLight)
.style("font-size", "16px")
.style("font-weight", "bold")
.style("margin-bottom", "-4px")
.style("text-align", "center")
.text(select.name);
wrapper.append("div")
.style("font-family", franklinLight)
.style("font-size", "16px")
.style("text-align", "center")
.style("margin-bottom", "8px")
.text("Compared to 1993-2016 average");
const tickSize = 0;
const margin = {left: 0, right: 0, top: 0, bottom: tickSize + 18};
const width = 300 - margin.left - margin.right;
const height = 12;
const svg = wrapper.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("display", "table")
.style("margin", "0 auto")
.style("overflow", "visible");
svg.call(gradient);
const values = extent;
const scale = d3.scaleLinear(extent, [0, width]);
const g = svg.append("g")
.attr("transform", `translate(${[margin.left, margin.top]})`);
g.append("rect")
.attr("fill", `url(#${gradient.id()})`)
.attr("height", height)
.attr("width", width);
const tick = g.selectAll(".tick")
.data(values)
.join("g")
.attr("class", "tick")
.attr("transform", d => `translate(${[scale(d), height]})`);
tick.append("line")
.attr("stroke", "black")
.attr("y2", tickSize);
tick.append("text")
.attr("dy", tickSize)
.attr("font-family", franklinLight)
.attr("font-size", 14)
.attr("text-anchor", (d, i) => i === 0 ? "start" : "end")
.attr("y", 14)
.text((d, i) => select.legend[i]);
return wrapper.node();
}