{
const width = 500, height = 400, margin = 50;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
const xScale = d3.scaleLinear()
.domain([0, d3.max(data, d => d.x) + 1])
.range([margin, width - margin]);
const yScale = d3.scaleLinear()
.domain([-d3.max(data, d => d.y2) - 5, d3.max(data, d => d.y1) + 5])
.range([height - margin, margin]);
svg.append("g")
.attr("transform", `translate(0, ${height - margin})`)
.call(d3.axisBottom(xScale).ticks(5));
svg.append("g")
.attr("transform", `translate(${margin}, 0)`)
.call(d3.axisLeft(yScale).ticks(10)
.tickFormat(d => Math.abs(d))
);
svg.selectAll(".tick line").attr("stroke", "none");
svg.append("line")
.attr("x1", xScale(0))
.attr("x2", xScale(0))
.attr("y1", margin)
.attr("y2", height - margin)
.attr("stroke", "black")
.attr("stroke-width", 1.5)
.attr("stroke-dasharray", "4,2");
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("background", "#fff")
.style("border", "1px solid black")
.style("padding", "5px")
.style("border-radius", "5px")
.style("visibility", "hidden")
.style("opacity", 0);
svg.selectAll(".y1-points")
.data(data)
.enter()
.append("circle")
.attr("cx", d => xScale(d.x))
.attr("cy", d => yScale(d.y1))
.attr("r", 5)
.attr("fill", "blue")
.on("mouseover", function(event, d) {
tooltip.transition().duration(200).style("opacity", .9);
tooltip.html(`(${d.x}, ${d.y1})`)
.style("visibility", "visible")
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px");
})
.on("mouseout", function() {
tooltip.transition().duration(200).style("opacity", 0);
});
svg.selectAll(".y2-points")
.data(data)
.enter()
.append("circle")
.attr("cx", d => xScale(d.x))
.attr("cy", d => yScale(-d.y2))
.attr("r", 5)
.attr("fill", "red")
.on("mouseover", function(event, d) {
tooltip.transition().duration(200).style("opacity", .9);
tooltip.html(`(${d.x}, ${d.y2})`)
.style("visibility", "visible")
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px");
})
.on("mouseout", function() {
tooltip.transition().duration(200).style("opacity", 0);
});
return svg.node();
}