Friday, January 30, 2026
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=
    </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=
    </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

1 COMMENT

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS