【问题标题】:d3.js Force Directed Tree with Labelsd3.js 带标签的强制定向树
【发布时间】:2014-06-06 22:34:33
【问题描述】:

我完全无法在http://bl.ocks.org/mbostock/1138500 找到的力导向树形图中添加标签

我已尝试将力导向树与其他示例(包括标签以及遵循Add text label to d3 node in Force directed Graph and resize on hover 的答案)合成,但图表似乎总是中断。

此代码适用于带有标签和图片的力有向图

    <!DOCTYPE html>
<meta charset="utf-8">
<style>

.link {
  stroke: #ccc;
}

.node text {
  pointer-events: none;
  font: 10px sans-serif;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 500

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

var force = d3.layout.force()
    .gravity(.05)
    .distance(100)
    .charge(-100)
    .size([width, height]);

d3.json("graph.json", function(error, json) {
  force
      .nodes(json.nodes)
      .links(json.links)
      .start();

  var link = svg.selectAll(".link")
      .data(json.links)
    .enter().append("line")
      .attr("class", "link");

  var node = svg.selectAll(".node")
      .data(json.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("image")
      .attr("xlink:href", "https://github.com/favicon.ico")
      .attr("x", -8)
      .attr("y", -8)
      .attr("width", 16)
      .attr("height", 16);

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

  force.on("tick", function() {
    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 + ")"; });
  });
});

</script>

但是,当我尝试修改它以从 Mike 的示例中形成树结构时,我的代码看起来像这样,但不起作用。

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.link {
  stroke: #ccc;
}

.node text {
  pointer-events: none;
  font: 10px sans-serif;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 500

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

var force = d3.layout.force()
    .gravity(.05)
    .distance(100)
    .charge(-100)
    .size([width, height]);

d3.json("test.json", function(error, json) {
  force
      .nodes(json.nodes)
      .links(json.links)
      .start();

  var link = svg.selectAll(".link")
      .data(json.links)
    .enter().append("line")
      .attr("class", "link");

  var node = svg.selectAll(".node")
      .data(json.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("image")
      .attr("xlink:href", "https://github.com/favicon.ico")
      .attr("x", -8)
      .attr("y", -8)
      .attr("width", 16)
      .attr("height", 16);

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

  force
      .nodes(json.nodes)
      .links(json.links)
      .on("tick", tick)
      .start();

  function tick(e) {

    // Push sources up and targets down to form a weak tree.
    var k = 6 * e.alpha;
    json.links.forEach(function(d, i) {
      d.source.y -= k;
      d.target.y += k;
    });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });

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

</script>

我已经尝试并尝试解决此问题,但无法将标签与力有向图结合起来,非常感谢您提供任何帮助,我已经为此苦苦挣扎了一段时间......

谢谢!

【问题讨论】:

    标签: javascript d3.js tree label force-layout


    【解决方案1】:

    您的示例代码中缺少的部分是对tick 回调的添加,该回调会减少源的y 值,并且每次都会少量增加目标的值。

    这是一个jsfiddle example,我认为它可以满足您的需求。

    关键部分是在tick 函数中添加一个参数,此处称为e,以及行

    var k = 6 * e.alpha;
    json.links.forEach(function(d, i) {
      d.source.y -= k;
      d.target.y += k;
    });
    

    结果看起来像这样,一旦你也增加了电荷的大小以将节点彼此推得更远一点:

    【讨论】:

    • 非常感谢,这太完美了!我花了几个小时来解决这个问题,但你的解释很有道理,干杯!
    • 不用担心,很乐意为您提供帮助。欢迎使用 StackOverflow!
    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 2015-05-05
    • 2013-06-25
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多