[英]Uncaught TypeError in D3 Sankey Drag Function
I have created a d3 Sankey graph. 我创建了一个d3 Sankey图。 The issue that I'm facing is that I am unable to drag the nodes.
我面临的问题是我无法拖动节点。 This is how the node is created:
这是节点的创建方式:
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click",highlight_node_links)
.call(d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() { this.parentNode.appendChild(this); })
.on("drag", dragmove));
And the dragmove function is as follows: 拖拉功能如下:
function dragmove(d) {
d3.select(this).attr("transform",
"translate(" + (
d.x = Math.max(0, Math.min(width - d.dx, d3.event.x))
) + "," + (
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", path);
}
But everytime I run my code, it throws this error: Uncaught TypeError: Cannot read property 'target' of null 但每次我运行我的代码时,它都会抛出此错误: 未捕获的TypeError:无法读取null的属性“target”
What am I doing wrong? 我究竟做错了什么?
The error was being thrown because of the placement of the script in the program. 由于脚本在程序中的位置而引发错误。 While integrating the graph into a page with a lot of other content, I placed the script in the head section which seemed to cause the issue.
在将图形集成到包含许多其他内容的页面时,我将脚本放在头部区域,这似乎导致了问题。 Now that, I placed it near the end, everything works fine.
既然如此,我把它放在了最后,一切正常。 I'm sorry to have posted this, I was being silly.
我很抱歉发布了这个,我很傻。
<html>
<style><!-- Style for the Sankey graph -->
</style>
<body>
<div id="sankey-graph">
</div>
<script><!-- Script for the Sankey graph -->
</script>
</body>
</html>
This is the correct way I guess. 这是我猜的正确方法。 I had initially put my script before the div that contained sankey graph.
我最初把我的脚本放在包含sankey图的div之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.