How to get the type of scale in D3?

If I create two scales:

a = d3.scale.ordinal() b = d3.scale.linear() 

How can I find out what is ordinal and linear? Something like d3.scale.isOrdinal(a)

+7
source share
2 answers

there is no direct way to find out i.e. there is no scaling function property that tells you what type of scale it has.

The best way to do this is to check the scaling interface by checking the presence / absence of any configuration method present in one of the types and not the other.

For example:

 typeof a.rangePoints === "function" typeof b.rangePoints === "undefined" 

The ordinal scale provides rangePoints, and the linear scale does not

+5
source

Add your own property type when creating the scale:

 var scaleType = { LINEAR: "LINEAR", POWER: "POWER", LOG: "LOG", ORDINAL: "ORDINAL" }; var scale_a = d3.scale.ordinal() .domain([1,2,3]) .range([0,100]); scale_a.type = scaleType.ORDINAL; var scale_b = d3.scale.linear() .domain([0,100]) .range([0,100]); scale_b.type = scaleType.LINEAR; 
+8
source

All Articles