Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 difference between ordinal and linear scales

var xScale = d3.scale.ordinal().domain([0, d3.max(data)]).rangeRoundBands([0, w], .1); var yScale = d3.scale.linear().domain([0, data.length]).range([h, 0]); 

I'm confused about when to use ordinal or linear scale in D3.

Below is what I've discovered from the API doc, still bit lost... if anyone can help, it would be much appreciated.

ordinal(x)

Given a value x in the input domain, returns the corresponding value in the output range.

If the range was specified explicitly (as by range, but not rangeBands, rangeRoundBands or rangePoints), and the given value x is not in the scale’s domain, then x is implicitly added to the domain; subsequent invocations of the scale given the same value x will return the same value y from the range.

d3.scale.linear()

Constructs a new linear scale with the default domain [0,1] and the default range [0,1]. Thus, the default linear scale is equivalent to the identity function for numbers; for example linear(0.5) returns 0.5.

like image 780
Bill Avatar asked Apr 21 '15 23:04

Bill


People also ask

What is d3 scale linear?

d3. scaleLinear constructs creates a scale with a linear relationship between input and output. lin = ƒ(t) By default, the scale's domain and range are both the interval [0,1], and the scale can be expressed as the identity function: y = x.

What is d3 scale ordinal ()?

The d3. scaleOrdinal() function is used to create and return the ordinal scale which has the specified range and domain. If the domain and range are not given then both are set to empty array. These types of scales have a discrete domain and range.

Is the domain of an ordinal scale discrete?

Ordinal scales have a discrete domain, such as a set of names or categories. There are also [[quantitative scales|Quantitative-Scales]], which have a continuous domain, such as the set of real numbers.

What is domain and range in d3?

The domain is the complete set of values, so in this case that is all of your temperatures, from 33 to 64. The range is the set of resulting values of a function, in this case the resulting values of scaling your temperatures from 0 to 600.


1 Answers

As for Ordinal Scales:

Ordinal scales have a discrete domain, such as a set of names or categories.

An ordinal scale's values must be coercible to a string, and the stringified version of the domain value uniquely identifies the corresponding range value.

So, as an example, a domain of an ordinal scale may contain names, like so:

var ordinalScale = d3.scale.ordinal()         .domain(['Alice', 'Bob'])         .range([0, 100]);  ordinalScale('Alice'); // 0 ordinalScale('Bob'); // 100 

Notice how all values are strings. They cannot be interpolated. What is between 'Alice' and 'Bob'? I don't know. Neither does D3.

Now, as for Quantitative Scales (e.g. Linear Scales):

Quantitative scales have a continuous domain, such as the set of real numbers, or dates.

As an example, you can construct the following scale:

var linearScale = d3.scale.linear()         .domain([0, 10])         .range([0, 100]);  linearScale(0); // 0 linearScale(5); // 50 linearScale(10); // 100 

Notice how D3 is able to interpolate 5 even if we haven't specified it explicitly in the domain.

Take a look at this jsfiddle to see the above code in action.

like image 138
Oleg Avatar answered Oct 19 '22 19:10

Oleg