Open In App

D3.js scaleThreshold() Function

Last Updated : 31 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • domain: It defines the minimum and maximum value for the scale. It is an optional parameter. The default value is [0.5].
  • range: It accepts a range of discrete values, Every value in domain maps with a value in the range. It is an optional parameter. The default range is [0, 1].

Return Values: This function does not return anything.

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

Example 1:

HTML




<!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:

HTML




<!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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads