【问题标题】:Can't add text without append("g"), append("g") gives error from d3.js没有附加(“g”)无法添加文本,附加(“g”)从 d3.js 给出错误
【发布时间】:2014-10-09 03:29:41
【问题描述】:

我是 D3 的新手,我正在使用来自 here 的代码。我更改了代码,以便在单击节点时使用来自 MySql 的数据向它们添加新节点(邻居)和边,这就是为什么部分节点代码位于 start() 中的原因。我想将文本标签附加到节点上,通过一些谷歌搜索,我了解到圆形元素和文本元素都需要在 g 内。但是,当我这样做时,我在第 742 行从 d3.js 收到一个错误:

未捕获的 NotFoundError:无法在“节点”上执行“插入之前”: 要插入新节点的节点不是 这个节点

为什么会这样?如何修复它以获得我想要的,同时保留 addNode 功能?

这是我的代码:

var width = 960,
   height = 500;

var color = d3.scale.category10();

var nodes = [],
    links = [];

var force = d3.layout.force()
    .nodes(nodes)
    .links(links)
    .charge(-400)
    .linkDistance(120)
    .size([width, height])
    .on("tick", tick);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var node = svg.selectAll(".node")/*.append("g")*/,
    link = svg.selectAll(".link");

function start() {
    link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
    link.enter().insert("line", ".node").attr("class", "link");
    link.exit().remove();

    node = node.data(force.nodes(), function(d) { return d.id;});
    node.enter()/*.append("g")*/
        .append("circle")
            .attr("class", function(d) { return "node " + d.id; })
            .attr("id", function(d) { return d.id; })
            .attr("r", 8)
        .on("click", nodeClick)
        .append("text")
            .text(function(d) {return d.id; });

    node.exit().remove();
    force.start();
}

function nodeClick() {
    var node_id = event.target.id;
    handleClick(node_id, "node");
}

function tick() {
    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return 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; });
}

注释掉的append("g") 表示我尝试将其放置在哪里(单独尝试)。

【问题讨论】:

  • 您的代码究竟在哪里得到了错误?
  • 在 d3.js 中的第 742 行(不是我的代码)。该行是:return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null);
  • 但是您的代码中的某些内容正在调用导致此错误的某些 d3 函数。你的代码中的那一行是什么?
  • addEdge()->start()->d3.js function addEdge(node_1, node_2) { links.push({source: node_1, target: node_2}); start(); }
  • 你发布的代码对我来说很好。

标签: d3.js nodes javascript


【解决方案1】:

您想在附加文本之前缓存 d3 选择器。

node.enter().append("g")
    .append("circle")
        .attr("class", function(d) { return "node " + d.id; })
        .attr("id", function(d) { return d.id; })
        .attr("r", 8)
    .on("click", nodeClick)
    .append("text")
        .text(function(d) {return d.id; });

这可行,但要创建一个像这样的 xml 结构:

<g>
    <circle>
        <text>...</text>
    </circle>
 </g>

你想要的是:

<g>
    <circle>...</circle>
    <text>...</text>
 </g>

为此,您必须插入一个步骤:

var g = node.enter().append("g");
g.append("circle")
        .attr("class", function(d) { return "node " + d.id; })
        .attr("id", function(d) { return d.id; })
        .attr("r", 8)
    .on("click", nodeClick);
g.append("text")
        .text(function(d) {return d.id; });

【讨论】:

  • 我尝试实现你最底层的代码块,但我仍然得到同样的错误。也许它与node = node.data(force.nodes(), function(d) { return d.id;});有关?
猜你喜欢
  • 1970-01-01
  • 2017-07-02
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多