legend = () => {
const wrapper = d3.create("div")
.attr("class", "legend");
wrapper.append("div")
.attr("class", "legend-title")
.text("Days with heat waves that stress transformers");
const margin = {left: 1, right: 42, top: 1, bottom: 19};
const width = 280 - margin.left - margin.right;
const height = 12;
const rect0Width = 16
const svg = wrapper.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.call(gradient);
const scale = d3.scaleLinear()
.domain([0, 120])
.range([0, width - rect0Width]);
const g = svg.append("g")
.attr("transform", `translate(${[margin.left, margin.top]})`);
const rectGradient = g.append("rect")
.attr("fill", `url(#${gradient.id()})`)
.attr("x", rect0Width)
.attr("width", width - rect0Width)
.attr("height", height);
const rect0 = g.append("rect")
.attr("class", "rect-0")
.attr("width", rect0Width)
.attr("height", height);
const tick = g.selectAll(".tick")
.data(d3.range(0, 150, 30))
.join("g")
.attr("class", "tick")
.attr("transform", d => `translate(${rect0Width + scale(d)}, ${height})`);
tick.append("text")
.attr("x", (d, i, e) => i === e.length - 1 ? 10 : 0)
.attr("y", 14)
.text((d, i, e) => i === 0 ? 1 : `${i === e.length - 1 ? ">" : ""}${d}${i === e.length - 1 ? " days" : ""}`);
return wrapper.node();
}