【发布时间】:2021-08-03 10:55:06
【问题描述】:
我有一个名为 data 的简单数据对象,其中包含一些我希望用于我的圆圈的半径、坐标和颜色。但是我现在想让它们全部变成橙色,但最后一行代码似乎没有运行?
const myCircles = svg.selectAll()
.data(data);
myCircles.enter().append('circle')
.attr('cx' , (d) => d.x)
.attr('cy' , (d) => d.y)
.attr('r' , (d) => d.radius )
.attr('fill' , (d) => d.color )
myCircles.attr('fill' , 'orange');
我尝试过的其他方法无效
我试过了
d3.selectAll(myCircles).attr('fill' , 'orange');
我试过了
svg.selectAll(myCircles).attr('fill' , 'orange');
但两次都收到错误:d3.v7.min.js:2 Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Element': '[object Object]' is not a valid selector.
什么有效,但我不想要它
d3.selectAll('circle').attr('fill' , 'orange')
因为我想通过变量 myCircles 选择圆圈,而不是使用 d3 标签“circle”,因为我打算稍后制作更多圆圈。
【问题讨论】:
-
myCircles包含空的 update 选择,而您正在附加到 enter 选择。要操作输入选择中的圆圈,您需要存储对该选择的引用,或者您需要使用.merge()来合并两个选择。
标签: javascript svg d3.js