2017-05-03 48 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;'..这是现货,我感谢你。剩下的唯一问题是一些旗帜漂移,不能停止 –