chart = () => {
const svg = d3.create("svg").attr("class", "chart");
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 dot = g.append("g")
.attr("class", "dots")
.selectAll(".dot")
.data(data)
.join("circle")
.attr("class", "dot")
.attr("dx", 5)
.attr("fill", d => fill(d.season))
.attr("stroke", d => stroke(d.season));
const label = g.append("g")
.attr("class", "labels")
.selectAll(".label")
.data(data.filter(d => d.days === 7))
.join("text")
.attr("class", "label");
return Object.assign(svg.node(), {
update(ww, place, duration) {
const r = ww <= 480 ? 6 : 7;
const margin = {
left: 45,
right: ww <= 480 ? 102 : ww <= 768 ? 114 : r + 1,
top: 32,
bottom: 8
};
const size = Math.min(420, ww);
const width = size - margin.left - margin.right;
const height = Math.min(300, size * 0.75) - margin.top - margin.bottom;
x.range([0, width]);
y.range([height, 0]);
const labelData = data
.filter(d => d.days === 7)
.map(d => (d.y = y(d.mean_error), d));
const dodged = dodge(labelData.map(d => d.y), ww <= 480 ? 16 : 20);
labelData.forEach((d, i) => (d.y = dodged[i], d));
svg
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
g
.attr("transform", `translate(${[margin.left, margin.top]})`);
xaxis
.call(g => xaxisGenerator(g, ww, x, height));
yaxis
.call(g => yaxisGenerator(g, y, width, height, margin));
dot
.attr("cx", d => x(d.days))
.attr("cy", d => y(d.mean_error))
.attr("r", r);
label
.data(labelData)
.attr("dx", r + 5)
.attr("dy", r - 2)
.attr("fill", d => stroke(d.season))
.attr("x", width)
.attr("y", d => d.y)
.text(d => seasonText(d.season));
}
})
}