D3 Tree Image Organization Schema

Organizasyon Şeması

by Fatih Demir

HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Schema</title>

<link rel="stylesheet" href="style.css" />
</head>

<body>
	
	
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
</html>

CSS

.node {
  cursor: pointer;
}

.node image {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 3.5px;
}

.node text {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

JavaScript

var margin = {top: 50, right: 120, bottom: 20, left: 120},
    width = 960 - margin.right - margin.left,
    height = 800 - margin.top - margin.bottom;

var i = 0,
    duration = 750,
    root;

var tree = d3.layout.tree()
    .size([height, width]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.x, d.y]; });

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.right + margin.left)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.bottom + "," + margin.top + ")");
    
// Json Check link or url or path
d3.json("https://api.myjson.com/bins/17rntt", function(data) {

root = data;
root.x0 = height / 2;
root.y0 = 0;

function collapse(d) {
  if (d.children) {
    d._children = d.children;
    d._children.forEach(collapse);
    d.children = null;
  }
}

root.children.forEach(collapse);
update(root);

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);

  // Normalize for fixed-depth.
  nodes.forEach(function(d) { d.y = d.depth * 180; });

  // Update the nodes…
  var node = svg.selectAll("g.node")
      .data(nodes, function(d) { return d.id || (d.id = ++i); });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + source.x0 + "," + source.y0 + ")"; })
      .on("click", click);

  // add picture
  nodeEnter
    .append('defs')
    .append('pattern')	
    .attr('id', function(d,i){
      return 'pic_' + d.fname + d.lname;
    })
	.attr('patternUnits', 'userSpaceOnUse')
    .attr('height',"90px")
    .attr('width',"90px")
    .attr('x',"45px")
    .attr('y',"45px")
    .append('image')
    .attr('xlink:href',function(d,i){
      return d.photo;
    })
    .attr('height',"90px")
    .attr('width',"90px")
    .attr('x',"0")
   ...