2014-06-26 2 views
0

У меня есть структура данных, которая выглядит следующим образом, и я хочу вложить ее в создание карты D3. Я могу вложить его, используя либо Underscore.nest, либо D3.nest(). Однако, поскольку мои данные имеют произвольную глубину, я получаю кучу неопределенных узлов. Как я могу вложить его на произвольную глубину или rollup неопределенные узлы?D3 вложенные данные с произвольной глубиной

// Example Data 
data = [ 
    {'name' : 'Post 1', '0' : 'tag_a'}, 
    {'name' : 'Post 2', '0' : 'tag_b'}, 
    {'name' : 'Post 3', '0' : 'tag_b', '1' : 'tag_c'}, 
    {'name' : 'Post 4', '0' : 'tag_d', '1' : 'tag_e', '2' : 'tag_f', '3' : 'tag_g'} 
]; 


// Nesting with the Underscore nest plugin 
var underscoreNested = _.nest(data, ['0', '1', '2', '3']); 

{ 
    "children": [ 
    { 
     "name": "tag_a", 
     "children": [ 
     { 
      "name": "undefined", 
      "children": [ 
      { 
       "name": "undefined", 
       "children": [ 
       { 
        "name": "undefined", 
        "children": [ 
        { 
         "0": "tag_a", 
         "name": "Post 1" 
        } 
        ], 
        "index": 0 
       } 
       ], 
       "index": 0 
      } 
      ], 
      "index": 0 
     } 
     ], 
     "index": 0 
    }... 

// Nesting with D3 
var d3Nested = d3.nest() 
    .key(function(d) { return d['0']; }) 
    .key(function(d) { return d['1']; }) 
    .key(function(d) { return d['2']; }) 
    .key(function(d) { return d['3']; }) 
    .entries(data); 

[ 
    { 
    "key": "tag_a", 
    "values": [ 
     { 
     "key": "undefined", 
     "values": [ 
      { 
      "key": "undefined", 
      "values": [ 
       { 
       "key": "undefined", 
       "values": [ 
        { 
        "0": "tag_a", 
        "name": "Post 1" 
        } 
       ] 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    },... 

ответ

0

Я нашел хороший solution here используя burrow.js. Он очень хорошо работает с примером масштабируемой тройки D3.

// nest rows with keys array, requires Underscore.js 
burrow = function (data) { 
    // create simple nested object 
    var object = {}; 
    _(data).each(function(d) { 
    var _object = object; 
    // branch based on an array of items, 
    // with arbitray depth/length, 
    // in this case the tags array 
    _(d.tags).each(function(element,index) { 
     _object[element] = _object[element] || {}; 
     _object = _object[element]; 
    }); 
    }); 

    // recursively create children array 
    function descend(object) { 
    if (!_.isEmpty(object)) {  
     var array = []; 
     _(object).each(function(value, key) { 
      var children = descend(value); 
      if (!!children) { 
      var branch = { 
       name: key, 
       children: children 
      }; 
      } else { 
      var branch = { 
       name: key, 
       value: 1 
      }; 
      } 
      array.push(branch) 
     }); // _.each 
     return array; 
    } // if 
    else return false; 
    }; // descend 

    // nested objectect 
    return { 
    name: "Nested Data", 
    children: descend(object) 
    }; 
}; 
Смежные вопросы