Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  160] [ 3]  / answers: 1 / hits: 34667  / 8 Years ago, mon, october 17, 2016, 12:00:00

In D3 version 4.x, d3.scale.ordinal() has been changed to d3.scaleOrdinal and d3.rangeRoundBands has been changed to:



d3.scaleBand()
.rangeRound([range]);


To find the width of a band, what is the equivalent of d3.rangeBand()?


More From » d3.js

 Answers
33

To find the width of the band in a band scale you have to use:



scale.bandwidth();


According to the API, bandwidth():




Returns the width of each band.




Here is a demo:





var scale = d3.scaleBand()
.domain([foo, bar, baz, foobar, foobaz])
.range([0, 100]);

console.log(The width of each band is + scale.bandwidth() + pixels)

<script src=https://d3js.org/d3.v5.min.js></script>





As you can see, the bandwidth depends on the number of elements in the domain, the extent of the range and the paddings. Here is the same snippet above, with paddings:





var scale = d3.scaleBand()
.domain([foo, bar, baz, foobar, foobaz])
.range([0, 100])
.paddingOuter(0.25)

console.log(The width of each band is + scale.bandwidth() + pixels)

<script src=https://d3js.org/d3.v5.min.js></script>




[#60369] Friday, October 14, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
analiseb

Total Points: 252
Total Questions: 96
Total Answers: 106

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
analiseb questions
;