强行定向图过滤节点和链接

问题描述 投票:0回答:1

我目前正在使用D3.js中的强制导向图进行数据可视化。我有一个用例,在这个用例中,我必须根据一个分数的阈值来过滤节点和链接(过滤的意思是不应该在数据可视化中显示。

以下是一段数据json

"links": [
{
"source": 17,
"target": 9,
"score": 0.428
},
{
"source": 3,
"target": 9,
"score": 0.198
},
{
"source": 17,
"target": 13,
"score": 0.336
},
{
"source": 11,
"target": 13,
"score": 0.178
},
{
"source": 17,
"target": 13,
"score": 0.336
}]

"nodes": [
{
"size": 8,
"score": 0.5,
"id": "Node1",
"name": "Node1",
"type": "triangle-up"
},
{
"size": 10,
"score": 0.1,
"id": "Node2",
"name": "Node2",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node3",
"name": "Node3",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node4",
"name": "Node4",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node5",
"name": "Node5",
"type": "circle"
}]

所以我所做的是基于链接中的得分参数,我已经从链接数组中删除了它的一个项目,当我试图从节点数组中删除节点时,我的图形给出了多个错误。所以我想知道是否有任何方法可以让我找到不与任何其他节点链接的节点,这样我就可以在我的数据可视化中删除或不显示它们。

javascript d3.js force-layout d3-force-directed
1个回答
0
投票

我是这样做的。

var links=data.links.filter(function(d) {
    return d.score > 0.5;
});

var nodes=data.nodes.filter(function(d) {
    if (d3.set(links.map(function(v) { return v.source_id })).values().includes(d.id) | d3.set(links.map(function(m){ return m.target_id })).values().includes(d.id)) {
        return d.id
    };
});

然后,我有一个函数,如 drawNetwork(links,nodes){...}

希望对大家有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.