chart = () => {
const svg = d3.create("svg");
svg.append("style").html(css);
const g = svg.append("g");
const xaxis = g.append("g")
.attr("class", "axis x-axis");
const yaxis = g.append("g")
.attr("class", "axis y-axis");
const line = g.selectAll(".line")
.data(data)
.join("path")
.attr("class", "line")
.attr("stroke", d => color(d.lead_time));
const label = g.selectAll(".label")
.data(data)
.join("text")
.attr("class", "label")
.attr("fill", d => color(d.lead_time))
.html((d, i, e) => `<tspan>${d.lead_time}${i === e.length - 1 ? "-day</tspan><tspan>forecast" : " days"}</tspan>`)
return Object.assign(svg.node(), {
resize(ww) {
const margin = { left: 33, right: 70, top: 4, bottom: 24};
const basewidth = Math.min(640, ww);
const width = basewidth - margin.left - margin.right;
const height = Math.max(300, basewidth * 9 / 16) - margin.top - margin.bottom;
x.range([0, width]);
y.range([height, 0]);
xaxisGenerator.tickSize(height + 10)
xaxis
.call(xaxisGenerator);
yaxisGenerator.tickSize(width + 10)
yaxis
.attr("transform", `translate(${width})`)
.call(yaxisGenerator)
svg
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
g
.attr("transform", `translate(${[margin.left, margin.top]})`);
line
.attr("d", d => lineGenerator(d.errors));
label
.attr("y", d => y(d.errors[d.errors.length - 1].error))
.selectAll("tspan")
.attr("dx", 5)
.attr("dy", (d, i) => 5 + i * 17)
.attr("x", width)
}
})
}