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 xaxisLabel = g.append("text")
.attr("class", "axis-label x-axis-label");
const yaxisLabel = g.append("text")
.attr("class", "axis-label y-axis-label");
const circle = g.selectAll("circle")
.data(data)
.join("circle")
.attr("fill", d => color(d.city));
const annotation = g.selectAll(".annotation")
.data(annotations)
.join("text")
.attr("class", "annotation")
.attr("fill", d => color(d.city))
.text(d => d.city);
return Object.assign(svg.node(), {
resize(size){
const r = 2;
const margin = { left: 56, right: 20, top: 32, bottom: 48 };
const width = size - margin.left - margin.right;
const height = Math.max(400, size) - margin.top - margin.bottom;
const offset = 33;
x.range([0, width]);
y.range([height, 0]);
xaxisGenerator.tickSize(height + 10);
yaxisGenerator.tickSize(width + 10);
svg
.attr("width", size)
.attr("height", height + margin.top + margin.bottom);
g
.attr("transform", `translate(${[margin.left, margin.top]})`);
xaxis
.call(xaxisGenerator);
yaxis
.attr("transform", `translate(${width})`)
.call(yaxisGenerator);
const tick = yaxis.select(".tick:last-of-type");
tick.select("text").attr("x", -(yaxisGenerator.tickSize() + yaxisGenerator.tickPadding()) + offset)
tick.select("line").attr("x2", -yaxisGenerator.tickSize() + offset);
xaxisLabel
.attr("y", height + 40)
.html(`<tspan dx=-14 dy=4>Daily maximum temperature</tspan><tspan class="arrow" dx=4 dy=-1>→</tspan>`);
yaxisLabel
.attr("x", -margin.left)
.attr("y", -margin.top + 8)
.html(`<tspan dx=10 dy=4>Daily electricity demand</tspan><tspan class="arrow" x=${-margin.left} dy=-3>↑</tspan>`);
circle
.attr("cx", d => x(d.tmax))
.attr("cy", d => y(d.demand))
.attr("r", r);
annotation
.attr("text-anchor", d => d.textAnchor(size))
.attr("x", d => x(d.x(size)))
.attr("y", d => y(d.y(size)));
}
});
}