我有这个代码:
var stage = d3.select("body").append("svg");
var points = [0, 50, 100, 150];
var letters = "drag".split(",");
var log = d3.select("#log");
//drag behaviour, will bind later
var drag = d3.behavior.drag()
.origin(Object)
.on("drag", dragmove);
//append texts
var texts = stage
.selectAll("text")
.data(letters)
.enter()
.append("text")
.attr("font-family", "Arial")
.attr("class", "word")
.attr("x", function(d,i){return points[i]})
.attr("y", 100)
.text(function(d){return d})
.call(drag);
function dragmove() {
log.text(
"x: " + d3.event.x + ", " +
"y: " + d3.event.y + ", " +
"dx: " + d3.event.dx + ", " +
"dy: " + d3.event.dy
);
d3.select(this).attr("x", d3.event.x);
}
但是,d3.event.x
并且d3.event.y
正在返回NaN
。有趣的是,d3.event.dx
并且d3.event.dy
正在返回正确的值。我的代码有什么问题?
你可以在这里看到一个 jsfiddle:http: //jsfiddle.net/UMwmr/