简体   繁体   中英

Can't add text without append(“g”), append(“g”) gives error from d3.js

I am new to D3 and I am working with code from here . I changed the code so I can add new nodes (neighbors) and edges to them with data from MySql upon click on a node, which is why part of the node code is in start() . I want to append text labels to the nodes, and from some googling I understand that both the circle element and the text element needs to be within a g . However, when I do this, I get an error from d3.js on line 742:

Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node

Why is this and how do I fix it to get what I want, while preserving the addNode functionality?

Here is my code:

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; });
}

The commented-out append("g") indicates where I tried to place it (separate attempts).

You want to cache the d3 selector before appending the text.

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; });

That'll work but create a xml structure like this:

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

What you want is:

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

To achieve that, you must insert one step:

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; });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM