0
힘 지향 그래프를 만들고 그래프 노드에 텍스트를 추가하고 싶습니다.D3.js - 강제 방향 그래프의 노드에 텍스트 추가
콘솔에서 텍스트 요소가 원에 추가되지만 값이 없음을 알 수 있습니다.
그래프에 대한 코드는 다음과 같다 : 지금은
var force = d3.layout.force()
.charge(-120)
.linkDistance(40);
var width = $(document).width() - 550;
height = $(document).height() - 300;
var colour = d3.scale.category20();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("style", "outline: thin solid black;");
force
.size([width, height])
.nodes(convertedInfo.nodes)
.links(convertedInfo.links)
.start();
//Adding nodes and data
var link = svg.selectAll("link")
.data(convertedInfo.links)
.enter()
.append("line")
.attr("stroke-width", 3)
.attr("class", "link")
.attr("stroke", "grey");
var node = svg.selectAll("node")
.data(convertedInfo.nodes)
.enter()
.append("circle")
.attr("r", 7)
.attr("class", "node")
.style("fill", function(d) {return colour(d.domain_count);})
.append('text');
var text = svg.selectAll("text")
.data(convertedInfo.nodes)
.enter()
.append("text")
.attr("x", function(d) { return d.cx; })
.attr("y", function(d) { return d.cy; })
.text('text')
.attr("font-family", "sans-serif")
.attr("font-size", "20px")
.attr("fill", "red");
//Fisheye view
var fisheye = d3.fisheye.circular()
.radius(70)
.distortion(2);
svg.on("mousemove", function() {
fisheye.focus(d3.mouse(this));
node.each(function(d) { d.fisheye = fisheye(d); })
.attr("cx", function(d) { return d.fisheye.x; })
.attr("cy", function(d) { return d.fisheye.y; })
.attr("r", function(d) { return d.fisheye.z * 4.5; });
link.attr("x1", function(d) { return d.source.fisheye.x; })
.attr("y1", function(d) { return d.source.fisheye.y; })
.attr("x2", function(d) { return d.target.fisheye.x; })
.attr("y2", function(d) { return d.target.fisheye.y; });
});
force.on("tick", function() {
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; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
내가 그렇게된다면 내가 볼 수 있도록 단지 text
값의 자리 표시자를 넣어 있습니다.
이 코드의 잘못된 점은 무엇입니까?
'convertedInfo.nodes'가 강제 레이아웃에 제공하는 것이면 잘못된 속성 이름을 사용하고 있습니다.'.cx'와'.cy'는 강제 레이아웃에 의해 설정되지 않습니다. '.x'와'.y'를 사용하십시오. –
불행히도 여전히 작동하지 않습니다. – ALR
사실, 콘솔에서 텍스트 요소가 추가 된 것을 볼 수 있습니다. 단지 값이 없습니다. 그리고 줄을'.text ('text')'라고 바꾸면, 여전히 작동하지 않습니다. – ALR