gpt4 book ai didi

javascript - D3 Sankey 拖动函数中未捕获的类型错误

转载 作者:行者123 更新时间:2023-11-29 14:41:19 25 4
gpt4 key购买 nike

我创建了一个 d3 Sankey 图。我面临的问题是我无法拖动节点。这是节点的创建方式:

 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));

而dragmove函数如下:

 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);
}

但是每次我运行我的代码时,它都会抛出这个错误:未捕获的类型错误:无法读取 null 的属性“目标”

我做错了什么?

If I click on the error, it takes me to a blank line on the index page

最佳答案

错误是由于脚本在程序中的位置而引发的。在将图表集成到包含许多其他内容的页面中时,我将脚本放在了似乎导致问题的头部部分。现在,我把它放在最后,一切正常。很抱歉张贴了这个,我当时很傻。

  <html>
<style><!-- Style for the Sankey graph -->
</style>
<body>
<div id="sankey-graph">
</div>
<script><!-- Script for the Sankey graph -->
</script>
</body>
</html>

我猜这是正确的方法。我最初将我的脚本放在包含桑基图的 div 之前。

关于javascript - D3 Sankey 拖动函数中未捕获的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37991532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com