为什么这个D3代码将<p>元素添加到正文之外,而不是在其中?

Tak*_*son 9 javascript jquery d3.js

我正在学习D3并且使用select运算符遇到了一个问题.

具体来说,为什么以下代码将<p>元素添加到正文之外,而不是在其中?

var pData1 = d3.select("body").select("p").data([1]).enter().append("p");

我正在使用一个完全空白的HTML文件,只有<head><body>标签来测试.

<html lang="en">
<head>
<title><!-- Insert your title here --></title>
  <script type="text/javascript" src="d3.min.js"></script>
</head>
<body>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Ame*_*aBR 13

(这重复了Lars Kotthoff的答案中的内容,但我花时间创建了演示,所以我想我还会发帖.)

问题在于select,selectAll不会重新定义enter()选择中添加的元素的父元素.

d3.select("body").select("p#a")
    .data([1])
    .enter().append("p").attr("id", "a")
    .text("This paragraph is appended to <html> (the document root) 
          because no selectAll statement reset the parent element.");

d3.selectAll("p#b")
    .data([1])
    .enter().append("p").attr("id", "b")
    .text("This paragraph is appended to <html> 
          because the selectAll statement is called directly on the root.");

d3.selectAll("body").select("p#c")
    .data([1])
    .enter().append("p").attr("id", "c")
    .text("This paragraph is also appended to <html> 
          because the last selectAll statement was called directly from the root.");

d3.select("body").selectAll("p#d")
    .data([1])
    .enter().append("p").attr("id", "d")
    .text("This paragraph is appended to <body> 
          because the selectAll statement is a sub-selection of the body selection.");

d3.selectAll("body").selectAll("p#e")
    .data([1])
    .enter().append("p").attr("id", "e")
    .text("This paragraph is also appended to <body> 
          because the final selectAll statement is a sub-selection of the body.");
Run Code Online (Sandbox Code Playgroud)

http://fiddle.jshell.net/eLF4H/

select语句之后使用输入链是不常见的(与selectAll相比),因为如果要进行数据连接,通常会选择多个元素.但是,如果要创建元素(如果它不存在)或者更新它,则有两个选项:

  • 使用selectAll语句后跟数据连接

    var pdata1 = d3.select("body").selectAll("p#data") 
                        //select element if it exists
                   .data([dataObject]);  
                        //join to the current data
    
    pdata1.enter().append("p").attr("id", "data"); 
                        //create element if required
    
    pdata1.text(function(d){return d.textValue;}); 
                        //set or update the element based on the data
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果需要,使用if语句创建元素并用于.datum()绑定数据

    var pdata1 = d3.select("p#data") 
                        //select element if it exists
    
    if ( pdata1.empty() ) {
       pdata1 = d3.select("body").append("p").attr("id", "data"); 
                        //create element if required
    }
    
    pdata1.datum(dataObject)
                        //note that you don't need to put the data into an array
          .text(function(d){return d.textValue;}); 
                        //set or update the element based on the data
    
    Run Code Online (Sandbox Code Playgroud)