【问题标题】:How to append text in d3 force layout?如何在 d3 强制布局中附加文本?
【发布时间】:2015-07-09 07:06:53
【问题描述】:

我正在尝试在强制布局中添加文本。首先,我正在创建一个 svg 组,并将圆圈和文本添加到其中。圆圈工作正常,但文本不起作用。这是代码

var node = svg.selectAll("g")
                    .data(measures.nodes)
                    .enter().append("g")
                    .attr("class", "node")
                    .call(node_drag);
var circle = node.append("circle")
                    .attr("fill", "blue")
                    .attr("r",5)
                    .attr("dx", ".10em")
                    .attr("dy", ".10em");

var text = node.append("text")
                .data(measures.nodes)
                .attr("color", "blue")
                .text(function(d){ return d.name; })

【问题讨论】:

  • 你能创建一些可运行的东西吗,一个 jsfiddle 或堆栈 sn-p?
  • 这里是代码。 jsfiddle.net/6n0vd4nb

标签: javascript jquery d3.js svg


【解决方案1】:

文本是屏幕的,因为您错过了定位方法。如果你添加这个,你会看到text attached to the nodes

        text.attr("x", function(d) { return d.x; })
        .attr("y", function(d) { return d.y; });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2014-05-17
    相关资源
    最近更新 更多