toolkit/javascript/d3/src/core/nest.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 d3.nest = function() {
       
     2   var nest = {},
       
     3       keys = [],
       
     4       sortKeys = [],
       
     5       sortValues,
       
     6       rollup;
       
     7 
       
     8   function map(array, depth) {
       
     9     if (depth >= keys.length) return rollup
       
    10         ? rollup.call(nest, array) : (sortValues
       
    11         ? array.sort(sortValues)
       
    12         : array);
       
    13 
       
    14     var i = -1,
       
    15         n = array.length,
       
    16         key = keys[depth++],
       
    17         keyValue,
       
    18         object,
       
    19         o = {};
       
    20 
       
    21     while (++i < n) {
       
    22       if ((keyValue = key(object = array[i])) in o) {
       
    23         o[keyValue].push(object);
       
    24       } else {
       
    25         o[keyValue] = [object];
       
    26       }
       
    27     }
       
    28 
       
    29     for (keyValue in o) {
       
    30       o[keyValue] = map(o[keyValue], depth);
       
    31     }
       
    32 
       
    33     return o;
       
    34   }
       
    35 
       
    36   function entries(map, depth) {
       
    37     if (depth >= keys.length) return map;
       
    38 
       
    39     var a = [],
       
    40         sortKey = sortKeys[depth++],
       
    41         key;
       
    42 
       
    43     for (key in map) {
       
    44       a.push({key: key, values: entries(map[key], depth)});
       
    45     }
       
    46 
       
    47     if (sortKey) a.sort(function(a, b) {
       
    48       return sortKey(a.key, b.key);
       
    49     });
       
    50 
       
    51     return a;
       
    52   }
       
    53 
       
    54   nest.map = function(array) {
       
    55     return map(array, 0);
       
    56   };
       
    57 
       
    58   nest.entries = function(array) {
       
    59     return entries(map(array, 0), 0);
       
    60   };
       
    61 
       
    62   nest.key = function(d) {
       
    63     keys.push(d);
       
    64     return nest;
       
    65   };
       
    66 
       
    67   // Specifies the order for the most-recently specified key.
       
    68   // Note: only applies to entries. Map keys are unordered!
       
    69   nest.sortKeys = function(order) {
       
    70     sortKeys[keys.length - 1] = order;
       
    71     return nest;
       
    72   };
       
    73 
       
    74   // Specifies the order for leaf values.
       
    75   // Applies to both maps and entries array.
       
    76   nest.sortValues = function(order) {
       
    77     sortValues = order;
       
    78     return nest;
       
    79   };
       
    80 
       
    81   nest.rollup = function(f) {
       
    82     rollup = f;
       
    83     return nest;
       
    84   };
       
    85 
       
    86   return nest;
       
    87 };