未显示在 D3 数据可视化中的圆圈的标签



我正在按照本教程创建一个类似节点的数据可视化。

<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("miserables.json", function(error, graph) {
if (error) throw error;
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", 5)
.attr("fill", function(d) { return color(d.group); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("span")
.text(function(d) { return d.id; });
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
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; });
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>

但是,我想在每个圆圈旁边发短信,上面有它的 id/名称,并带有额外的元素g但由于某种原因没有显示任何内容。

当我检查每个圆圈时。它显示了具有正确 id 的 ,但跨度根本不显示在图表上。<circle><span>example id</span></circle>

您不是将文本附加到g元素,而是将文本附加到圆圈,让我们按照您的代码查看变量node包含的内容:

var node = svg.append("g")    // returns a selection holding a g
.attr("class", "nodes")     // returns the same g, now with class "nodes"
.selectAll("circle")        // returns an empty selection as there are no circles yet
.data(graph.nodes)          // returns the empty selection with bound data
.enter().append("circle")   // returns a selection of newly entered circles
.attr("r", 5)             // continues to return the circles
...

因此,node包含一系列圆圈,因此在使用node.append("span")时,我们尝试向圆圈添加文本,这是行不通的。因此,我们需要通过分解链接来稍微修改一下:

var node = svg.append("g")  
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("g");  // returns a selection of newly entered g elements
node.append("circle")   // returns a selection of circles, newly append to each g in node
.attr("r", 5)
.attr("fill", function(d) { return color(d.group); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

现在,由于节点包含g元素的选择,我们也可以附加文本:

node.append("text") // returns a selection of text, newly append to each g in node
.text(function(d) { return d.id; });

这里也有一个变化,我已经将node.append("span")更改为node.append("text"),我们正在将svg元素而不是html元素附加到svg。虽然可以附加 span 标记,但它不会显示,因为它们不是 svg 元素。

最后,与此答案一样,您需要更新每个即时报价中每个g的翻译,而不是cxcy属性,因为g不是按 cx 和 cy 属性定位的:

node
.attr("transform",function(d) { return "translate("+[d.x,d.y]+")"; });

这是一个演示。

请注意,要查看您的链接,您需要设置描边颜色:.attr("stroke","black');在您的行上

最新更新