Thursday, September 4, 2025
HomeLanguagesJavascriptD3.js time.clamp() Function

D3.js time.clamp() Function

The time.clamp() function in D3.js is used to enable or disable the clamp. If the clamp is disabled then the range of the return value may be outside the given range.

Syntax:

time.clamp( clamp )

Parameters: This function accepts only one parameter as given above and described below:

  • clamp: It is a boolean value that defines if the clamp is enabled or disabled.

Return Values: This function does not return anything.

Below programs illustrate the time.clamp() function in D3.js:

Example 1: When the clamp is set to false.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
    <script src=
        "https://d3js.org/d3-color.v1.min.js">
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color: green">neveropen</h1>
  
    <p>time.clamp() Function </p>
  
    <script>
  
        var time = d3.scaleTime()
            // Setting domain for the scale.
            .domain([1, 100])
            .range([1, 10])
            .clamp(false);
  
        document.write("<h3>time(900): " +
            time(900) + "</h3>");
        document.write("<h3>time(100.5): " +
            time(100.5) + "</h3>");
        document.write("<h3>time(888): " +
            time(888) + "</h3>");
        document.write("<h3>time(150): " +
            time(150) + "</h3>");
    </script>
</body>
  
</html>


Output:

Example 2: When clamp is set to true.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
    <script src=
        "https://d3js.org/d3-color.v1.min.js">
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color: green">neveropen</h1>
  
    <p>time.clamp() Function </p>
  
    <script>
  
        var time = d3.scaleTime()
            // Setting domain for the scale.
            .domain([1, 100])
            .range([1, 10])
            .clamp(true);
  
        document.write("<h3>time(900): "
            + time(900) + "</h3>");
        document.write("<h3>time(100.5): "
            + time(100.5) + "</h3>");
        document.write("<h3>time(888): "
            + time(888) + "</h3>");
        document.write("<h3>time(150): "
            + time(150) + "</h3>");
    </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
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS