Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3js v4: scaleOrdinal does not have the rangePoints()

Tags:

d3.js

I'm migrating the parallel coordinates source code here to the newest d3js version (d3js 4.5.0). I'm stuck to this line:

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

It seems that in d3js v4 the rangePoints function does not exist anymore. I can change to d3.scaleOrdinal(), but it has only the range function, not the rangePoints function. Any clue on for this?

like image 751
Donato Pirozzi Avatar asked Jan 30 '17 17:01

Donato Pirozzi


1 Answers

In D3 v4, two new scales were created: scaleBand and scalePoint, which now have some of the features that scale.ordinal had in version 3 (among them rangePoints).

It's documented in the changelog:

Similarly, the ordinal.rangePoints and ordinal.rangeRoundPoints methods have been replaced with a new subclass of ordinal scale: point scales. The following code in 3.x:

var x = d3.scale.ordinal()
.domain(["a", "b", "c"])
.rangePoints([0, width]);

Is equivalent to this in 4.0:

var x = d3.scalePoint()
.domain(["a", "b", "c"])
.range([0, width]);

Therefore, the scale you need here is scalePoint:

Point scales are a variant of band scales with the bandwidth fixed to zero. Point scales are typically used for scatterplots with an ordinal or categorical dimension.

Thus, change your scale to:

var x = scalePoint()
    .range([0, width])
    .padding(.1);

Pay attention to the padding: in a point scale, range accepts only the range array, not the padding anymore.

like image 145
Gerardo Furtado Avatar answered Oct 16 '22 06:10

Gerardo Furtado