block by denisemauldin 2ba97b6750becc6ca604d4943611e01e

Tree View

Full Screen

Built with blockbuilder.org

forked from anonymous‘s block: Tree View

index.html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
  </style>
</head>

<body>
  <section class="d3-tree">
              <h2>Interactive Tree View</h2>
              <div id="tree" class="">
                <h4 class="pre">Generating Tree. Please wait...
                </h4>
              </div>
            </section>
  <script>
// var ref_link_others = document.getElementById('ref_link_others');
//     function display_others_ref_link(d) {
//       if(d.referral_name){
//         ref_link_others.innerHTML = "https://app.projectethereum.com/register/".concat(d.referral_name);
//       }
//     }

  var userData;
  var url = "/admin/treedata";
  var zm;

  $.ajax({
    url: url
  }).done( function(data){
    userData = data

    $(".pre").html(function(){
      return "";
    });

    var margin = {
        top: 20,
        right: 20,
        bottom: 20,
        left: 20
      },
      width = 500,
      height = 500;

    var svg = d3.select("#tree").append("svg")
      .attr("width", width)
      .attr("height", height)
      .attr("style", 'border: 1px solid black;')
      .call(zm = d3.behavior.zoom().scaleExtent([.1, 3]).on("zoom", redraw)).append("g")
      .attr("transform", "translate(" + 350 + "," + 20 + ")");

      //necessary so that zoom knows where to zoom and unzoom from
      zm.translate([350, 20]);


    var root = {
      "name": "Test Account" ,
      "level": "0",
      "referral_name": "test",
      "children": []
    };

    var i = 0,
      duration = 600,
      rectW = 120,
      rectH = 50;

    var tree = d3.layout.tree().nodeSize([130, 60]);
    var diagonal = d3.svg.diagonal()
    .projection(function(d) {
      return [d.x + rectW / 2, d.y + rectH / 2];
    });

    //Redraw for zoom
    function redraw() {
      svg.attr("transform",
        "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
    }

  

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

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

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

    d3.select("#tree").style("height", "500px");

    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 * 60;
      });

      // 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).on("mouseover", display_others_ref_link);

      nodeEnter.append("rect")
        .attr("width", rectW)
        .attr("height", rectH)
        .attr("stroke", "black")
        .attr("stroke-width", 1)
        .style("fill", function(d) {
          if (d.active == 0) {
            return d._children ? "sandybrown": "peachpuff";
          } else {
            return d._children ? "gainsboro" : "#fff";
          }
        });

      nodeEnter.append("text")
        .attr("x", rectW / 2)
        .attr("y", rectH / 2)
        .attr("dy", "0em")
        .attr("text-anchor", "middle")
        .text(function(d) {
          return d.name;
        });

      nodeEnter.append("text")
        .attr("x", rectW / 15)
        .attr("y", rectH / 4)
        .attr("dy", "0em")
        .attr("text-anchor", "middle")
        .text(function(d) {
          return d.level;
        });

      nodeEnter.append("text")
        .attr("x", rectW / 2)
        .attr("y", rectH / 2)
        .attr("dy", "1.6em")
        .attr("text-anchor", "middle")
        .text(function(d) {
          return d.referral_name;
        });

      //nodeEnter.append("text")
      //  .attr("x", rectW / 2)
      //  .attr("y", rectH / 2)
      //  .attr("dy", "2em")
      //  .attr("text-anchor", "middle")
      //  .text(function(d) {
      //    return d.email;
      //  });

      // Transition nodes to their new position.
      var nodeUpdate = node.transition()
        .duration(duration)
        .attr("transform", function(d) {
          return "translate(" + d.x + "," + d.y + ")";
        });

      nodeUpdate.select("rect")
        .attr("width", rectW)
        .attr("height", rectH)
        .attr("stroke", "black")
        .attr("stroke-width", 1)
        .style("fill", function(d) {
          if (d.active == 0) {
            return d._children ? "sandybrown": "peachpuff";
          } else {
            return d._children ? "gainsboro" : "#fff";
          }
        });

      nodeUpdate.select("text")
        .style("fill-opacity", 1);

      // Transition exiting nodes to the parent's new position.
      var nodeExit = node.exit().transition()
        .duration(duration)
        .attr("transform", function(d) {
          return "translate(" + source.x + "," + source.y + ")";
        })
        .remove();

      nodeExit.select("rect")
        .attr("width", rectW)
        .attr("height", rectH)
        .attr("stroke", "black")
        .attr("stroke-width", 1)

      nodeExit.select("text");

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

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

      // Transition links to their new position.
      link.transition()
        .duration(duration)
        .attr("d", diagonal);

      // Transition exiting nodes to the parent's new position.
      link.exit().transition()
        .duration(duration)
        .attr("d", function(d) {
          var o = {
            x: source.x,
            y: source.y
          };
          return diagonal({
            source: o,
            target: o
          });
        })
        .remove();

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

    // Toggle children on click.
    function click(d) {
      //If it has no children (Hidden or not hidden), AJAX request that name
      if(!d.children && !d._children){

          $.ajax({
            url: "/account/9397/yield_children/" + d.referral_name + "/" + d.level + "?format=json",
            })
          .done( function(data){
            fakeDB = $.extend(true, fakeDB, data);

            var data = $.extend(true, {}, fakeDB[d.referral_name])

             for (var i = 0; i < data.children.length; i++) {
               data.children[i] = $.extend(true, {}, fakeDB[data.children[i]])
               delete data.children[i].children
             };
             d.children = data.children

             update(d)

          });

      }
      else if (d.children) {
        d._children = d.children;
        d.children = null;
      } else {
        d.children = d._children;
        d._children = null;
      }
      update(d);
    }

  });

      

  </script>
</body>