chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg.append("g").call(xAxis);
svg.append("g").call(yAxis);
svg
.append("g")
.attr("stroke", "none")
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", d => x(d.date))
.attr("cy", d => y(+d.value))
.attr("fill", d => z(+d.anomaly))
.attr("r", 5.5)
.on("mouseover", function(d) {
d3.select(this)
.attr("stroke", "orange")
.style("cursor", "pointer");
let x_range = x.range();
let middle = (x_range[1] - x_range[0]) / 2;
let x_offset = 15;
if (d.x > middle) x_offset = -180;
let tooltip = d3
.select("body")
.append("div")
.attr("class", "svg-tooltip")
.style("position", "absolute")
.style("visibility", "visible")
.html(
"Temperature: " +
d.target.__data__.value +
" ℉" +
"<br>" +
"Year: " +
formatTime(d.target.__data__.date)
)
.style("top", d.pageY - 10 + "px")
.style("left", d.pageX + x_offset + "px");
})
.on("mouseout", function(d) {
d3.selectAll(".svg-tooltip").remove();
d3.select(this).attr("stroke", "none");
});
svg
.append("path")
.datum(regression(data))
.style("fill", "none")
.style("stroke", "gray")
.style("stroke-width", "2px")
.attr("d", line);
return svg.node();
}