Open In App

D3.js scaleThreshold() Function

The d3.scaleThreshold() function in D3.js is used to create and return a new threshold scale that has the specified domain and range. The default value of domain is [0.5] and that of range is [0, 1].

Syntax:



d3.scaleThreshold( domain, range )

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

Return Values: This function does not return anything.



Below programs illustrate the d3.scaleThreshold() function in D3.js:

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <h2 style="color: green;">GeekforGeeks</h2>
  
    <p>d3.scaleThreshold() Function </p>
  
    <script>
        var threshold = d3.scaleThreshold()
  
            // Setting domain and range for the scale
            .domain([10])
            .range(["red", "green", "black", "blue"]);
  
        document.write("<h4>" + threshold(9) + "</h4>");
        document.write("<h4>" + threshold(10) + "</h4>");
    </script>
</body>
  
</html>

Output:

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <h2 style="color: green;">GeekforGeeks</h2>
  
    <p>d3.scaleThreshold() Function </p>
  
    <script>
        var threshold = d3.scaleThreshold()
  
            // Setting domain and range for the scale
            .domain([1, 2, 10])
            .range([0, 1, 2, 3]);
  
        document.write("<h4>" + threshold(2) + "</h4>");
        document.write("<h4>" + threshold(10) + "</h4>");
    </script>
</body>
  
</html>

Output:


Article Tags :