Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify domain in d3.scale.ordinal() ?

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.

like image 452
nickponline Avatar asked Apr 23 '12 16:04

nickponline


People also ask

What are the domain and range of a d3 scale?

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.

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 scale ordinal in d3?

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.

What is domain in d3 JS?

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.


1 Answers

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].

like image 147
mbostock Avatar answered Sep 22 '22 19:09

mbostock