Vì vậy, tôi có mã graph lực lượng bố trí tiếp theo để thiết lập các nút, liên kết và các yếu tố khác:Làm cách nào để xóa tất cả các phần tử con khỏi nút và sau đó áp dụng lại chúng với màu sắc và kích thước khác nhau?
var setLinks = function()
{
link = visualRoot.selectAll("line.link")
.data(graphData.links)
.enter().append("svg:line")
.attr("class", "link")
.style("stroke-width", function (d) { return nodeStrokeColorDefault; })
.style("stroke", function (d) { return fill(d); })
.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; });
graphData.links.forEach(function (d)
{
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
};
var setNodes = function()
{
node = visualRoot.selectAll(".node")
.data(graphData.nodes)
.enter().append("g")
.attr("id", function (d) { return d.id; })
.attr("title", function (d) { return d.name; })
.attr("class", "node")
.on("click", function (d, i) { loadAdditionalData(d.userID, this); })
.call(force.drag)
.on("mouseover", fadeNode(.1)).on("mouseout", fadeNode(1));
};
//append the visual element to the node
var appendVisualElementsToNodes = function()
{
node.append("circle")
.attr("id", function (d) { return "circleid_" + d.id; })
.attr("class", "circle")
.attr("cx", function (d) { return 0; })
.attr("cy", function (d) { return 0; })
.attr("r", function (d) { return getNodeSize(d); })
.style("fill", function (d) { return getNodeColor(d); })
.style("stroke", function (d) { return nodeStrokeColorDefault; })
.style("stroke-width", function (d) { return nodeStrokeWidthDefault; });
//context menu:
d3.selectAll(".circle").on("contextmenu", function (data, index)
{
d3.select('#my_custom_menu')
.style('position', 'absolute')
.style('left', d3.event.dx + "px")
.style('top', d3.event.dy + "px")
.style('display', 'block');
d3.event.preventDefault();
});
//d3.select("svg").node().oncontextmenu = function(){return false;};
node.append("image")
.attr("class", "image")
.attr("xlink:href", function (d) { return d.profile_image_url; })//"Images/twitterimage_2.png"
.attr("x", -12)
.attr("y", -12)
.attr("width", 24)
.attr("height", 24);
node.append("svg:title")
.text(function (d) { return d.name + "\n" + d.description; });
};
Bây giờ, màu sắc và phụ thuộc kích thước thay đổi và tôi cần phải vẽ lại các vòng tròn đồ thị (+ tất cả các yếu tố nối) với màu sắc và bán kính khác nhau. Có vấn đề với nó.
tôi có thể làm điều này:
visualRoot.selectAll(".circle").remove();
nhưng tôi có tất cả những hình ảnh mà tôi gắn liền với '.circles'
vẫn còn đó.
Trong mọi trường hợp, mọi trợ giúp sẽ được đánh giá cao, hãy cho tôi biết nếu giải thích không đủ rõ ràng, tôi sẽ cố gắng khắc phục.
P.S. sự khác nhau giữa graphData.nodes
và d3.selectAll('.nodes')
là gì?
Thật không may .html ("") không hoạt động trong Safari. Hoạt động tốt trong tất cả các trình duyệt khác. – glyph
@glyph: xem http://stackoverflow.com/a/43661877/1587329 để biết cách chính thức để thực hiện việc này –