2017-05-03 46 views
1

我設置的節點被固定固定節點不被固定(D3力向圖)

let link = svg.append("g") 
    .attr("class", "links") 
    .selectAll("line") 
    .data(graph.links) 
    .enter().append("line") 
    .attr("stroke-width", () => 4) 

let node = svg.append('g') 
    .attr("class", "nodes") 
    .selectAll(".node") 
    .data(graph.nodes) 
    .enter().append("g") 
    .attr("class", "node") 
    .call(
    d3.drag() 
    .on("start", Node.dragstarted) 
    .on("drag", Node.dragged) 
    .on("end", Node.dragended)) 

node.append("title") 
    .text(d => d.country) 

node.append('image') 
    .attr('xlink:href', d => 'https://rawgit.com/hjnilsson/country-flags/master/svg/' + d.code + '.svg') 
    .attr('height', d => 2.5 * (area[d.code]||5)) 
    .attr('width', d => 4 * (area[d.code])||5) 
simulation 
    .nodes(graph.nodes.map(c => { 
    if(latlong[c.code] === undefined) { 
     console.log(c,'missing lat/long data') 
     return c 
    } 
    c.x = (+latlong[c.code][0]) 
    c.y = (+latlong[c.code][1]) 
    c.fixed = true 
    return c 
    })) 
    .on("tick", ticked) 

這並不正確顯示在比沒有x和y值明顯不同的位置的圖像,但..固定屬性不起作用。

這裏是我的代碼:codepen

如果有誰還知道如何設置畫布了,這樣什麼事都逃不過了頂部或底部我會很感激這一點。

+0

你爲什麼要設置'c.fixed = TRUE'財產? – bumbeishvili

+0

這是這篇文章的主要內容。我有像'{foo:'bar',baz:'bam'...}'這樣的節點的數據。我在那裏設置它也有x,y座標,並且是固定的,因此節點不會移動。我是否在這樣分配時做了一些微妙的錯誤?看到[這裏的答案](http://stackoverflow.com/questions/17656502/d3js-create-a-force-layout-with-fixed-nodes) –

+0

預期的行爲是什麼? – thedude

回答

2

d3.js v4沒有fixed屬性。相反,您需要設置節點fxfy屬性。請注意,您的拖動功能已經在執行此操作。

.nodes(graph.nodes.map(c => { 
if(latlong[c.code] === undefined) { 
    console.log(c,'missing lat/long data') 
    return c 
} 
c.fx = c.x = (+latlong[c.code][0]) 
c.fy = c.y = (+latlong[c.code][1]) 
return c 
})) 

https://codepen.io/anon/pen/ZKXmVe

+0

,所以它變成一個很好的使用法則是'c.fx = cx =(+ latlong [c.code] [1] *(width/246.3 ))+ width/2; c.fy = c.y =(+ latlong [c.code] [0] *(height/130)* -1)+ height/2;'..這是現貨,我感謝你。剩下的唯一問題是一些旗幟漂移,不能停止 –