如何使用 Sigma JS (NEO4J) 获取选定的节点?
How to get a selected Node with Sigma JS (NEO4J)?
我正在尝试检索选定的节点。
我完成了:
s.graph.nodes()
但是这个命令returns所有图形节点?
你知道怎么做吗?
您可以创建一些查找函数以通过 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>
我正在尝试检索选定的节点。
我完成了:
s.graph.nodes()
但是这个命令returns所有图形节点?
你知道怎么做吗?
您可以创建一些查找函数以通过 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>