Open In App

D3.js scaleLog() Function

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

The d3.scaleLog() function is used to create a new continuous scale with the user-defined domain and range, the default base is 10. clamping is disabled by default in this scale. 

Syntax:

d3.scaleLog([[domain, ]range])

Parameters: This function accepts two parameters as mentioned above and described below.

  • domain: This parameter always accepts two or more than two numbers. Default value is [1, 10].
  • range: This parameter accepts a number or a string array. It’s default value is [0, 1].

Return Value: This function returns the newly created continuous scale.

Below given are a few examples of the function given above.

Example 1:

html




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" path1tent=
    "width=device-width, initial-scale=1.0" />
      
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <script>
        var log = d3.scaleLog()
            .domain([1, 10])
            .range([10, 20, 30, 40, 50, 60]);
        console.log(log(1));
        console.log(log(2));
        console.log(log(3));
        console.log(log(4));
    </script>
</body>
  
</html>


Output:

Example 2:

html




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" path1tent=
    "width=device-width, initial-scale=1.0" />
  
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <script>
        var log = d3.scaleLog()
            .domain([-1, 1])
            .range([10, 20, 30, 40, 50, 60]);
  
        // Returns NaN as Domain cant be less than one
        console.log("Domain in log scale cannot"
            + " be less than one : ", log(1));
              
        var log = d3.scaleLog()
            .domain([10, 100])
            .range(["red", "green", "blue", "white"]);
        console.log("log(1): ", log(1));
        console.log("log(1.5): ", log(1.5));
    </script>
</body>
  
</html>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads