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

Uncaught TypeError in D3 Sankey Drag Function

我创建了一个 d3 桑基图。我面临的问题是我无法拖动节点。 节点是这样创建的:

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

但是每次我 运行 我的代码都会抛出这个错误: 未捕获类型错误:无法读取 属性 'target' of null

我做错了什么?

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

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

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