【问题标题】:Can't change fill of d3.js rectangle无法更改 d3.js 矩形的填充
【发布时间】:2013-08-19 06:00:05
【问题描述】:

所以我有这个代码 -

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(...) 正在将颜色从蓝色更改为蓝色。你想发生什么?

标签: javascript d3.js coffeescript


【解决方案1】:

将attr() 替换为style()。所以你的线:

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

应该是:

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

【讨论】:

  • 实际上,看起来我上面的小提琴中两者的区别仅在 CSS 也存在时才存在:样式覆盖它,填充不存在。 @praks,你的例子中有 CSS 吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
  • 2012-02-14
  • 2019-03-01
  • 1970-01-01
  • 2015-07-13
相关资源
最近更新 更多