Tôi mới đến D3 và đang làm việc trên đồ thị có lực tác động nơi dữ liệu json động. Tôi có thể thay đổi đồ thị lực khi nhận dữ liệu mới nhưng điều đó xảy ra với hiệu ứng tạo luồng. Mã tạo biểu đồ lực của tôi là:Cập nhật các liên kết trên đồ thị lực tác động từ dữ liệu json động
<div class="graph"></div>
<script>
var w = 660,
h = 700,
r = 10;
var vis = d3.select(".graph")
.append("svg:svg")
.attr("width", w)
.attr("height", h)
.attr("pointer-events", "all")
.append('svg:g')
.call(d3.behavior.zoom().on("zoom", redraw))
.append('svg:g');
vis.append('svg:rect')
.attr('width', w)
.attr('height', h)
.attr('fill', 'rgba(1,1,1,0)');
function redraw() {
console.log("here", d3.event.translate, d3.event.scale);
vis.attr("transform", "translate(" + d3.event.translate + ")" +
" scale(" + d3.event.scale + ")");
};
var force = d3.layout.force()
.gravity(.05)
.charge(-200)
.linkDistance(260)
.size([w, h]);
var svg = d3.select(".text")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.json(graph, function(json) {
var nodeList = json.nodes;
var link = vis.selectAll("line")
.data(json.links)
.enter()
.append("line")
.attr("stroke-opacity", function(d) {
if(d.label == 'is a') {
return '0.8';
} else {
return '0.2';
};
})
.attr("stroke-width", function(d) {
if(d.value !== null) {
return d.value;
} else {
return 2;
};
})
.style("stroke", function(d) {
if(d.color !== null) {
return d.color;
};
})
.on("mouseover", function() {
d3.select(this)
.style("stroke", "#999999")
.attr("stroke-opacity", "1.0");
})
.on("mouseout", function() {
d3.select(this)
.style("stroke", function(d) {
if(d.color !== null) {
return d.color;
};
})
.attr("stroke-opacity", function(d) {
if(d.label == 'is a') {
return '0.8';
} else {
return '0.2';
};
})
});
link.append("title")
.text(function(d) { return d.label });
var node = vis.selectAll("g.node")
.data(json.nodes)
.enter()
.append("svg:g")
.attr("class","node")
.call(force.drag);
node.append("svg:circle")
.attr("r", function(d) {
if (d.size > 0) {
return 10+(d.size*2);
} else {
return 10;
}
})
.attr("id", function(d) { return "Node;"+d.id; })
.style("fill", function(d) {
if(d.style == 'filled') {
return d.color;
};
})
.style("stroke", function(d) {
if(d.style !== 'filled') {
return d.color;
};
})
.style("stroke-width", "2")
.on("mouseover", function() {
d3.select(this).style("fill", "#999");
fade(.1);
})
.on("mouseout", function(d) {
if (d.style == 'filled') {
d3.select(this).style("fill",d.color);fade(1);
} else {
d3.select(this).style("stroke",d.color);
d3.select(this).style("fill","black");
}
fade(1);
});
node.append("title")
.text(function(d) { return d.Location; });
force.nodes(json.nodes)
.links(json.links)
.on("tick", tick)
.alpha(1)
.start();
function tick() {
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
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; });
}
});
</script>
Tôi có thể tạo biểu đồ mới khi nhận được chuỗi json mới bằng cách gọi lại toàn bộ hàm. Điều này tạo ra một đồ thị mới thay cho cũ. Tôi không thể cập nhật biểu đồ cũ với tập giá trị mới khi nhận được các giá trị; các nút trong biểu đồ của tôi không thay đổi, chỉ là mối quan hệ giữa chúng thay đổi.
Tôi đã vấp ngã khi một ví dụ (http://bl.ocks.org/1095795) trong đó một nút mới bị xóa và tạo lại, nhưng việc triển khai thực hiện hơi khác một chút.
Bất kỳ con trỏ hoặc trợ giúp nào sẽ được đánh giá cao.
@Rahul Về cơ bản bạn đang vẽ lại toàn bộ đồ thị? – Pramod
tôi không thể hiểu câu trả lời này. bất cứ ai có thể giúp tôi hiểu nó, để tôi có thể sử dụng nó. –
chỉ là một vài quan sát, phần tử được cho là chứa svg là một tham số của hàm tạo Graph, nhưng sau đó một giá trị mã hóa cứng được sử dụng thay thế. Ngoài ra, việc đặt tên hàm không nhất quán là camelCased, như removeallLinks. – glasspill