未在d3数据可视化中显示的圆圈标签

实践完美

我正在按照本教程创建类似节点的数据可视化。

<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

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

var color = d3.scaleOrdinal(d3.schemeCategory20);

var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.id; }))
    .force("charge", d3.forceManyBody())
    .force("center", d3.forceCenter(width / 2, height / 2));

d3.json("miserables.json", function(error, graph) {
  if (error) throw error;

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

  var node = svg.append("g")
      .attr("class", "nodes")
    .selectAll("circle")
    .data(graph.nodes)
    .enter().append("circle")
      .attr("r", 5)
      .attr("fill", function(d) { return color(d.group); })
      .call(d3.drag()
          .on("start", dragstarted)
          .on("drag", dragged)
          .on("end", dragended));

  node.append("span")
      .text(function(d) { return d.id; });

  simulation
      .nodes(graph.nodes)
      .on("tick", ticked);

  simulation.force("link")
      .links(graph.links);

  function ticked() {
    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; });
  }
});

function dragstarted(d) {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function dragged(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}

</script>

但是,我想在每个圆旁边加上ID /名称加上额外的元素,g但由于某种原因没有任何显示。

当我检查每个圆圈时。它显示了具有正确ID的,但跨度根本没有显示在图表上。<circle><span>example id</span></circle>

安德鲁·里德(Andrew Reid)

您没有在g元素上添加文本,而是在圆上添加了文本,让我们按照您的代码查看变量的含义node

  var node = svg.append("g")    // returns a selection holding a g
    .attr("class", "nodes")     // returns the same g, now with class "nodes"
    .selectAll("circle")        // returns an empty selection as there are no circles yet
    .data(graph.nodes)          // returns the empty selection with bound data
    .enter().append("circle")   // returns a selection of newly entered circles
      .attr("r", 5)             // continues to return the circles
      ...

因此node保留了一组圆形,因此在使用时node.append("span")我们尝试将文本添加到圆形,这将不起作用。因此,我们需要通过中断链接来对它进行一些修改:

var node = svg.append("g")  
  .attr("class", "nodes")
  .selectAll("circle")
  .data(graph.nodes)
  .enter().append("g");  // returns a selection of newly entered g elements

node.append("circle")   // returns a selection of circles, newly append to each g in node
  .attr("r", 5)
  .attr("fill", function(d) { return color(d.group); })
  .call(d3.drag()
      .on("start", dragstarted)
      .on("drag", dragged)
      .on("end", dragended));

现在,由于node拥有g个元素的选择,因此我们也可以附加文本:

  node.append("text") // returns a selection of text, newly append to each g in node
      .text(function(d) { return d.id; });

这里也有变化,我将node.append(“ span”)更改为node.append(“ text”),我们将svg元素而不是html元素附加到svg。尽管可以附加span标签,但由于它们不是svg元素,因此不会显示。

最后,就像在此答案中一样,您需要更新g每个刻度的每个翻译,而不是cxandcy属性,因为ag不是由cx和cy属性定位的:

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

这是一个演示

注意,就看你的链接,你需要设置笔触颜色:.attr("stroke","black');您行

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章