Augustus
Augustus

Reputation: 634

How to get the vertically-oriented tree using d3.js with rectangular box

The following url is going to get vertical-oriented tree using d3.

How to get the vertically-oriented tree using d3.js

I followed the below answered :

https://i.sstatic.net/EDsDY.png

My requirement is to draw the above one in reverse order with marker in each link in the middle of the link. Please suggest a proper valid solution for this requirement.

Tried so far

<html>
<head>
  <title>Test</title>
</head>
<body>
<script type="text/javascript" src="d3.v2.min.js"></script>
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
<script>

var margin = {top: 100, right: 50, bottom: 100, left: 50},
    width = 2200 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var tree = d3.layout.tree()
    .separation(function(a, b) { return a.parent === b.parent ? 1 : 1.2; })
    .children(function(d) { return d.parents; })
    .size([width, height]);

var svg = d3.select("body")
    .attr("bgcolor", "white")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var nodes = tree.nodes(getJsonData());

var node = svg.selectAll(".node")
    .data(nodes)
    .enter()
    .append("g");

node.append("rect")
    .attr("width", 145)
    .attr("height", 80)
    .attr("fill", "#DFF0F0")
    .attr("stroke", "black")
    .attr("stroke-width", "1")
    .attr("x", function(d) { return d.x - 70; })
    .attr("y", function(d) { return height - d.y - 40; });

node.append("text")
    .attr("font-size", "7px")
    .attr("font-weight", "bold")
    .attr("fill", "#181A1A")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return height - d.y - 15; })
    .style("text-anchor", "middle")
    .text(function(d) { return d.accountId; });

node.append("text")
    .attr("font-size", "8px")
    .attr("fill", "blue")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return 10 + height - d.y; })
    .style("text-anchor", "middle")
    .text(function(d) { return d.branchName; });

node.append("text")
    .attr("font-size", "7px")
    .attr("fill", "grey")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return 25 + height - d.y; })
    .style("text-anchor", "middle")
    .text(function(d) { return d.currency; });

node.append("text")
    .attr("font-size", "6px")
    .attr("fill", "grey")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return 35 + height - d.y; })
    .style("text-anchor", "middle")
    .text(function(d) { return d.country; });

node.append("text")
    .attr("font-size", "12px")
    .attr("font-weight", "bold")
    .attr("fill", "#181A1A")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return 7 + height - d.y; })
    .style("text-anchor", "middle")
    .text(function(d) { return d.finIns; });

node.append("text")
    .attr("font-size", "11px")
    .attr("font-weight", "bold")
    .attr("fill", "#181A1A")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return 7 + height - d.y; })
    .style("text-anchor", "middle")
    .text(function(d) { return d.customerName; });

var link = svg.selectAll(".link")
    .data(tree.links(nodes))
    .enter()
    .insert("path", "g")
    .attr("fill", "none")
    .attr("stroke", "#000")
    .attr("stroke", "#000")
    .attr("shape-rendering", "crispEdges")
    .attr("d", connect);

function connect(d, i) {
    return     "M" + d.source.x + "," + (height - d.source.y)
             + "V" + (height - (3*d.source.y + 4*d.target.y)/7)
             + "H" + d.target.x
             + "V" + (height - d.target.y);
};


function getJsonData() {
    return {  
        "customerName" : "ACC",
         parents: [
              {"finIns" : "child",
             parents: [
        {
        "accountId": "024",
        "branchName": "AMSTERDAM",
        "currency": "USD",

        "country": "Netherlands"

    },
    {
        "accountId": "000001",
        "branchName": "RR, N.A.",
        "currency": "USD",

        "country": "United States"

    }
]},] 
}; 
};
</script>
</body>
</html>

Upvotes: 1

Views: 981

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

To do this, you need to invert all of the y coordinates -- that is, where you currently have something like height - d.y, you should have d.y. For the markers, you can use SVG line markers.

Complete example here.

Upvotes: 2

Related Questions