简体   繁体   English

使用 D3.js 修复力图节点的位置

[英]Fix position of force-graph node using D3.js

I'm using a force-graph D3 visualization from https://github.com/vasturiano/force-graph .我正在使用来自https://github.com/vasturiano/force-graph force-graph D3 可视化。 This seems to provide great high-level API to create a force directed graph but I find it hard to customise as I'm new to Javascript.这似乎提供了很好的高级 API 来创建力有向图,但我发现很难自定义,因为我是 Javascript 的新手。

<script src="//unpkg.com/force-graph"></script>
<script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>

<script>

fetch('data.json').then(res => res.json()).then(data => {
      const Graph = ForceGraph()
      (document.getElementById('graph'))
        .graphData(data)
        .nodeAutoColorBy('group')
        .linkSource('source')
        .linkTarget('target')
})

</script>

The JSON data looks like this: JSON 数据如下所示:

{
  "nodes": [
    {"id": "Myriel", "group": 1},
    {"id": "Napoleon", "group": 1}
  ],
  "links": [
    {"source": "Napoleon", "target": "Myriel", "value": 1},
    {"source": "Mlle.Baptistine", "target": "Myriel", "value": 8}
  ]
}

I am trying to fix the position of a particular node, for example id = Myriel to x=500 , y=500 .我正在尝试修复特定节点的位置,例如id = Myrielx=500 , y=500 According to d3 force documentation , I need to specify fx and fy .根据 d3 force文档,我需要指定fxfy How do I retrieve the id of the particular node to set the those attributes?如何检索特定节点的 id 以设置这些属性? Any help is greatly appreciated!任何帮助是极大的赞赏!

I have found a solution:我找到了一个解决方案:

.nodeVal(node => {         
  node.fx = 500;
  node.fy = 500
})

Using the nodeVal method, I can access the attributes of the nodes to fix it.使用nodeVal方法,我可以访问节点的属性来修复它。 Adding a if statement would allow me to change it only if it the specific nodes I need.添加if语句将允许我仅在它是我需要的特定节点时对其进行更改。 Thanks for everyone's help!感谢大家的帮助!

The API link that you've provided is really beautiful.您提供的 API 链接非常漂亮。 It's such a complex chart to work with and it ticks almost every box.这是一个非常复杂的图表,几乎每个方框都打勾。

However, it looks like the only way to adapt it to YOUR needs would be to hack into the js code which I would not advise.但是,看起来使它适应您的需求的唯一方法是侵入我不建议的 js 代码。

Personally I would start with a simpler d3 version and customise from here.我个人会从一个更简单的 d3 版本开始,然后从这里进行自定义。

For example: https://observablehq.com/@d3/force-directed-graph例如: https : //observablehq.com/@d3/force-directed-graph

Here, you'd need to customise the simulation.on("tick" function. Something like this:在这里,您需要自定义 simulation.on("tick" 函数。像这样:

node
    .attr("cx", d => d.x)
    .attr("cy", d => d.y)
    **.attr("fx", d => d.id === "Myrial" ? 500 : null)**
    **.attr("fy", d => d.id === "Myrial" ? 500 : null);**

` `

OR for a similar approach to your example you could use Vega.或者对于与您的示例类似的方法,您可以使用 Vega。 That allows you to use fx and fy:这允许您使用 fx 和 fy:

https://vega.github.io/vega/docs/transforms/force/ https://vega.github.io/vega/docs/transforms/force/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM