我在 d3 中使用力导向布局,但在开发过程中遇到了一些障碍。
var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle")
.attr("r", function( d ) {
return d.fValue;
})
.style('fill', function( d ) {
return strokeColor( d.name );
})
.call(force.drag);
基本上,我想向 force.drag 定义的“拖动”行为添加更多事件监听器 - 也就是说,我想确保节点在拖动时改变颜色(而不是在鼠标悬停时)。我能想到的唯一两种方法是以某种方式更改 force.drag 函数或定义新的拖动行为。
我不知道如何使用第一种方式,但是当我尝试第二种方式时,方法链将只接受第二个拖动事件,而忽略第一个 force.drag 事件。
.call(customDrag)
.call(force.drag); // This would work
如何附加另一个拖动事件监听器或修改现有的 force.drag 以适应我要添加的新动画?
提前致谢
最佳答案
定义你自己的监听器,因为 force.drag
只调用 force.tick()
afaik:
var node_drag = d3.behavior.drag()
.on("dragstart", dragstart)
.on("drag", dragmove)
.on("dragend", dragend);
vis.selectAll("g.node").call(node_drag)
然后确保在 dragmove
中调用 tick()
。
我这样做是为了通过强制布局添加拖放支持。 see my stackoverflow question & answer for an example
关于javascript - 两个 'calls' 到 d3 中的拖动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297399/