【问题标题】:Increase link length in node-link graph using d3使用 d3 增加节点链接图中的链接长度
【发布时间】:2019-09-04 18:36:33
【问题描述】:

如何增加力图的链接长度。我的代码写在下面。我应该改变什么?

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script>
  var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

  // Add lines for every link in the dataset
  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);
    });

  // Add circles for every node in the dataset
  var node = svg.append("g")
    .attr("class", "nodes")
    .selectAll("circle")
    .data(graph.nodes)
    .enter().append("circle")
    //   .style("fill", function (d) { return '#1f77b4'; })
    .attr("r", 10)
    .attr("fill", function(d) {
      return '#aec7e8';
    })
    // .attr("fill", function(d) { return color(d.group); })
    .call(d3.drag()
      .on("start", dragstarted)
      .on("drag", dragged)
      .on("end", dragended)
    );

  // Basic tooltips
  node.append("title")
    .text(function(d) {
      return d.id;
    });

  // Attach nodes to the simulation, add listener on the "tick" event
  simulation
    .nodes(graph.nodes)
    .on("tick", ticked);

  // Associate the lines with the "link" force
  simulation.force("link")
    .links(graph.links)
</script>

我希望读者能够清楚地看到可视化。这是因为节点的可视化彼此非常接近。

【问题讨论】:

  • 您的代码似乎不完整。你能用上面的代码生成力图吗?
  • 我尝试显示完整的 html 代码,但 stackoverflow 给出了错误注释
  • 您似乎在下面找到了答案。请将下面提供的解决方案标记为答案,如果它解决了您的问题,请记得给它投票。

标签: javascript json d3.js svg visualization


【解决方案1】:

尝试将.strength()d3.forceManyBody() 更改为更大的值


.force("charge", d3.forceManyBody().strength(-3));


.force("charge", d3.forceManyBody().strength(-30));

【讨论】:

  • @NurHidayah 很高兴为您提供帮助
  • 您知道如何突出显示仅连接到特定节点的节点吗?我尝试了一些代码,但它不起作用。
  • @NurHidayah 是的,但您需要将此信息添加到节点
  • @NurHidayah 是的,关于节点的信息需要被高亮,看这里的 sn-p,我刚刚纠正了它以展示你的情况ru.stackoverflow.com/a/969307/188366
猜你喜欢
  • 1970-01-01
  • 2012-10-31
  • 2013-06-15
  • 1970-01-01
  • 2023-03-20
  • 2020-03-25
  • 1970-01-01
  • 2017-01-15
  • 1970-01-01
相关资源
最近更新 更多