0
該場景:d3js多個網絡問題
我有d3呈現多個網絡。
它由單個節點,多個連接的節點網絡組成。
我正在嘗試附加d3-force(模擬),以便拖動時節點不重疊,整個網絡保持相對定位。
正在使用衆多的力量。
問題:
- 當仿真負載(simulation.restart()),整個網絡不斷張開。所有節點/網絡都無限期地離開。
- 即使拖動來自一個網絡的節點,所有其他網絡也會移動。
如果有人能指出我正確的解決方案,將不勝感激。
繼承人小提琴:https://jsfiddle.net/pdubey84/or0tn49k/2/
var nodes = [
{ id: "mammal", group: 0, label: "Mammals", level: 1 },
{ id: "dog" , group: 0, label: "Dogs" , level: 2 },
{ id: "cat" , group: 0, label: "Cats" , level: 2 },
{ id: "fox" , group: 0, label: "Foxes" , level: 2 },
{ id: "elk" , group: 0, label: "Elk" , level: 2 },
{ id: "insect", group: 1, label: "Insects", level: 1 },
{ id: "ant" , group: 1, label: "Ants" , level: 2 },
{ id: "bee" , group: 1, label: "Bees" , level: 2 },
{ id: "fish" , group: 2, label: "Fish" , level: 1 },
{ id: "carp" , group: 2, label: "Carp" , level: 2 },
{ id: "pike" , group: 2, label: "Pikes" , level: 2 },
{ id: "pike" , group: 3, label: "Pikes" , level: 1 },
{ id: "pike" , group: 4, label: "Pikes" , level: 1 },
{ id: "pike" , group: 4, label: "Pikes" , level: 1 },
{ id: "foo" , group: 4, label: "foo" , level: 1 },
{ id: "bar" , group: 4, label: "bar" , level: 1 }
]
var links = [
{ target: "mammal", source: "dog" , strength: 0.7 },
{ target: "mammal", source: "cat" , strength: 0.7 },
{ target: "mammal", source: "fox" , strength: 0.7 },
{ target: "mammal", source: "elk" , strength: 0.7 },
{ target: "insect", source: "ant" , strength: 0.7 },
{ target: "insect", source: "bee" , strength: 0.7 },
{ target: "fish" , source: "carp", strength: 0.7 },
{ target: "fish" , source: "pike", strength: 0.7 },
{ target: "cat" , source: "elk" , strength: 0.1 },
{ target: "carp" , source: "ant" , strength: 0.1 },
{ target: "elk" , source: "bee" , strength: 0.1 },
{ target: "dog" , source: "cat" , strength: 0.1 },
{ target: "fox" , source: "ant" , strength: 0.1 },
{ target: "pike" , source: "cat" , strength: 0.1 },
{ target: "foo" , source: "bar" , strength: 0.1 }
]
function getNodeColor(node, neighbors) {
return 'red'
}
function getLinkColor(node, link) {
return '000'
}
var width = window.innerWidth
var height = window.innerHeight
var svg = d3.select('svg')
svg.attr('width', width).attr('height', height)
// simulation setup with all forces
var linkForce = d3
.forceLink()
.id(function (link) { return link.id })
.strength(function (link) { return link.strength })
var simulation = d3
.forceSimulation()
.force('link', linkForce)
.force('charge', d3.forceManyBody().strength(-500).distanceMax(50))
.force('center', d3.forceCenter(width/2, height/2))
var dragDrop = d3.drag().on('start', function (node) {
node.fx = node.x
node.fy = node.y
}).on('drag', function (node) {
simulation.alphaTarget(0.7).restart()
node.fx = d3.event.x
node.fy = d3.event.y
}).on('end', function (node) {
if (!d3.event.active) {
simulation.alphaTarget(0)
}
node.fx = null
node.fy = null
})
var linkElements = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", 1)
.attr("stroke", "rgba(50, 50, 50, 0.2)")
var nodeElements = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", 10)
.attr("fill", getNodeColor)
.call(dragDrop)
simulation.nodes(nodes).on('tick',() => {
nodeElements
.attr('cx', function (node) { return node.x })
.attr('cy', function (node) { return node.y })
linkElements
.attr('x1', function (link) { return link.source.x })
.attr('y1', function (link) { return link.source.y })
.attr('x2', function (link) { return link.target.x })
.attr('y2', function (link) { return link.target.y })
})
simulation.force("link").links(links)
謝謝很多爲你的迴應@Gerardo ...我試過你的解決方案,但問題仍然是相同的....每當我嘗試拖動一個組件,其他每個連接組件開始移動....它的一種問題當我有很多大型網絡......我是想知道我是否能夠識別每個組件,然後僅適用於這些組件,這樣他們不會移動其他任何東西。思考? – prgrmr
那麼,這是一個*第三個*問題,這不是在你的問題:這是因爲你正在重新加熱模擬。 –
Hi @gerardo ...對不起,你用它,但我嘗試了一堆的東西,但仍然是我提到的第三個問題仍然存在。你碰巧知道一種方式,我可以讓多個組件有不同的力量,這樣它們就不會相互干擾? – prgrmr