将"mouseover"事件绑定到d3.js中一行上的点

vgo*_*ani 4 visualization d3.js

我想通过使用以下代码单击该行来获取某一点上的点的坐标:

var lineData = [ { "x": 1,   "y": 5},  { "x": 20,  "y": 20},
                 { "x": 40,  "y": 10}, { "x": 60,  "y": 40},
                 { "x": 80,  "y": 5},  { "x": 100, "y": 60}];

var lineFunction = d3.svg.line()
    .x(function(d) { return d.x; })
    .y(function(d) { return d.y; })
    .interpolate("linear");

var svgContainer = d3.select("body").append("svg")
    .attr("width", 200)
    .attr("height", 200);

var lineGraph = svgContainer.append("path")
    .data([lineData]).attr("d", lineFunction)
  //.attr("d", lineFunction(lineData))  
    .attr("stroke", "blue")
    .attr("stroke-width", 2)
    .attr("fill", "none")
    .on('mousedown', function(d) {
        console.log({"x":d.x, "y":d.y})
    });
Run Code Online (Sandbox Code Playgroud)

(我更新了代码以解决注释,但我仍然得到"Object {x:undefined,y:undefined}")

点击该行时,我不断得到"未定义".我错过了一步吗?

Lar*_*off 6

您可以使用以下方式获取事件的坐标d3.event:

.on("mousedown", function() {
    console.log({"x": d3.event.x, "y": d3.event.y});
});
Run Code Online (Sandbox Code Playgroud)