gpt4 book ai didi

javascript - 如何在 vis.js 中隐藏图形的分支?

转载 作者:行者123 更新时间:2023-11-29 19:09:01 24 4
gpt4 key购买 nike

我可以通过单击节点 1205 来显示 vis.js 中的部分图表,但无法弄清楚如何使展开的部分在第二次单击时消失节点?

var nodes = new vis.DataSet([
{id: 2696, label: "l"},
{id: 1205, label: "l"},
{id: 2697, label: "l"}
]);

var edges = new vis.DataSet([
{from: 2696, to: 2697},
{from: 2696, to: 1205}
]);

var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};

var network = new vis.Network(container, data, {});
network.on("click", function(e) {
tw_id = 1205;
if (e.nodes[0] == tw_id) {
sel_id = e.nodes[0];
var node = nodes.get(e.nodes[0]);
nodes.add([
{id: 2021, label: "l"},
{id: 2047, label: "l"}
]);
edges.add([
{from: 1205, to: 2021},
{from: 1205, to: 2047}
]);
nodes.update(node);
}
});
#mynetwork {
width: 400px;
height: 300px;
border: 1px solid lightgray;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.17.0/vis.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.17.0/vis.min.js"></script>
<div id="mynetwork"></div>

最佳答案

下面是一个示例,说明如何根据@pgoldweic 的选项以及预定义每次在节点 1205 上单击将显示/不显示的边和节点来实现此功能。

代码维护一个 bool 值来指示下一个操作是显示还是隐藏其他节点。对于 hidden 属性,显示为 false,隐藏为 true

var nodes = new vis.DataSet([
{id: 2696, label: "2696", hidden: false},
{id: 1205, label: "1205", hidden: false},
{id: 2697, label: "2697", hidden: false},
{id: 2021, label: "2021", hidden: true},
{id: 2047, label: "2047", hidden: true}
]);

var edges = new vis.DataSet([
{id: 'e1', from: 2696, to: 2697, hidden: false},
{id: 'e2', from: 2696, to: 1205, hidden: false},
{id: 'e3', from: 1205, to: 2021, hidden: true},
{id: 'e4', from: 1205, to: 2047, hidden: true}
]);

var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};

var network = new vis.Network(container, data, {});

// true=hide; false=show
var toggle = false;
network.on("click", function(e) {
tw_id = 1205;
if (e.nodes[0] == tw_id) {
nodes.update([
{id: 2021, hidden: toggle},
{id: 2047, hidden: toggle}
]);
edges.update([
{id: 'e3', hidden: toggle},
{id: 'e4', hidden: toggle}
]);
network.fit();
// switch toggle
toggle = !toggle;
}
});
#mynetwork {
width: 400px;
height: 300px;
border: 1px solid lightgray;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.17.0/vis.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.17.0/vis.min.js"></script>
<div id="mynetwork"></div>

关于javascript - 如何在 vis.js 中隐藏图形的分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40609135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com