【问题标题】:d3.js selector not returning actual objectd3.js 选择器不返回实际对象
【发布时间】:2018-10-19 00:11:25
【问题描述】:

我正在使用 d3.js v4。我已经在 google chrome 浏览器的控制台上执行了以下代码。

var theData = [ 1, 2, 3 ]

var p = d3.select("body").selectAll("p")
          .data(theData)
          .enter()
          .append("p")
          .text("hello ");

console.log(p);

我期待这样的结果:

但我得到的是如下所示

有人可以帮我解释一下为什么会有这种差异吗?

【问题讨论】:

    标签: d3.js


    【解决方案1】:

    根据 D3 4.x API:

    选择不再使用原型链注入子类化Array;它们现在是普通对象,提高了性能。

    因此,在 D3 版本 4.x 中,选择对象。

    另外,值得一提的是,您使用的是压缩版本 (https://d3js.org/d3.v4.min.js),它返回:

    zi {_groups: Array[1], _parents: Array[1]}
    

    在普通版本(https://d3js.org/d3.v4.js)中,console.log 返回应该是:

    Selection {_groups: Array[1], _parents: Array[1]}
    

    如果您想获得类似于 D3 v3 中的内容,请使用nodes()

    var theData = [ 1, 2, 3 ]
    
    var p = d3.select("body").selectAll("p")
              .data(theData)
              .enter()
              .append("p")
              .text("hello ");
    
    console.log(p.nodes());
    <script src="https://d3js.org/d3.v4.js"></script>

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2021-01-25
      • 2021-08-27
      相关资源
      最近更新 更多