D3.js axisTop() Function

Axes can be drawn using built-in D3 functions. This is made of Lines, Ticks and Labels. The d3.axisTop() function in D3.js is used to create a top horizontal axis. This function will construct a new top-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.axisTop(scale)

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

  • scale: This parameter is the scale used.

Return Value: This function returns a top horizontal axis.



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

Example 1:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        D3.js | d3.axisTop() 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 xscale = d3.scaleLinear()
            .domain([0, 100])
            .range([0, width - 50]);
  
        var x_axis = d3.axisTop(xscale);
  
        svg.append("g")
            .attr("transform", "translate(20,100)")
            .call(x_axis)
    </script>
</body>
  
</html>

chevron_right


Output:

Example 2:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        D3.js | d3.axisTop() 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: end;
        }
    </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 xscale = d3.scaleLinear()
            .domain([d3.min(data), d3.max(data)])
            .range([0, width - 60]);
  
        var x_axis = d3.axisTop(xscale);
  
        var xAxisTranslate = height / 2;
  
        svg.append("g")
            .attr("transform", "translate(50, "
            + xAxisTranslate + ")")
            .call(x_axis)
    </script>
</body>
  
</html>

chevron_right


Output:

Reference: https://devdocs.io/d3~5/d3-axis#axisTop

full-stack-img




My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.