Open In App

D3.js axisRight() Function

Last Updated : 28 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Axes can be drawn using built-in D3 functions. This is made of Lines, Ticks and Labels. The d3.axisRight() function in D3.js is used to create a vertical right-oriented axis. This function will construct a new right-oriented axis generator for the given scale, with empty tick arguments, a tick size of 6 and padding of 3.

Axis API can be configured using the following script.

<script src = "https://d3js.org/d3-axis.v1.min.js"></script>

Syntax:

d3.axisRight( scale )

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

  • scale: This parameter holds the used scale.

Return Value: This function returns the created a vertical right-oriented axis.

Below programs illustrate the d3.axisRight() function in D3.js:

Example 1:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        D3.js | d3.axisRight() Function
    </title>
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <script>
        var width = 400, height = 400;
        var svg = d3.select("body")
            .append("svg")
            .attr("width", width)
            .attr("height", height);
  
        var yscale = d3.scaleLinear()
            .domain([0, 100])
            .range([height - 50, 0]);
  
        var y_axis = d3.axisRight(yscale);
  
        svg.append("g")
            .attr("transform", "translate(100,10)")
            .call(y_axis)
    </script>
</body>
  
</html>


Output:

Example 2:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        D3.js | d3.axisRight() Function
    </title>
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <style>
        svg text {
            fill: green;
            font: 15px sans-serif;
            text-anchor: start;
        }
    </style>
</head>
  
<body>
    <script>
        var width = 400, height = 400;
        var data = [10, 12, 14, 16, 18, 20];
        var svg = d3.select("body")
            .append("svg")
            .attr("width", width)
            .attr("height", height);
  
        var yscale = d3.scaleLinear()
            .domain([d3.min(data), d3.max(data)])
            .range([height - 50, 0]);
  
        var y_axis = d3.axisRight(yscale);
  
  
        svg.append("g")
            .attr("transform", "translate(100,20)")
            .call(y_axis)
    </script>
</body>
  
</html>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads