D3 4.0 rangeRoundBands equivalent?

I see a lot of D3 code that has something like this:

var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); 

Starting from D3 version 4.0, d3.scale.ordinal() now d3.scaleOrdinal and rangeRoundBands seem to be gone.

 > d3.scaleOrdinal() { [Function: scale] domain: [Function], range: [Function], unknown: [Function], copy: [Function] } 

What would be the equivalent of D3 v4 of this code (from Mike Bostock example histogram )?

 var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); 
+42
javascript
May 31 '16 at 14:25
source share
3 answers

In D3 4.x, rangeRoundBands been moved to a new range scale:

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

This is equivalent to:

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

Here is the API: https://github.com/d3/d3-scale#band-scales

+47
May 31 '16 at 2:39 pm
source share
 var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); 

The above calculates the stripes and sets the indentation between the stripes. In v4, the equivalent is

 var x = d3.scaleBand() .rangeRound([0, width]) .padding(0.1); 
+30
Oct 24 '16 at 23:42
source share
 var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 60}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom; var x = d3.scaleBand().rangeRound([0, width]).padding(0.1), y = d3.scaleLinear().rangeRound([height, 0]); var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.tsv("data.txt", function(d) { dy = +dy; return d; }, function(error, data) { if (error) throw error; x.domain(data.map(function(d) { return dx; })); y.domain([0, d3.max(data, function(d) { return dy; })]); 

Runable syntax for a classic chart using both scaleBand and scaleLinear.

-2
Jun 28 '17 at 14:05
source share



All Articles