简体   繁体   English

D3,无法添加新的组元素和行

[英]D3, having trouble adding new group element and line

I'm in the process of visualizing a linked list. 我正在可视化链接列表。 Just doing it for fun and to learn d3.js. 只是为了好玩并学习d3.js。 I am able to create group elements for the circles (which represent a node). 我能够为圆(代表一个节点)创建组元素。 But I can't figure out why a new group element is not being added after that. 但是我不知道为什么之后没有添加新的组元素。 I want this new group to contain the line/arrow (the .next reference representation). 我希望这个新组包含行/箭头(.next参考表示)。 I debugged through CDT the code is being executed but nothing is being added to the DOM. 我通过CDT调试了代码,但是没有执行任何操作。

var g = svg.selectAll("g")
        .data(dataArr)
    .enter().append("g")
        .attr("transform", function() {
            if (addMethod === "headAdd") {
                return "translate(" + 50 + " ," + y + ")";
            } else {
                return "translate(" + x + " ," + y + ")";
            }
        })
        .attr("class", "nodes")
        .attr("id", function(d) { return d.getData(); });

    // Create a circle representing the node.
    g.append("circle")
        .attr("r", 40)
        .style("fill", "#7DC24B")
        .style("stroke", "#122A0A")
        .style("stroke-width", 5)
        .style("opacity", 0)
        .style("stroke-opacity", 0.8)
        .transition().duration(1000).style("opacity", 1);

    // Add data to the node.
    g.append("text")
        .attr("dy", ".35em")
        .attr("text-anchor", "middle")
        .style("fill", "#ffffff")
        .text(function(d) { return d.getData(); });


    // Create the "next" arrow.
    // l is the line group that will enclose the line
    // and the text.

    // This doesn't seem to be working from here below.
    var l = svg.selectAll("g")
        .data(["1"])
    .enter().append("g")
        .attr("class", "lines")
        .attr("transform", "translate(" + (x + 40) + ", " + y + ")");

    l.append("line")
        .attr("x1", x + 40)
        .attr("y1", y)
        .attr("x2", x + 100)
        .attr("y2", y)
        .style("stroke", "#8EBD99")
        .style("stroke-width", 30); 

The ... 的...

var l = svg.selectAll("g")
        .data(["1"])

... selects the existing group elements (ie the one you've just created) and performs a data join with those. ...选择现有的组元素(即您刚刚创建的组元素)并与这些元素进行数据联接。 The enter() selection will be empty since the elements you selected were already present in the DOM. 由于您选择的元素已经存在于DOM中,所以enter()选择将为空。 Therefore the parts after .enter() will not be executed and the l selection will be empty. 因此, .enter()之后的部分将不会执行,并且l选择将为空。

You have to make the selector more specific, like: 您必须使选择器更具体,例如:

var l = svg.selectAll(".lines")
        .data(["1"])

The Thinking with Joins article does a good job in explaining the data join concepts. 关于联接思考文章在解释数据联接概念方面做得很好。 See also the General Update Patterns (I,II,III) for more details about the different kind of selections you'll encounter during data joins. 另请参阅常规更新模式(I,II,III),以获取有关数据连接期间将遇到的各种选择的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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