简体   繁体   English

如何将JSON对象数组准备为D3分层树结构

[英]How to prepare array of json objects to d3 hierarchical tree structure

I'm trying to create a collapsible tree diagram in D3v4 like this . 我正在尝试像这样在D3v4中创建可折叠的树形图。 I have my data as an array of JSON objects and need to prepare the data into a hierarchical tree structure. 我将数据作为JSON对象数组,需要将数据准备为分层树结构。

My data looks likes this: 我的数据如下所示:

[
  {
    "ingoingBatch": "BC1",
    "process": "API",
    "outgoingBatch": "CD1",
    "counterIndex": "5"
  },
  {
    "ingoingBatch": "BC2",
    "process": "API",
    "outgoingBatch": "CD1",
    "counterIndex": "6"
  },
  {
    "ingoingBatch": "AB1",
    "process": "PUR",
    "outgoingBatch": "BC1",
    "counterIndex": "1"
  },
  {
    "ingoingBatch": "AB2",
    "process": "PUR",
    "outgoingBatch": "BC1",
    "counterIndex": "2"
  },
  {
    "ingoingBatch": "AB3",
    "process": "PUR",
    "outgoingBatch": "BC2",
    "counterIndex": "3"
  },
  {
    "ingoingBatch": "AB4",
    "process": "PUR",
    "outgoingBatch": "BC2",
    "counterIndex": "4"
  }
]

The structure I wanted is something like this structure 我想要的结构就是这样的结构

I have tried making a JSfiddle where you can see the original data on line 129-142. 我尝试制作一个JSfiddle ,您可以在其中看到第129-142行的原始数据。 I have inserted my own data on line 11-115 and used the function d3 function nest as found here on line 117-126: 我已经插入线11-115我自己的数据和使用中发现的功能,D3功能窝在这里就行117-126:

var entries = d3.nest()
    .key(function(d) { return d.ingoingBatch; })
    .key(function(d) { return d.outgoingBatch; })
    .entries(json);

console.log(entries);

var treeData = d3.hierarchy(entries);

console.log(treeData);

I assume the problem is within the data preparation. 我认为问题出在数据准备之内。 The error message I get when I run the JSfiddle with my data is: TypeError: root.children is undefined 使用数据运行JSfiddle时收到的错误消息是: TypeError: root.children is undefined

The root node should be C1 and the structure should look like this: 根节点应为C1,结构应如下所示:

           C1 
    B2            B1
 A1    A2      A3    A4

The link between the nodes is the ingoingBatch and outgoingBatch. 节点之间的链接是ingoingBatch和outgoingBatch。 Can anybody please help prepare my data to get my data preparation just right? 有人可以帮助准备我的数据以使我的数据准备正确吗?

-----------UPDATE----------- -----------更新-----------

If I use the answer from here this question I get a bit closer on having the right data structure. 如果我使用的答案从这里这个问题我得到有合适的数据结构更接近一点。 When I instead of this: 当我代替这个:

var entries = d3.nest()
    .key(function(d) { return d.ingoingBatch; })
    .key(function(d) { return d.outgoingBatch; })
    .entries(json);

console.log(entries);

var treeData = d3.hierarchy(entries);

console.log(treeData);

Use the the answer from the other question and have this function instead: 使用其他问题的答案,并改用以下功能:

var newData = { name :"root", children : [] },
    levels = ["outgoingBatch"];
    // levels = ["ingoingBatch","outgoingBatch"];

// For each data row, loop through the expected levels traversing the output tree
json.forEach(function(d){
    // Keep this as a reference to the current level
    var depthCursor = newData.children;
    // Go down one level at a time
    levels.forEach(function( property, depth ){

        // Look to see if a branch has already been created
        var index;
        depthCursor.forEach(function(child,i){
            if ( d[property] == child.name ) index = i;
        });
        // Add a branch if it isn't there
        if ( isNaN(index) ) {
            depthCursor.push({ name : d[property], children : []});
            index = depthCursor.length - 1;
        }
        // Now reference the new child array as we go deeper into the tree
        depthCursor = depthCursor[index].children;
        // This is a leaf, so add the last element to the specified branch
        if ( depth === levels.length - 1 ) depthCursor.push({ outgoingBatch : d.outgoingBatch });
    });
});

var treeData = newData
console.log(treeData);

I get a three diagram out. 我得到三个图表。 But it is not structured correctly, since CD1 is not set as the root node but instead one of the children. 但是它的结构不正确,因为CD1没有设置为根节点,而是子节点之一。

I found out how to return the root node from the place I get the JSON objects. 我发现了如何从获取JSON对象的地方返回根节点。 In this way I could use that and make the JSON objects the child of this with the code from the update. 通过这种方式,我可以使用它,并使用更新中的代码使JSON对象成为其子对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM