javascript - 在力向图 d3 中拖动整个节点

标签 javascript d3.js

我在 d3 中使用强制定向布局来绘制此图。我看过这篇文章http://bl.ocks.org/eyaler/10586116但我似乎很难知道如何应用它。这是我到目前为止所做的事情。

 var width = 960,
 height = 500,
 root;

 var force = d3.layout.force()
.linkDistance(90)
.charge(-1000)
.gravity(.100)
.size([width, height])
.on("tick", tick);

 var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var link = svg.selectAll(".link"),
node = svg.selectAll(".node");

var div = d3.select("body")
.append("div")  // declare the tooltip div 
.attr("class", "tooltip")
.style("opacity", 0);

d3.json("graph.json", function(error, json) {
root = json;
update();
});

  function update() {
  var nodes = flatten(root),
  links = d3.layout.tree().links(nodes);

//重新启动强制布局。

  force
  .nodes(nodes)
  .links(links)
  .start();

//更新链接。

 link = link.data(links, function(d) { return d.target.id; });

 link.exit().remove();

 link.enter().insert("line", ".node")
  .attr("class", "link")
  .style("stroke", function(d) { return d.target.level; });

//更新节点。

  node = node.data(nodes, function(d) { return d.id; });

  node.exit().remove();

  var nodeEnter = node.enter().append("g")
  .attr("class", "node")
  .on("click", click)
  .call(force.drag);

  var defs = node.append("defs").attr("id", "imgdefs");

  var imgPattern = defs.append("pattern")
  .attr("id", "imgPattern")
  .attr("height", 30)
  .attr("width", 30)
  .attr("x", "0")
  .attr("y", "0");

  imgPattern.append("image")
  .attr("height", 30)
  .attr("width", 30)
  .attr("xlink:href", function(d) { return d.image; });

  node.select("circle")
  .style("fill", "url(#imgPattern)");

  nodeEnter.append("circle")
  .attr("r", 15)
  .style("stroke",color)
  .style("stroke-width", 4)
  .style("fill", "url(#imgPattern)")
  .on("mouseover", function(d) {

                  circlePos = this.getBoundingClientRect();

                  div.transition()
                      .duration(500)
                      .style("opacity", 1)
                      .style("cursor","pointer")
                      .style("left", (circlePos.left + circlePos.width+window.scrollX) + 'px')
                      .style("top", (circlePos.top + window.scrollY) + 'px')
                      .style("position", "absolute")

                        div.html(
                          "<div style='position: absolute; border:2px solid red; background-color: yellow; z-index=-1'>" +
                            "<div class='container'>" +
                              "<div id='wrap'>" +
                                "<div class='col-md-3'>" +
                                  "<div class='g-hover-card'>"  +                                   
                                      "<div class='user-avatar'>" +
                                        "<img src='"+ d.image +"' alt='' style='margin-top:50px;'/>" +
                                      "</div>" +
                                      "<div class='info'>" +
                                        "<div class='description'>" +
                                          "<font size='5px' color='black'>" + d.name + "</font>" +

                                        "</div>" +  
                                      "</div>" +
                                      "<div class='bottom'>" +
                                        "<br/>" +
                                        "<a id='thisLink' onclick=\"window.open('http://facebook.com/" + d.pid  + "');\" >" + "Follow on Facebook" +
                                        "</a>" +
                                      "</div>" +
                                  "</div>" +
                                "</div>" +
                              "</div>" +
                            "</div>" +
                          "</div>"
                        )

                      })

                    .on("mouseout", function(d) {
                        div.transition()
                            .duration(500)
                            .style("opacity", 0)
                            .each('end', function(d) {
                                d3.select(this).html("");
                            });
                    });

                    div.on("mouseover", function(d) {
                        d3.select(this).transition()
                            .duration(500)
                            .style("opacity", .9);

                    }).on("mouseout", function(d) {
                        d3.select(this).transition()
                            .duration(500)
                            .style("opacity", 0)
                            .each('end', function(d) {
                                d3.select(this).html("");
                            });
                    }); 

  nodeEnter.append("text")
  .attr("dy", ".35em")
  .text(function(d) { return d.name; });


  }

  function tick() {
  link.attr("x1", function(d) { return d.source.x; })
  .attr("y1", function(d) { return d.source.y; })
  .attr("x2", function(d) { return d.target.x; })
  .attr("y2", function(d) { return d.target.y; });

   node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
   }

   function color(d) {
   return d._children ? "#800000" // collapsed package
   : d.children ? "#008000" // expanded package
  : "#68228b"; // leaf node

}

 // Toggle children on click.
      function click(d) {
      if (d3.event.defaultPrevented) return; // ignore drag
      if (d.children) {
      d._children = d.children;
      d.children = null;
      } else {
      d.children = d._children;
      d._children = null;
      }
      update();
      }

//返回根下所有节点的列表。

     function flatten(root) {
     var nodes = [], i = 0;

     function recurse(node) {
     if (node.children) node.children.forEach(recurse);
     if (!node.id) node.id = ++i;
     nodes.push(node);
     }

     recurse(root);
     return nodes;
     }

我想拖动所有节点,因为我将把 iframe 滚动设置为“否”。

最佳答案

尝试对“selectAll”的返回值调用拖动,而不是“append”

关于javascript - 在力向图 d3 中拖动整个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26291521/

相关文章:

javascript - 使用包含函数解析 JSON

javascript - D3负值条形图

javascript - 网站呈现问题 : Safari 4 displays flash of content, 然后白屏

c# - MVC 3 复选框关闭不使用RequiredAttribute 的验证

javascript - 在knockout.js中,是否可以使用动态绑定(bind)值?

javascript - 仅当数字有小数位时才将数字格式化为 2 位小数

javascript - 如何在图表条上方显示值标签?

d3.js - 你如何用 d3.js 在散点图中绘制线性线

javascript - 嵌套的javascript函数丢失 "this"

javascript - AngularJS 对自定义指令的单向绑定(bind)