<!DOCTYPE html style="width: 100%, height: 100%">
<meta charset="UTF-8">
<style>
	svg {
      width: 100%;
      height: 100%;
    }
    .node rect {
        stroke-width: 80px;
    }
    
    .node text {
        font: 22px sans-serif;
        fill: #fff;
    }
    
    .link {
        fill: none;
        stroke: #ccc;
        stroke-width: 2px;
    }
</style>

<body style="width: 100%, height: 100%">

    <!-- load the d3.js library -->
    <script src="d3.min.js"></script>
    <script>

var treeData =
  {
    "name": "Click on me","subname": " (zoom, drag and follow the path)", "fill":"#ff0000", "rectWidth" : 400,"children": [
      { 
        "name": "I examine the",
        "subname": "properties of a group",    
        "fill":"#ff9f00",
        "rectWidth" : 250, "children":[{
          "name": "I examine social diffusion",
          "subname" : "(disease, information, new behavior, energy)",
          "fill" : "#ffc100",
          "rectWidth": 500, "children": [
          {"name": "Is the probability of diffusion dependent",
            "subname": "on the frequency of interactions/associations?",
            "fill" : "#9ad60b",
            "rectWidth": 500,
            "children" : [
              {"name": "Yes",
                "fill" : "#4ce614",
                "rectWidth": 50, "children":[
                  {"name": "Does the diffusion show ",
                   "subname": "directional patterns?",  
                    "fill" : "#0fb2cf",
                    "rectWidth": 260,
                    "children" : [
                      {"name" : "Yes",
                        "fill": "#0077ff",
                        "rectWidth": 50, "children":[
                          {"name": "Directed, through strongest or weakest links global efficiency",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 610},
                          {"name": "Directed, through strongest or weakest links diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 610}
                        ]},

                      {"name" : "No",
                        "fill": "#0077ff",
                        "rectWidth": 50, "children":[
                          {"name": "Undirected, through strongest or weakest links global efficiency",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 650},
                          {"name": "Undirected, through strongest or weakest links diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 610}
                        ]},
                    ]},
                ]},
              {"name": "No",
                "fill" : "#4ce614",
                "rectWidth": 50, "children":[
                {"name": "Does the diffusion show ",
                	"subname": "directional patterns?", 
                	"fill": "#0fb2cf",
                	"rectWidth": 260,"children" : [
                      {"name" : "Yes",
                        "fill": "#4ce614",
                        "rectWidth": 50, "children":[
                          {"name": "Directed, unweighted global efficiency",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 420},
                          {"name": "Directed, unweighted diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 420}
                        ]},
                      {"name" : "No",
                        "fill": "#4ce614",
                        "rectWidth": 50, "children":[
                          {"name": "Undirected, unweighted global efficiency",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 420},
                          {"name": "Undirected, unweighted diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 420},
                          {"name": "Density",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 100}
                        ]},
                    ]}
                ]}
            ]
          }]
        },
        {
          "name": "I examine network connectivity",
          "subname" : "(cohesion, resilience)",
          "fill" : "#ffc100",
          "rectWidth": 500, "children": [
          {"name": "Are links' weights important?",
            "subname": "",
            "fill" : "#9ad60b",
            "rectWidth": 400,
            "children" : [
              {"name": "Yes",
                "fill" : "#4ce614",
                "rectWidth": 50, "children":[
                  {"name": "Is directionality important?",
                   "subname": "",  
                    "fill" : "#0fb2cf",
                    "rectWidth": 300,
                    "children" : [
                      {"name" : "Yes",
                        "fill": "#4ce614",
                        "rectWidth": 50, "children":[
                          {"name": "Directed, through strongest or weakest links diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 610}
                        ]},
                      {"name" : "No",
                        "fill": "#4ce614",
                        "rectWidth": 50, "children":[
                          {"name": "Undirected, through strongest or weakest links diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 610}
                        ]},
                    ]},
                ]},
              {"name": "No",
                "fill" : "#4ce614",
                "rectWidth": 50, "children":[
                {"name": "Is directionality important?",
                	"subname": "", 
                	"fill": "#0fb2cf",
                	"rectWidth": 300,"children" : [
                      {"name" : "Yes",
                        "fill": "#4ce614",
                        "rectWidth": 50, "children":[
                          {"name": "Directed, unweighted diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 420},
                        ]},
                      {"name" : "No",
                        "fill": "#4ce614",
                        "rectWidth": 50, "children":[
                          {"name": "Undirected, unweighted diameter",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 420},
                           {"name": "Density",
                          "subname" : "",
                          "fill" : "#191fcd",
                          "rectWidth" : 100}
                        ]},
                    ]}
                ]}
            ]
          }]
        },
        ]
      },
      { "name": "I examine",
      "subname": "interaction patterns" ,"fill":"#ff9f00", "rectWidth" : 250, "children":[
      	{"name" : "I examine with whom", "subname": "individuals interact","fill":"#ffc100", "rectWidth" : 250 , "children":[
      		{"name": "Are the weights of", "subname": "interactions/associations important?", "fill":"#9ad60b", "rectWidth" : 400, "children":[
      			{"name": "Yes", "fill" : "#4ce614", "rectWidth": 50, "children":[
      				{"name": "Are individual", "subname": "characteristics categorical?", "fill":"#0fb2cf","rectWidth" : 300, "children":[
      					{"name": "Yes", "fill" : "#0077ff", "rectWidth": 50, "children":[
      						{"name": "Weighted categorical", "subname": "assortativity","fill":"#191fcd", "rectWidth" : 250 }
      					]},
      					{"name": "No", "fill" : "#0077ff", "rectWidth": 50, "children":[
							{"name": "Weighted continuous", "subname": "assortativity","fill":"#191fcd", "rectWidth" : 250 }
      					]},
      				]}
      			]},
      			{"name": "No", "fill" : "#4ce614", "rectWidth": 50, "children":[
      				{"name": "Are individual", "subname": "characteristics categorical?", "fill":"#0fb2cf","rectWidth" : 300, "children":[
      					{"name": "Yes", "fill" : "#0077ff", "rectWidth": 50, "children":[
      						{"name": "Unweighted categorical", "subname": "assortativity","fill":"#191fcd", "rectWidth" : 250 }
      					]},
      					{"name": "No", "fill" : "#0077ff", "rectWidth": 50, "children":[
      						{"name": "Unweighted continuous", "subname": "assortativity","fill":"#191fcd", "rectWidth" : 250 }
      					]},
      				]}
      			]},
      		]}
      	]}
	  ]},
      { "name": "I examine how individual characteristics","subname": "affect the heterogeneity of social interactions","fill":"#ff9f00", "rectWidth" : 450, "children": [
          { "name": "I examine sociality aspects", "subname": "","fill":"#ffc100", "rectWidth" : 300, "children":[
          	{"name": "Are link weights ", "subname": "important?","fill":"#9ad60b", "rectWidth" : 260, "children":[
          		{"name":"Yes","subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               		{"name": "Is directionality", "subname": "important?","fill":"#0fb2cf", "rectWidth" : 260, "children":[
               			{"name": "Yes", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "instrength", "subname": "","fill":"#191fcd", "rectWidth" : 200},
               				{"name": "outstrength", "subname": "","fill":"#191fcd", "rectWidth" : 200},
               				{"name": "Directed eigenvector", "subname": "(through incoming or outgoing link symmetrization prior to computation)","fill":"#191fcd", "rectWidth" : 800}
               			]},
               			{"name": "No", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "strength", "subname": "","fill":"#191fcd", "rectWidth" : 200},
               				{"name": "Undirected eigenvector", "subname": "","fill":"#191fcd", "rectWidth" : 300}
               			]}
               		]}
               ]},
               {"name":"No","subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               		{"name": "Is directionality", "subname": "important?","fill":"#0fb2cf", "rectWidth" : 260, "children":[
               			{"name": "Yes", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "indegree", "subname": "","fill":"#191fcd", "rectWidth" : 200},
               				{"name": "outdegree", "subname": "","fill":"#191fcd", "rectWidth" : 200},
               				{"name": "Directed binary eigenvector", "subname": "(through incoming or outgoing link symmetrization prior to computation)","fill":"#191fcd", "rectWidth" : 800}
               			]},
               			{"name": "No", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "degree", "subname": "","fill":"#191fcd", "rectWidth" : 200},
               				{"name": "Undirected binary eigenvector", "subname": "","fill":"#191fcd", "rectWidth" : 400}
               			]}
               		]}
               ]}
          	]}               
          ]},
          { "name": "I examine social diffusion", "subname": "(disease, information, new behavior, energy)","fill":"#ffc100", "rectWidth" : 450, "children": [
          	{"name": "Is the probability of diffusion  dependent on", "subname": "the frequency of interactions/associations?","fill":"#9ad60b", "rectWidth" : 450, "children":[
          		{"name":"Yes","subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               		{"name": "Does the diffusion show", "subname": "directional patterns?","fill":"#0fb2cf", "rectWidth" : 260, "children":[
               			{"name": "Yes", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "Outgoing weighted", "subname": "betweenness","fill":"#191fcd", "rectWidth" : 300}
               			]},
               			{"name": "No", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "Undirected weighted", "subname": "betweenness","fill":"#191fcd", "rectWidth" : 300}
               			]}
               		]}
               ]},
               {"name":"No","subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               		{"name": "Is diffusion unidirectional?", "subname": "","fill":"#0fb2cf", "rectWidth" : 260, "children":[
               			{"name": "Yes", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "Outgoing unweighted", "subname": "betweenness","fill":"#191fcd", "rectWidth" : 300},
               				{"name": "Directed binary eigenvector", "subname": "(through incoming or outgoing link symmetrization prior to computation)","fill":"#191fcd", "rectWidth" : 800}
               			]},
               			{"name": "No", "subname": "","fill":"#4ce614", "rectWidth" : 50, "children":[
               				{"name": "Undirected unweighted", "subname": "betweenness","fill":"#191fcd", "rectWidth" : 300},
               				{"name": "degree", "subname": "","fill":"#191fcd", "rectWidth" : 200},
               			]}
               		]}
               ]}
          	]}
          ]}
        ]
        }
    ]
  };

// Set the dimensions and margins of the diagram
var margin = {top: 20, right: 90, bottom: 30, left: 90},
    width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
    height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)

// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
    //.attr("width", width + margin.right + margin.left)
    //.attr("height", height + margin.top + margin.bottom)
    .call(d3.zoom()
    .scaleExtent([1 / 32, 8])
    .on("zoom", zoomed))
  .append("g")
    .attr("transform", "translate("
          + margin.left + "," + margin.top + ")");

function zoomed() {
  svg.attr("transform", d3.event.transform);
}

var i = 0,
    duration = 750,
    root;

// declares a tree layout and assigns the size
var treemap = d3.tree()
	.separation(function(a, b) { return ((a.parent == root) && (b.parent == root)) ? 1 : 1; })
	.size([height, width]);

// Assigns parent, children, height, depth
root = d3.hierarchy(treeData, function(d) { return d.children; });
root.x0 = height / 2;
root.y0 = 0;

// Collapse after the second level

 collapse(root);
update(root);

// Collapse the node and all it's children
function collapse(d) {
  if(d.children) {
    d._children = d.children;
    d.children.forEach(collapse)
    d.children = null
  }
}


function update(source) {

  // Assigns the x and y position for the nodes
  var treeData = treemap(root);

  // Compute the new tree layout.
  var nodes = treeData.descendants(),
      links = treeData.descendants().slice(1);

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

  // ****************** Nodes section ***************************

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

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

    var rectHeight = 80;
    
    nodeEnter.append('rect')
      .attr('class', 'node')
      .attr("width", function(d){return d.data.rectWidth})
      .attr("height", rectHeight)
      .attr("x", 0)
      .attr("y", (rectHeight/2)*-1)
      .attr("rx","5")
      .style("fill", function(d) {
          return d.data.fill;
      });

  // Add labels for the nodes
  nodeEnter.append('text')
      .attr("dy", "-.35em")
      .attr("x", function(d) {
        return 13;
      })
      .attr("text-anchor", function(d) {
        return "start";
      })
      .text(function(d) { return d.data.name; })
      .append("tspan")
      .attr("dy", "1.75em")
      .attr("x", function(d) {
        return 13;
      })
      .text(function(d) { return d.data.subname; });

  // UPDATE
  var nodeUpdate = nodeEnter.merge(node);

  // Transition to the proper position for the node
  nodeUpdate.transition()
    .duration(duration)
    .attr("transform", function(d) { 
        return "translate(" + d.y + "," + d.x + ")";
     });

  // Update the node attributes and style
  nodeUpdate.select('circle.node')
    .attr('r', 10)
    .style("fill", function(d) {
        return d._children ? "lightsteel#4ce614" : "#fff";
    })
    .attr('cursor', 'pointer');


  // Remove any exiting nodes
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) {
          return "translate(" + source.y + "," + source.x + ")";
      })
      .remove();

  // On exit reduce the node circles size to 0
  nodeExit.select('circle')
    .attr('r', 1e-6);

  // On exit reduce the opacity of text labels
  nodeExit.select('text')
    .style('fill-opacity', 1e-6);

  // ****************** links section ***************************

  // Update the links...
  var link = svg.selectAll('path.link')
      .data(links, function(d) { return d.id; });

  // Enter any new links at the parent's previous position.
  var linkEnter = link.enter().insert('path', "g")
      .attr("class", "link")
      .attr('d', function(d){
        var o = {x: source.x0, y: source.y0}
        return diagonal(o, o)
      });

  // UPDATE
  var linkUpdate = linkEnter.merge(link);

  // Transition back to the parent element position
  linkUpdate.transition()
      .duration(duration)
      .attr('d', function(d){ return diagonal(d, d.parent) });

  // Remove any exiting links
  var linkExit = link.exit().transition()
      .duration(duration)
      .attr('d', function(d) {
        var o = {x: source.x, y: source.y}
        return diagonal(o, o)
      })
      .remove();

  // Store the old positions for transition.
  nodes.forEach(function(d){
    d.x0 = d.x;
    d.y0 = d.y;
  });

  // Creates a curved (diagonal) path from parent to the child nodes
  function diagonal(s, d) {

    path = `M ${s.y} ${s.x}
            C ${(s.y + d.y) / 2} ${s.x},
              ${(s.y + d.y) / 2} ${d.x},
              ${d.y} ${d.x}`

    return path
  }

  // Toggle children on click.
function click(d) {
        if (d.children) {
          d._children = d.children;
          d.children = null;
        } 
        else {
          d.children = d._children;
          d._children = null;
        }
        if (d.parent && d.children) { //add here, 
          d.parent.children.forEach(function(element) {
        if (d !== element) {
        collapse(element);
        }});
      }
        update(d);
      }
}
</script>
</body>