简体   繁体   English

d3拖动事件导致TypeError

[英]d3 drag event causes TypeError

I am trying to enable dragging in my chart, but I only got dragstart and dragend events. 我正在尝试在图表中启用拖动,但是我只遇到了dragstart和dragend事件。

something strange is that if I use the .origin method as in the examples, it throws an exception (Uncaught TypeError: Cannot read property 'x' of undefined ): 奇怪的是,如果我在示例中使用.origin方法,它将引发异常(Uncaught TypeError:无法读取undefined的属性'x'):

this._drag = d3.behavior.drag()
//.origin(function(d) { return d;}) //if uncommented, throws error!
.on("drag", function(d){console.log("drag!", d); self.on_drag(this);})
.on("dragstart", function(){self.on_drag_start(this);})
.on("dragend", function(){self.on_drag_end(this);})
;

the problem with origin was caused by the fact that I was trying to drag a svg element that was not part of the chart itself, but a decorator item (to emulate a icon) that I added separately. 源问题是由于我试图拖动一个svg元素而导致的,该元素不是图表本身的一部分,而是我单独添加的装饰器项(以模拟图标)。 thus, the element has not a datum (d is undefined) but if you return a custom object with x and y attributes, you can use the drag behaviour normally. 因此,该元素没有基准(d未定义),但是如果返回具有x和y属性的自定义对象,则可以正常使用拖动行为。

thanks to the people on the #d3.js irc channel for helping me to figure this out! 感谢#d3.js irc频道上的人员帮助我解决了这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM