gpt4 book ai didi

d3.js - D3 在将 .data() 设置为附加元素时抛出 "Cannot read property ' ownerDocument' of null"

转载 作者:行者123 更新时间:2023-12-01 23:03:26 34 4
gpt4 key购买 nike

我收到此错误:

Uncaught TypeError: Cannot read property 'ownerDocument' of null



这是来自设置 .data() 的一行到最近由 d3 添加的元素:
const someSet = someSelection.filter('.some:(filter)')

someSet.each((d, i, nodes) => {
const someElement = d3.select(nodes[i])
const someChild = someElement.append('g')

someChild.data(this.data) // <-- This line throws the error
.enter()
.append('circle')
.classed('some-class', true)
})

环顾四周,我能找到的只有 a source saying they had this error when the element in question hadn't yet been added to the page ,但我已确认在抛出此错误时我的元素已添加到页面中。

最佳答案

结果我忘记在将 .data() 添加到嵌套元素之前添加一个新的空选择:

const someSet = someSelection.filter('.some:(filter)')

someSet.each((d, i, nodes) => {
const someElement = d3.select(nodes[i])
const someChild = someElement.append('g')

someChild.selectAll('circle.some-class') // added empty selection here
.data(this.data)
.enter()
.append('circle')
.classed('some-class', true)
})

如果我执行诸如创建组、存储对组的引用、附加数据并追加,然后尝试将新数据附加到对组的引用 ( <g> ) 而不是选择组的内容)。例如,这将导致此错误:
const someObj = {} // or some class etc
const someSet = someSelection.filter('.some:(filter)')

someSet.each((d, i, nodes) => {
const someElement = d3.select(nodes[i])
const someChild = someElement.append('g')
someObj.someChild = someChild

someChild.selectAll('circle.some-class') // added empty selection here
.data(this.data)
.enter()
.append('circle')
.classed('some-class', true)
})

// ...do stuff
someObj.someChild.data(newData)
// Error because someObj.someChild is a D3 element, not a D3 selection

所以,最后应该是:
someObj.someChild.selectAll('circle.some-class').data(newData)

...或者存储的引用应该是数据添加到的选择。

关于d3.js - D3 在将 .data() 设置为附加元素时抛出 "Cannot read property ' ownerDocument' of null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49696537/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com