2
我正在使用sigma.js來處理一個項目,其中我想在forceAtlas2運行時動態添加和刪除主圖中的子圖。我是JS新手,所以我的問題可能是JavaScript問題,但我無法繞過它們。Sigma.js - 修改現有圖中的節點屬性
這是用於加載圖圖入西格瑪實例的函數:
function loadGraph (json) {
console.log ("load graph");
sigInst.stopForceAtlas2();
$.getJSON(json, function (data) {
var edges = data.edges;
var nodes = data.nodes;
$.each(nodes, function (index, value) {
addNode(value.label, value.x, value.y, value.id, value.size, value.color);
});
$.each(edges, function (index, value) {
addEdge(value.source, value.target, value.id, value.weight);
});
});
sigInst.draw(2,2,2);
}
這是ADDNODE功能:
function addNode (label, x, y, id, size, color) {
if (!sigInst.hasNode(id)) {
sigInst.addNode(id,{
'x': x,
'y': y,
label: label,
size:size,
color: color
});
console.log ("added node " + id);
}
else {
var nodeExisting = sigInst.getNodes(id);
var exSize = parseInt(nodeExisting.size);
console.log ("node " + id + " former size: "+ exSize);
exSize += parseInt(size);
nodeExisting.size = exSize;
console.log ("node " + id + " size now: " + exSize);
}
sigInst.draw();
}
和addEdge功能:
function addEdge (source, target,id, weight) {
if (!sigInst.hasEdge(id)) {
sigInst.addEdge(id, source, target, weight).draw();
}
else {
var edgeExisting = sigInst.getEdges(id);
//console.log("existing weight: " + edgeExisting.weight);
var exSize = parseInt(edgeExisting.weight);
exSize += parseInt(weight);
edgeExisting.weight = exSize;
//console.log("modified weight: " + edgeExisting.weight + " (" + edgeExisting.source + " -> " + edgeExisting.target + ")");
}
}
這個工作,我增加了兩個功能於sigma.min.js:
this.hasNode = function(id){return typeof(b.graph.nodesIndex[id]) != 'undefined';};
和
this.hasEdge = function (id) {return typeof(b.graph.edgesIndex[id])!='undefined';};
我的問題是ADDNODE功能不真的會增加現有節點的大小。我來自Java,並且我知道在JavaScript中,現有節點可能不是通過引用傳遞的,所以我不能修改字段並期望sigInst對象反映這一點,但我不知道如何去做。如果有人能幫忙,我將非常感激。提前致謝!
請您詳細說明您所做的工作是否可以動態更改節點/邊緣屬性?我試着添加你的代碼,但它不起作用。 –