Open In App

D3.js linkHorizontal() Method

Last Updated : 14 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The d3.linkHorizontal() method returns a new link generator with Horizontal tangents. It is typically used when the root is on the top/bottom edge with the children going down/up.

Syntax:

var link = d3.linkHorizontal()
    .x(function(d) { return d.x; })
    .y(function(d) { return d.y; });

Parameters: This function does not take any parameter.

Return Value: This method returns a new link generator.

Example:

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
 
    <script src=
    </script>
</head>
 
<body>
 
    <h1 style="text-align: center; color: green;">
        GeeksforGeeks
    </h1>
 
    <h3 style="text-align: center;">
        D3.js | linkHorizontal() Method
    </h3>
 
    <center>
    <svg id="gfg" width="200" height="200"></svg>
    </center>
 
    <script>
 
        var data = [
            {source: [100,25], target: [200,175]},
            {source: [100,25], target: [25,175]}];
       
        // Horizontal link generator
        var link = d3.linkHorizontal()
                .source(function(d) {
                    return [d.source[1], d.source[0]];
                })
                .target(function(d) {
                    return [d.target[1], d.target[0]];
                });
       
        //Adding the link paths
        d3.select("#gfg")
        .selectAll("path")
        .data(data)
        .join("path")
        .attr("d", link)
        .classed("link", true);
 
    </script>
</body>
 
</html>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads