Wednesday, July 3, 2024
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!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments