tempChart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("path")
.datum(data)
.attr("fill", "steelblue")
.attr("opacity", 0.2)
.attr(
"d",
d3
.area()
.x(d => x(d.date))
.y0(d => y(d.min_temperature_200cm))
.y1(d => y(d.max_temperature_200cm))
);
svg
.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr(
"d",
d3
.line()
.x(d => x(d.date))
.y(d => y(d.mean_temperature))
);
const grid = svg
.append("g")
.attr("stroke", "black")
.attr("stroke-opacity", 0.1);
grid
.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom);
grid
.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right);
svg
.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(xAxis)
.call(g => g.select(".domain").remove());
svg
.append("g")
.attr("transform", `translate(${width - margin.right},0)`)
.call(yAxis)
.call(g => g.select(".domain").remove())
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", -3)
.attr("text-anchor", "end")
.attr("font-weight", "bold")
.text("temperature (°C)")
);
const marker = svg.append("g").style("display", "none");
const markerLine = marker
.append("line")
.attr("stroke", "black")
.attr("stroke-opacity", 0.8)
.attr("y2", -(height - margin.top - margin.bottom));
const markerDateLabel = marker
.append("text")
.attr("y", 30)
.attr("text-anchor", "middle")
.attr("font-size", 12);
const markerValue = marker
.append("circle")
.attr("r", 3)
.attr("fill", "red");
svg.on("touchmove mousemove", function(event) {
const item = bisect(d3.pointer(event, this)[0]);
marker
.style("display", null)
.attr(
"transform",
`translate(${x(item.date)},${height - margin.bottom})`
);
markerDateLabel.text(formatDate(item.date));
markerValue.attr("cy", y(item.mean_temperature));
});
svg.on("touchend mouseleave", () => {
marker.style("display", "none");
});
return svg.node();
}