我有一个带有轴的 D3 图表,我想通过创建它的原始调用来设置其样式。然而,它似乎只在第一次通话时有效。我会告诉你我的意思:
当我通过
创建轴时svg.select('g.y.axis').call(yaxis)
.selectAll("path")
.attr("fill","none")
.attr("stroke", "#000")
.selectAll("line")
.attr("fill","none")
.attr("stroke", "#000");
只有路径的样式正确。你可以看看我的jsfiddle看看我的意思。我知道这可能比仅使用 CSS 样式慢,但我需要在原始调用中对我正在处理的内容进行样式设置。提前致谢!
最佳答案
因为d3.selectAll("foo").selectAll("bar")
将尝试查找<bar>
是建立的内部元素 <foo>
s。在你的情况下 svg 找不到 <line>
位于<path>
s。
单独调用即可:
svg.selectAll("path")...
svg.selectAll("line")...
UPD 要在 .y.axis 中查找路径/线:
svg.select(".y.axis").selectAll("path")
或
svg.selectAll(".y.axis path")
关于javascript - 定义时的 D3 样式 SVG 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34856617/