toolkit/javascript/d3/src/layout/partition.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 d3.layout.partition = function() {
       
     2   var hierarchy = d3.layout.hierarchy(),
       
     3       size = [1, 1]; // width, height
       
     4 
       
     5   function position(node, x, dx, dy) {
       
     6     var children = node.children;
       
     7     node.x = x;
       
     8     node.y = node.depth * dy;
       
     9     node.dx = dx;
       
    10     node.dy = dy;
       
    11     if (children && (n = children.length)) {
       
    12       var i = -1,
       
    13           n,
       
    14           c,
       
    15           d;
       
    16       dx = node.value ? dx / node.value : 0;
       
    17       while (++i < n) {
       
    18         position(c = children[i], x, d = c.value * dx, dy);
       
    19         x += d;
       
    20       }
       
    21     }
       
    22   }
       
    23 
       
    24   function depth(node) {
       
    25     var children = node.children,
       
    26         d = 0;
       
    27     if (children && (n = children.length)) {
       
    28       var i = -1,
       
    29           n;
       
    30       while (++i < n) d = Math.max(d, depth(children[i]));
       
    31     }
       
    32     return 1 + d;
       
    33   }
       
    34 
       
    35   function partition(d, i) {
       
    36     var nodes = hierarchy.call(this, d, i);
       
    37     position(nodes[0], 0, size[0], size[1] / depth(nodes[0]));
       
    38     return nodes;
       
    39   }
       
    40 
       
    41   partition.size = function(x) {
       
    42     if (!arguments.length) return size;
       
    43     size = x;
       
    44     return partition;
       
    45   };
       
    46 
       
    47   return d3_layout_hierarchyRebind(partition, hierarchy);
       
    48 };