まったく新しいデータ(データストリームなど)からの力指向有向グラフに新しいノードを導入する方法を探しています。
mbostockの例(thisまたはthis)では、初期設定ではすべてのノードがレンダリングされるため、ノードはスムーズに出入りできます。
ただし、まったく新しいデータポイントが導入された場合、グラフは最初からレンダリングされます。まったく新しいノードをグラフにスムーズに移行させる方法はありますか?
私が何を意味するかについては、このコーデックス(2番目の例を直接適応したもの)を参照してください。既存のノードに出入りすることは問題ありませんが、新しいノードが入るときの移行は途方もないです。
//smooth update
nodes = [a, b];
links = [l_ab];
restart();
//not as smooth
var d = {id: id++};
nodes = [a, b, c, d];
links = [l_ab, l_bc, l_ca, {
source: a,
target: d
}];
restart();
真新しいノードの入り口は、単純な理由で「ジャンピー」に見えます。シミュレーションが開始されると、そのノードは最初に左上隅(0,0
SVG座標系)に表示されます。
「スムーズ」の定義に応じて、ここにはさまざまな解決策があります。私はそれを作るための最も明白な方法数えるsmoothierはSVGの中央にノードの初期位置を設定しています。そうすれば、新しいノードは最終的な位置までそれほど移動しません。
新しいノードのx
とy
プロパティを設定してそれを行うことができます:
var d = {id: id++, x: width/2, y: height/2};
その変更を加えたコードは次のとおりです。
var svg = d3.select("svg"),
width = 250
height = 250
color = d3.scaleOrdinal(d3.schemeCategory10);
var a = {
id: "a"
},
b = {
id: "b"
},
c = {
id: "c"
},
nodes = [a, b, c],
l_ab = {
source: a,
target: b
}
l_bc = {
source: b,
target: c
},
l_ca = {
source: c,
target: a
}
links = [l_ab, l_bc, l_ca];
var id = 0;
var simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody())
.force('link', d3.forceLink())
.force('center', d3.forceCenter(width / 2, height / 2))
.alphaTarget(1)
.on("tick", ticked)
.stop()
var g = svg.append("g")
link = g.append("g").attr("stroke", "#000").attr("stroke-width", 1.5).selectAll(".link"),
node = g.append("g").attr("stroke", "#fff").attr("stroke-width", 1.5).selectAll(".node");
restart();
function restart() {
// Apply the general update pattern to the nodes.
node = node.data(nodes, function(d) {
return d.id;
});
node.exit().transition()
.attr("r", 0)
.remove();
node = node.enter().append("circle")
.attr("fill", function(d) {
return color(d.id);
})
.call(function(node) {
node.transition().attr("r", 8);
})
.merge(node);
// Apply the general update pattern to the links.
link = link.data(links, function(d) {
return d.source.id + "-" + d.target.id;
});
// Keep the exiting links connected to the moving remaining nodes.
link.exit().transition()
.attr("stroke-opacity", 0)
.attrTween("x1", function(d) {
return function() {
return d.source.x;
};
})
.attrTween("x2", function(d) {
return function() {
return d.target.x;
};
})
.attrTween("y1", function(d) {
return function() {
return d.source.y;
};
})
.attrTween("y2", function(d) {
return function() {
return d.target.y;
};
})
.remove();
link = link.enter().append("line")
.call(function(link) {
link.transition().attr("stroke-opacity", 1);
})
.merge(link);
// Update and restart the simulation.
simulation.nodes(nodes);
simulation.force("link").links(links);
simulation.alpha(1).restart();
}
function ticked() {
node.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
})
link.attr("x1", function(d) {
return d.source.x;
})
.attr("y1", function(d) {
return d.source.y;
})
.attr("x2", function(d) {
return d.target.x;
})
.attr("y2", function(d) {
return d.target.y;
});
}
restart();
document.getElementById('btnRenderExisting3Node').addEventListener('click', function() {
nodes = [a, b, c];
links = [l_ab, l_bc, l_ca];
restart();
});
document.getElementById('btnRenderExisting2Node').addEventListener('click', function() {
nodes = [a, b];
links = [l_ab];
restart();
});
document.getElementById('btnRenderNewNode').addEventListener('click', function() {
var d = {
id: id++,
x: width / 2,
y: height / 2
};
nodes = [a, b, c, d];
links = [l_ab, l_bc, l_ca, {
source: a,
target: d
}];
restart();
});
svg {
border: 1px black solid
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<div>
<button id='btnRenderExisting2Node'>Render 2 existing nodes</button>
<button id='btnRenderExisting3Node'>Render 3 existing nodes</button>
<button id='btnRenderNewNode'>Render 3 existing nodes and 1 brand new node</button>
</div>
<svg width="250" height="250"></svg>
</div>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加