2017-05-03 8 views
1

(포스 방향 그래프는 D3)고정 노드하지 고정되는 I의 노드를 설정하고

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))) + 폭/2; c.fy = c.y = (+ latlong [c.code] [0] * (height/130) * -1) + height/2;'..이 자리에있었습니다. 왼쪽의 유일한 문제는 두 개의 기둥이 표류하고 멈추지 않는다는 것입니다. –