我有这段代码 -
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')并未改变矩形的颜色。为什么?
bars.selectAll(...)
正在将颜色从蓝色改变为蓝色。你想要发生什么? - Lars Kotthoff