簡體   English   中英

在D3中標記網絡節點

[英]Labeling network nodes in D3

除了帶有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元素的所有子級。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM