The ordinal.range() function in d3.js is used to set the range for the ordinal scale. If in the range there are less number of elements as compared to domain the values then the scale reuse values of the specified range from starting.
Syntax:
ordinal.range([range]);
Parameters: This function takes only one parameter as given above and described below.
- range: This parameter accepts an array of discrete values.
Return Values: This function does not return anything.
Example 1:
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" /> < meta name = "viewport" path1tent= " width = device -width, initial-scale = 1 .0" /> < script src = </ script > </ head > < body > < h2 style = "color:green" > neveropen </ h2 > < p >ordinal.range() Function </ p > < script > var ordinal = d3.scaleThreshold() // Setting domain for the scale .domain([1, 2, 3, 4]) // Range for the scale .range(["red", "green", "blue"]); document.write("< h3 >ordinal(1): " + ordinal(1) + "</ h3 >"); document.write("< h3 >ordinal(3): " + ordinal(3) + "</ h3 >"); </ script > </ body > </ html > |
Output:
Example 2:
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" /> < meta name = "viewport" path1tent= " width = device -width, initial-scale = 1 .0" /> < script src = </ script > </ head > < body > < h2 style = "color:green" > neveropen </ h2 > < p >ordinal.range() Function </ p > < script > var ordinal = d3.scaleThreshold() // Setting domain for the scale .domain([-2, 3, 4]) // 10 does not lie in the domain document.write("< h3 >ordinal(-3): " + ordinal(-3) + "</ h3 >"); document.write("< h3 >ordinal(10): " + ordinal(10) + "</ h3 >"); </ script > </ body > </ html > |
Output: