Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 - Add background fill to rect

Tags:

d3.js

Is it possible to add a background fill to a rect element? I am using rect elements to create a bar graph. Each rect element has a width and fill set. I want to fill the remaining width with a color.

See here: http://codepen.io/jesouhaite08/pen/fhvzA

Thanks!

like image 734
Molly Harper Avatar asked Oct 23 '14 18:10

Molly Harper


1 Answers

For best flexibility I would use other rectangles to draw the background for your bars, check the forked example: 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')
like image 84
matys84pl Avatar answered Oct 05 '22 19:10

matys84pl