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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With