我正在尝试检索所选节点。
我做了:
s.graph.nodes()
但是此命令会返回所有图形节点吗?
你知道怎么做吗?
答案 0 :(得分:2)
您可以创建一些查找函数,以按ID查找节点。
function lookupNodesByKeyValue(sigmaInstance, key, value) {
return sigmaInstance.graph.nodes().filter(node => node[key] === value);
}
function lookupNodeByKeyValue(sigmaInstance, key, value) {
return lookupNodesByKeyValue(sigmaInstance, key, value).pop();
}
function lookupNodeById(sigmaInstance, value) {
return lookupNodeByKeyValue(sigmaInstance, 'id', value);
}
var graphData = {
"nodes": [
{ "id": "n0", "label": "A node", "x": 0, "y": 0, "size": 3 },
{ "id": "n1", "label": "Another node", "x": 3, "y": 1, "size": 2 },
{ "id": "n2", "label": "And a last one", "x": 1, "y": 3, "size": 1 },
],
"edges": [
{ "id": "e0", "source": "n0", "target": "n1" },
{ "id": "e1", "source": "n1", "target": "n2" },
{ "id": "e2", "source": "n2", "target": "n0" },
]
};
// Initialize Sigma object
var s = new sigma({
graph: graphData,
container: 'sigma-container',
settings: { defaultNodeColor: '#ec5148' }
});
// Set initial zoom
s.cameras[0].goTo({ x: 1, y: 1, angle: 0, ratio: 2.0 });
console.log("Node 'n2' => " + lookupNodeById(s, "n2").label);
body {
background: #ddd;
}
.sigma-wrapper {
max-width: 240px;
background: #fff;
border: 2px solid #AAA;
margin: auto;
}
#sigma-container {
max-width: 240px;
height: 240px;
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sigma.js/1.2.1/sigma.min.js"></script>
<div class="sigma-wrapper">
<div id="sigma-container"></div>
</div>