【问题标题】:labels for circles not showing up in d3 data visualization未在 d3 数据可视化中显示的圆圈标签
【发布时间】:2018-04-17 16:15:51
【问题描述】:

我正在关注this tutorial 创建一个类似节点的数据可视化。

<!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/name 和额外的元素 g,但由于某种原因没有显示任何内容。

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

【问题讨论】:

  • @AndrewReid 这是一个类似的问题,但我不明白答案。从我上面添加的代码中可以看出,我确实有 g 元素。当我检查每个圆圈时,id/name 可以被视为 但现在显示出来了。
  • @AndrewReid 是的,对不起,我重新编辑了标题和内容以更好地描述问题。

标签: javascript d3.js data-visualization


【解决方案1】:

您没有将文本附加到 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 元素。

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

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

这是demo

注意,要查看您的链接,您需要在线条上设置笔触颜色:.attr("stroke","black');

【讨论】:

  • 没问题,很高兴为您提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多