gpt4 book ai didi

javascript - 无法更改 d3.js 矩形的填充

转载 作者:行者123 更新时间:2023-11-29 17:15:44 27 4
gpt4 key购买 nike

所以我有这段代码-

bars.
append('rect')
.attr('x', 0)
.attr('y', (d, i) ->
return yScale(i)
).attr('width', (datum) ->
return xScale(datum.freq)
)
.attr('height', barHeight)
.attr('fill', 'blue')
.attr('class', 'bar')
.on('click', ->
bars.selectAll('rect').attr('fill', '#0000ff')
currentFill = d3.select(this).style('fill')
nextColor = {}
if currentFill == '#0000ff'
nextColor = '#ff0000'
else
nextColor = '#0000ff'
d3.select(this).style('fill', nextColor)
)

但是,bars.selectAll('rect') 并没有改变矩形的颜色。为什么?

最佳答案

attr() 替换为 style()。所以你的台词:

bars.selectAll('rect').attr('fill', '#0000ff')

应该是:

bars.selectAll('rect').style('fill', '#0000ff')

关于javascript - 无法更改 d3.js 矩形的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18279921/

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