D3.js band.domain() Function

The band.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.domain([domain]);

Parameters: This function accepts single parameters as given above and described below.

  • domain: This parameter sets the domain of the scale i.e the minimum and the maximum value.

Return Values: This function does not return anything.

Example 1:



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html lang = "en"
<head
    <meta charset = "UTF-8" /> 
    <meta name = "viewport"
        path1tent = "width=device-width, 
        initial-scale = 1.0"/> 
     <script src=
        "https://d3js.org/d3.v4.min.js">
    </script>    
</head
  
<body
    <script
      // Create band scale with domain
      // and range
      var band = d3.scaleBand()
  
      // Setting domain for the scale
      .domain(["redcolor", "greencolor",
             "blackcolor", "bluecolor"]);
  
      console.log("band(redcolor): ",
              band("redcolor"));
      console.log("band(blackcolor): ",
              band("blackcolor"));
      console.log("band(greencolor): ",
              band("greencolor"));
      console.log("band(bluecolor): ", 
              band("bluecolor"));                        
    </script
</body
</html>

chevron_right


Output:

Example 2:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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
    <script
        // Creating the band scale with
        // specified domain and range.
        var band = d3.scaleBand()
  
        // Setting domain for the scale
                 .domain([10, 20, 30, 40])
                 .range([0, 11]);
  
        console.log("band(10): ", band(10));
        console.log("band(20): ", band(20));
        console.log("band(30): ", band(30));
        console.log("band(40): ", band(40));
    </script
</body
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.