【发布时间】:2018-08-15 11:18:34
【问题描述】:
我正在尝试根据datum 中包含的内容动态地append 形状。我的对象是这样的:
const boom = [
{
shape: 'rect',
color: 'red',
width: 50,
height: 50,
x: 50,
y: 100
}
]
我的代码是这样的:
const stage = d3.select('stageContainer')
.append('svg')
.attr('width', 100)
.attr('height', 100)
.style('border-width', '2')
.style('border-color', 'red')
.style('border-style', 'solid')
stage.selectAll('.group01')
.data(boom)
.enter()
.append(d => document.createElement(d.shape))
.attr('fill', d => d.color)
.attr('width', d => d.width)
.attr('height', d => d.height)
.attr('x', d => d.x)
.attr('y', d => d.y)
我可以看到它正在添加到 DOM,但实际上并没有呈现。
【问题讨论】:
-
试试
.append(d => d.shape) -
试过了。我不断收到:
"TypeError: Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'." -
现在我仔细阅读了文档,如果它是一个函数,它必须返回与常量字符串不同的类型。该方法应该能够处理不同的函数返回(包括字符串)