var W = 100;
var H = 200;
var data = [{v:4}, {v:8}, {v:15}, {v:16}, {v:23}, {v:42}];
var x = d3.scale.linear().domain([0, max_x]).range([0, W]);
var y = d3.scale.ordinal().domain([0, 1, 2, 3, 4, 5]).rangeBands([0, H]);
How do I automatically enumerate the domain of data without typing it out e.g., 0, 1, 2, 3
I have tried domain(data)
, and domain([0, data.length])
, but I need all the values in between.
D3 creates a function myScale which accepts input between 0 and 10 (the domain) and maps it to output between 0 and 600 (the range). Scales are mainly used for transforming data values to visual variables such as position, length and colour.
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 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.
domain() function in d3. js is used to set the domain of the scale. The first value that is present in the domain will be mapped to the first band in the range array and so on. Syntax: band.
If you want the domain of the ordinal scale to be the indexes of your data, then use d3.range. For example, d3.range(data.length)
returns [0, 1, 2, 3, 4, 5]
.
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