javascript - 在 D3 中标记网络节点

标签 javascript d3.js

我需要用D3绘制除节点之外的标签(名称)。这是我的代码:

<script src="//d3js.org/d3.v3.min.js"></script>
<script>

var width = window.innerWidth,
    height = 400;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(40)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .call(d3.behavior.zoom().on("zoom", redraw))
        .append('g');

    function redraw() {
      svg.attr("transform",
          "translate(" + d3.event.translate + ")"
          + " scale(" + d3.event.scale + ")");}

          var drag = force.stop().drag()
          .on("dragstart", function(d) {
            d3.event.sourceEvent.stopPropagation(); 
          });

d3.json("/static/net.json", function(error, graph) {
  if (error) throw error;

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

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", function(d) { return d.degree; })
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

  node.append("title")
      .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("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });

});

</script>

出于某种原因,只有当我将鼠标悬停在节点上时,标签才会出现,而不是始终被绘制。

当我更换时:

node.append("title")
.text(function(d) { return d.name; })

与:

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

即使悬停时也根本不会出现标签。

这段代码中是否有明显的我遗漏的东西?

最佳答案

问题是您正在尝试将文本元素附加到圆圈中。

var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", function(d) { return d.degree; })
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

 // node here consists of svg circles.
 node.append("text")
      .text(function(d) { return d.name; })

试试这个

var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag); // moved this here

node.append("circle")
    .attr("r", function(d) { return d.degree; })
    .style("fill", function(d) { return color(d.group); })
    // .call(force.drag);

 // node here is a `g` element so we can append text elements to it.
 node.append("text")
      .text(function(d) { return d.name; })

我还将 call(force.drag) 移至 .attr("class", "node") 行之后,以便将其应用于给定 node 元素的所有子元素。

关于javascript - 在 D3 中标记网络节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35440823/

相关文章:

javascript - html 在单击按钮时从 json 数据加载更多

php - 连接postgres和php,使用d3.js可视化

javascript - 更新 d3 中的图表

javascript - 如何覆盖 d3.js 对可渲染元素数量的限制?

javascript - 未捕获的 TypeError : d3. schemeCategory20 不是函数

javascript - ESLint:如何在配置文件中指定可以自动修复(使用--fix)的规则?

javascript - 为什么带有 javascript 验证的 jsf 表单不转发 View ?

javascript - 创建动态全屏和最小化 Div 函数

javascript - jQuery 替代 .keyup 函数

javascript - 当我在移动设备上上下滚动时,我的技能图表不会消失