Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t将文本追加到d3.js强制布局节点_Javascript_Svg_D3.js_Label_Force Layout - Fatal编程技术网

Javascript Can';t将文本追加到d3.js强制布局节点

Javascript Can';t将文本追加到d3.js强制布局节点,javascript,svg,d3.js,label,force-layout,Javascript,Svg,D3.js,Label,Force Layout,目标:将文本附加到d3 force布局中的每个节点 错误:文本被附加到对象上(我想,请参见控制台),但不会显示在屏幕上 这是我的建议 如果您有任何想法,我将不胜感激。您不能将svg文本添加到svg循环中。您应该首先为每个节点创建一个svg g对象(g代表组),然后为每个g元素添加一个圆圈和一个文本,如以下代码所示: var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("g"); var ci

目标:将文本附加到d3 force布局中的每个节点

错误:文本被附加到对象上(我想,请参见控制台),但不会显示在屏幕上

这是我的建议


如果您有任何想法,我将不胜感激。

您不能将svg文本添加到svg循环中。您应该首先为每个节点创建一个svg g对象(g代表组),然后为每个g元素添加一个圆圈和一个文本,如以下代码所示:

var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("g");

var circle = node.append("circle")
    .attr("class", "node")
    .attr("id", function (d) { return d.name; })
    .attr("r", 5)
    .style("fill", function (d) {
        return color(d.group);
    });

var label = node.append("svg:text")
    .text(function (d) { return d.name; })
    .style("text-anchor", "middle")
    .style("fill", "#555")
    .style("font-family", "Arial")
    .style("font-size", 12);
当然,勾号函数应该相应地更新:(还有一点css)

这是

是否为您解决了此问题?可能与
var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("g");

var circle = node.append("circle")
    .attr("class", "node")
    .attr("id", function (d) { return d.name; })
    .attr("r", 5)
    .style("fill", function (d) {
        return color(d.group);
    });

var label = node.append("svg:text")
    .text(function (d) { return d.name; })
    .style("text-anchor", "middle")
    .style("fill", "#555")
    .style("font-family", "Arial")
    .style("font-size", 12);
circle.attr("cx", function (d) {
    return d.x;
})
.attr("cy", function (d) {
    return d.y;
});

label.attr("x", function (d) {
    return d.x;
})
.attr("y", function (d) {
    return d.y - 10;
});