Complex d3.nest() manipulation

前端 未结 4 1722
清歌不尽
清歌不尽 2020-12-30 08:21

I have an array of arrays that looks like this:

var arrays = [[1,2,3,4,5],
              [1,2,6,4,5],
              [1,3,6,4,5],
              [1,2,3,6,5],
          


        
4条回答
  •  醉梦人生
    2020-12-30 09:03

    Edit - fixed

    Here is my solution Pro:It is all in one go (doesn't need objects converting to arrays like above) Pro:It keeps the size/value count Pro:the output is EXACTLY the same as a d3 flare with children Con:it is uglier, and likely less efficient Big Thanks to previous comments for helping me work it out.

        var data = [[1,2,3,4,5],
            [1,2,6,4,5],
            [1,3,6,4,5],
            [1,2,3,6,5],
            [1,7,5],
            [1,7,3,5]]
    
        var root = {"name":"flare", "children":[]} // the output
        var node // pointer thingy
        var row
    
    
    
    // loop through array
    for(var i=0;i

    Working examples https://jsfiddle.net/7qaz062u/

    Output

    { "name": "flare", "children": [ { "name": 1, "children": [ { "name": 2, "children": [ { "name": 3, "children": [ { "name": 4, "children": [ { "name": 5, "size": 1 } ] } ] }, { "name": 6, "children": [ { "name": 4, "children": [ { "name": 5, "size": 1 } ] } ] } ] }, { "name": 3, "children": [ { "name": 6, "children": [ { "name": 4, "children": [ { "name": 5, "size": 1 } ] } ] }, { "name": 3, "children": [ { "name": 6, "children": [ { "name": 5, "size": 1 } ] } ] } ] }, { "name": 7, "children": [ { "name": 5, "size": 1 }, { "name": 3, "children": [ { "name": 5, "size": 1 } ] } ] } ] } ] }
    

提交回复
热议问题