Wednesday, January 21, 2026
HomeLanguagesJavascriptD3.js scaleThreshold() Function

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:

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS