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

标签 javascript d3.js data-visualization

我正在关注 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 的 ,但跨度根本没有显示在图表上。 <circle><span>example id</span></circle>

最佳答案

您不是将文本附加到 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));

现在由于节点包含了 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 附加 svg 元素,而不是 html 元素。虽然可以附加 span 标签,但它不会显示,因为它们不是 svg 元素。

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

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

这是一个 demo .

请注意,要查看您的链接,您需要设置描边颜色:.attr("stroke","black'); 在您的行上

关于javascript - 圆圈标签未显示在 d3 数据可视化中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49882951/

相关文章:

javascript - 将 Google 帐户与在 Parse.com 中使用电子邮件创建的现有帐户相关联

javascript - 尝试使用 async/await 而不是 browser.sleep,但它不起作用

javascript - 无法在 D3 生成的 SVG 文本中打断长句

javascript - 使用带有react-chartjs-2的chartjs-chart-geo插件实现等值线图

python - 使用 Seaborn、Pandas 绘制高低图

pandas - 如何对 pandas 时间戳进行分组,在一张图中绘制多个图并将它们在 matplotlib 中堆叠在一起?

javascript - 简单的 Angular 单选按钮不起作用......看起来坏了?

javascript - D3访问html属性

javascript - d3 过渡在多个浏览器中不起作用

javascript - 为什么 javascript 中的 try/catch 在 IE 中失败