为什么此 D3 代码将

元素添加到正文外部,而不是内部?

2023-11-26

我正在学习 D3,并且在使用选择运算符时遇到了问题。

具体来说,为什么下面的代码要添加<p>元素在身体之外,而不是在身体内部?

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

我正在使用一个完全空白的 HTML 文件<head> and <body>标签进行测试。

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

</body>
</html>

(这重复了 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.");

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

在 a 后使用 Enter 链是不常见的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
    
  • 如果需要,使用 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
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么此 D3 代码将

元素添加到正文外部,而不是内部? 的相关文章

随机推荐

  • 安装ffi(1.1.2)时出错,Bundler无法继续

    在 Rails 3 0 项目中运行 OSX Mountain Lion 当尝试在 Rails 项目中使用捆绑器更新我的 gems 时 出现以下错误 An error occured while installing ffi 1 1 2 an
  • Linux 内核中的 hrtimer 重复任务

    我的目标是使用以下命令在 Linux 内核中创建一个重复任务hrtimer结构 我希望它每 500 毫秒重复一次 但是 我有点困惑如何hrtimer在 Linux 内核中工作 参见linux hrtimer h 我知道时间已指定 回调应该返
  • 加载报道会话时出错(代码 5001)

    当我尝试使用 EclEmma 检查覆盖率时出现以下错误 请帮助我 加载报道会话时出错 代码 5001 错误同时 分析包片段 roo t java at F solo repository target test classes 代码 500
  • 无法解决 WindowsError: [Error 2] 系统找不到指定的文件

    我正在尝试重命名目录中的所有图片 我需要在文件名中添加几个前置零 我是 Python 新手 我编写了以下脚本 import os path c tmp dirList os listdir path for fname in dirList
  • psycopg2 copy_expert() - 如何复制 gzipped csv 文件?

    如果我的表是 schema one table Five 并且我的文件名是 file to import csv gz 我应该为 copy expert cmd 提供什么参数才能将文件内容复制到表中 这就是我正在尝试的 this copy
  • Clojure(读取行)不等待输入

    我正在用 Clojure 编写一个文本游戏 我希望玩家在控制台上输入行 然后游戏逐行响应 研究表明 read line 是一种从 Clojure 中的标准输入获取文本行的方式 但它对我不起作用 我在一个新的 Leiningen 项目中 并且
  • Android Visualizer 类抛出运行时异常

    我在演示应用程序中使用 AndroidFX Visualizer 类来读取 FFT 但是当我尝试创建该类的对象时 它会抛出运行时异常 java lang RuntimeException Cannot initialize Visualiz
  • 转换为 int 与下限

    这些之间有什么区别吗 float foo1 int bar 3 0 float foo2 floor bar 3 0 据我了解 这两种情况都有相同的结果 编译后的代码有什么不同吗 转换为 int 将会截断为零 floor 将向负无穷截断 这
  • 在 OpenCV java 中声明 Mat

    如何使用 Java OpenCV 创建和分配 Mat C 版本来自这一页 is Mat C Mat
  • JAVA 用 InetAddress 指定端口

    我正在使用 InetAddress 来确定我的服务器是否在线 如果服务器离线 它将重新启动服务器 此过程每 5 分钟循环一次 以再次检查服务器是否在线 它工作正常 但现在我需要弄清楚如何指定在检查服务器状态时使用端口 43594 而不是默认
  • Netbeans 6.9.1 中的 JAX-RPC 支持

    我正在尝试生成新的 Web 服务客户端 当选择 客户端样式 为 JAX RPC 样式 时 我收到来自 Netbeans 的通知 您必须下载 JAX RPC 支持插件才能创建 JAX RPC 客户端 插件未在可用插件中列出 我搜索了有关此问题
  • ViewModel 使用 Jetpack Compose 触发导航

    在 Android 中 我经常想要导航以响应 ViewModel 的状态更改 例如 成功的身份验证会触发导航到用户的主屏幕 最佳实践是从 ViewModel 内部触发导航吗 是否有有意的机制来触发可组合项中的导航以响应 ViewModel
  • 检测 Paypal 订阅取消

    我编写了一个简单的贝宝订阅系统 用户可以在其中输入他们的信息 单击按钮 然后开始订阅 我想知道如何知道用户何时取消订阅 我已经看到 txn type subscr cancel 但我不知道如何使用它 因为 paypal 不会再次调用我的处理
  • 如何使用cElementTree检索父节点?

    对于 XML
  • 如何在 Emacs 中获取以某个键(组合)开头的命令列表?

    我可以用C h c describe key briefly 并输入一个组合键 它将返回绑定到它的函数 但我也想只输入一个prefix的键组合 并让它列出并描述绑定到以它开头的键序列的所有功能 例如 所有以C x 按组合键 然后按 C h
  • Swing:无法更新 JButton - repaint() 不起作用

    我第一次使用 Swing 来创建一个简单的 GUI 它由一个JFrame我在其上放置了一个JButton单击时 会调用一些其他代码 大约需要花费时间 3秒返回 就在调用此代码之前 在actionPerformed 我想更新按钮上的文本以通知
  • Java 中 fileSystemWatcher 的建议/示例代码

    我正在尝试在 java 中构建一个 fileSystemWatcher 类似于 C 中的 FileSystemWatcher 请建议java是否有任何内置框架功能 或者建议 直接到任何示例 开源项目 查看Apache Commons JCI
  • 获取可用 WiFi 点列表

    我正在创建一个需要互联网连接的应用程序 因此 当 WiFi 设置未启用时 我会弹出一条消息来将其打开 但当它打开时 它不一定连接到 WiFi 接入点 有什么方法可以在列表中显示当前所有可用的 WiFi 点吗 提前致谢 看看下面的方法Wifi
  • Jackson - 如何处理(反序列化)嵌套 JSON?

    vendors vendor id 367 name Kuhn Pollich company id 1 vendor id 374 name Sawayn Hermann company id 1 我有一个 Vendor 对象 可以从单个
  • 为什么此 D3 代码将

    元素添加到正文外部,而不是内部?

    我正在学习 D3 并且在使用选择运算符时遇到了问题 具体来说 为什么下面的代码要添加 p 元素在身体之外 而不是在身体内部 var pData1 d3 select body select p data 1 enter append p 我