Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?

Tags:

d3.js

nvd3.js

I'm trying to add some space/padding for a nvd3 multi bar chart. "groupSpacing" is not what I need, since it only adds space between groups. I'll need space between each bar inside group. I found one link in github support. Can you post any solution or tweak?

I also found a d3 example of grouped bar chart. Any help in this example also very helpful to me.

Thanks.

like image 523
rmadd Avatar asked Mar 18 '23 15:03

rmadd


1 Answers

I have draw a d3 group barchart:

fiddle

You can adjust the groupSpacing by change the code on line 56:

var groupSpacing = 6;

Technically i just achieve it by change the width of each rects' width:

var barsEnter = bars.enter().append('rect')
                .attr('class', 'stm-d3-bar')
                .attr('x', function(d,i,j) {
                    return (j * x1.rangeBand() );
                })
                .attr('y', function(d) { return y(d.y); })
                .attr('height', function(d) { return height - y(d.y); })
                .attr('width', x0.rangeBand() / barData.length - groupSpacing )
                .attr('transform', function(d,i) { 
                  return 'translate(' + x0(d.x) + ',0)'; 
                })
                .style("fill", function(d, i, j) { 
                  return color(data[j].key); 
                });

Hope it helps you understand how you can achieve it in d3.

like image 186
huan feng Avatar answered Apr 28 '23 11:04

huan feng