D3 - 向矩形添加背景填充

Mol*_*per 7 d3.js

是否可以向矩形元素添加背景填充?我正在使用 rect 元素来创建条形图。每个 rect 元素都有一个宽度和填充集。我想用颜色填充剩余的宽度。

See here: http://codepen.io/jesouhaite08/pen/fhvzA
Run Code Online (Sandbox Code Playgroud)

谢谢!

mat*_*4pl 5

为了获得最大的灵活性,我会使用其他矩形来绘制条形的背景,请查看分叉示例:http : //codepen.io/anon/pen/JnlAE

// Bars
svg_fun.selectAll('rect.background')
       .data(dataset)
       .enter()
       .append('rect')
       .classed('background', true)
       .attr('y', function(d, i) {return i * h_line; })
       .attr('x', 0)
       .attr('height', 20)
       .attr('width', function(d) {return scale_x(max_x);} )
       .attr('fill', 'red')

svg_fun.selectAll('rect.bar')
       .data(dataset)
       .enter()
       .append('rect')
       .classed('bar', true)
       .attr('y', function(d, i) {return i * h_line; })
       .attr('x', 0)
       .attr('height', 20)
       .attr('width', function(d) {return scale_x(d);} )
       .attr('fill', 'teal')
Run Code Online (Sandbox Code Playgroud)