如何将层次数据的数组转换为适合D3-Tree-Region的正确JSON格式

huangapple go评论77阅读模式
英文:

How to convert Array of hierarchy data into right JSON-Format for D3-Tree-Region

问题

我有问题将层次数据的数组转换为正确的对象格式。

我试图将这个转换为:

但我不知道如何让它工作。我在这个帖子中找到了一段代码,但我不知道如何应用它到我的用例。

有人可以帮助我吗?

我会非常感激任何建议!

谨以问候
Jegor

英文:

i having problems converting an array of hierarchy data into the right Object format.

I trying to convert this:

[
    {"PARENT_ID": 0,"CHILD_ID": 1,"NAME": "Quality","LEVEL_A": 0},
    {"PARENT_ID": 1,"CHILD_ID": 2,"NAME": "Machine","LEVEL_A": 1},
    {"PARENT_ID": 1,"CHILD_ID": 3,"NAME": "Method","LEVEL_A": 1},
    {"PARENT_ID": 1,"CHILD_ID": 4,"NAME": "Material","LEVEL_A": 1},
    {"PARENT_ID": 1,"CHILD_ID": 5,"NAME": "Man Power","LEVEL_A": 1},
    {"PARENT_ID": 1,"CHILD_ID": 6,"NAME": "Measurement","LEVEL_A": 1},
    {"PARENT_ID": 1,"CHILD_ID": 7,"NAME": "Milieu","LEVEL_A": 1},
    {"PARENT_ID": 1,"CHILD_ID": 10,"NAME": "Metal Lathe","LEVEL_A": 1},
    {"PARENT_ID": 2,"CHILD_ID": 3,"NAME": "dasd","LEVEL_A": 2},
    {"PARENT_ID": 2,"CHILD_ID": 8,"NAME": "Mill","LEVEL_A": 2},
    {"PARENT_ID": 2,"CHILD_ID": 9,"NAME": "Mixer","LEVEL_A": 2},
    {"PARENT_ID": 4,"CHILD_ID": 11,"NAME": "Masonite","LEVEL_A": 2},
    {"PARENT_ID": 4,"CHILD_ID": 12,"NAME": "Meat","LEVEL_A": 2},
    {"PARENT_ID": 4,"CHILD_ID": 13,"NAME": "Marscapone","LEVEL_A": 2},
    {"PARENT_ID": 5,"CHILD_ID": 20,"NAME": "Manager","LEVEL_A": 2},
    {"PARENT_ID": 5,"CHILD_ID": 21,"NAME": "Magician","LEVEL_A": 2},
    {"PARENT_ID": 5,"CHILD_ID": 22,"NAME": "Magister","LEVEL_A": 2},
    {"PARENT_ID": 5,"CHILD_ID": 24,"NAME": "Massage Artist","LEVEL_A": 2},
    {"PARENT_ID": 6,"CHILD_ID": 25,"NAME": "Malleability","LEVEL_A": 2},
    {"PARENT_ID": 7,"CHILD_ID": 26,"NAME": "Marine","LEVEL_A": 2},
    {"PARENT_ID": 12,"CHILD_ID": 19,"NAME": "Mutton","LEVEL_A": 3},
    {"PARENT_ID": 13,"CHILD_ID": 14,"NAME": "Malty","LEVEL_A": 3},
    {"PARENT_ID": 13,"CHILD_ID": 15,"NAME": "Minty","LEVEL_A": 3},
    {"PARENT_ID": 14,"CHILD_ID": 17,"NAME": "Minty","LEVEL_A": 4},
    {"PARENT_ID": 17,"CHILD_ID": 16,"NAME": "spearMint","LEVEL_A": 5},
    {"PARENT_ID": 17,"CHILD_ID": 18,"NAME": "pepperMint","LEVEL_A": 5},
    {"PARENT_ID": 22,"CHILD_ID": 23,"NAME": "Malpractice","LEVEL_A": 3}
]

to:

{
"PARENT_ID": 0,
"CHILD_ID": 1,
"NAME": "Quality",
"LEVEL_A": 0,
"children": [
{
"PARENT_ID": 1,
"CHILD_ID": 2,
"NAME": "Machine",
"LEVEL_A": 1,
"children": [
{
"PARENT_ID": 2,
"CHILD_ID": 3,
"NAME": "Banana",
"LEVEL_A": 2,
"children": []
},
{
"PARENT_ID": 2,
"CHILD_ID": 8,
"NAME": "Mill",
"LEVEL_A": 2,
"children": []
},
{
"PARENT_ID": 2,
"CHILD_ID": 9,
"NAME": "Mixer",
"LEVEL_A": 2,
"children": []
}
]
},
{
"PARENT_ID": 1,
"CHILD_ID": 3,
"NAME": "Method",
"LEVEL_A": 1,
"children": []
},
{
"PARENT_ID": 1,
"CHILD_ID": 4,
"NAME": "Material",
"LEVEL_A": 1,
"children": [
{
"PARENT_ID": 4,
"CHILD_ID": 11,
"NAME": "Masonite",
"LEVEL_A": 2,
"children": []
},
{
"PARENT_ID": 4,
"CHILD_ID": 12,
"NAME": "Meat",
"LEVEL_A": 2,
"children": [
{
"PARENT_ID": 12,
"CHILD_ID": 19,
"NAME": "Mutton",
"LEVEL_A": 3,
"children": []
}
]
},
{
"PARENT_ID": 4,
"CHILD_ID": 13,
"NAME": "Marscapone",
"LEVEL_A": 2,
"children": [
{
"PARENT_ID": 13,
"CHILD_ID": 14,
"NAME": "Malty",
"LEVEL_A": 3,
"children": [
{
"PARENT_ID": 14,
"CHILD_ID": 17,
"NAME": "Minty",
"LEVEL_A": 4,
"children": [
{
"PARENT_ID": 17,
"CHILD_ID": 16,
"NAME": "spearMint",
"LEVEL_A": 5,
"children": []
},
{
"PARENT_ID": 17,
"CHILD_ID": 18,
"NAME": "pepperMint",
"LEVEL_A": 5,
"children": []
}
]
}
]
},
{
"PARENT_ID": 13,
"CHILD_ID": 15,
"NAME": "Minty",
"LEVEL_A": 3,
"children": []
}
]
}
]
},
{
"PARENT_ID": 1,
"CHILD_ID": 5,
"NAME": "Man Power",
"LEVEL_A": 1,
"children": [
{
"PARENT_ID": 5,
"CHILD_ID": 20,
"NAME": "Manager",
"LEVEL_A": 2,
"children": []
},
{
"PARENT_ID": 5,
"CHILD_ID": 21,
"NAME": "Magician",
"LEVEL_A": 2,
"children": []
},
{
"PARENT_ID": 5,
"CHILD_ID": 22,
"NAME": "Magister",
"LEVEL_A": 2,
"children": [
{
"PARENT_ID": 22,
"CHILD_ID": 23,
"NAME": "Malpractice",
"LEVEL_A": 3,
"children": []
}
]
},
{
"PARENT_ID": 5,
"CHILD_ID": 24,
"NAME": "Massage Artist",
"LEVEL_A": 2,
"children": []
}
]
},
{
"PARENT_ID": 1,
"CHILD_ID": 6,
"NAME": "Measurement",
"LEVEL_A": 1,
"children": [
{
"PARENT_ID": 6,
"CHILD_ID": 25,
"NAME": "Malleability",
"LEVEL_A": 2,
"children": []
}
]
},
{
"PARENT_ID": 1,
"CHILD_ID": 7,
"NAME": "Milieu",
"LEVEL_A": 1,
"children": [
{
"PARENT_ID": 7,
"CHILD_ID": 26,
"NAME": "Marine",
"LEVEL_A": 2,
"children": []
}
]
},
{
"PARENT_ID": 1,
"CHILD_ID": 10,
"NAME": "Metal Lathe",
"LEVEL_A": 1,
"children": []
}
]
}

But I have no idea how to make it work. I have found a Code in this Post, but I don't know how to apply this to my usecase.

Can anyone help me with this?

I would be very grateful for any advice!

Kind Regards
Jegor

答案1

得分: 2

首先,通过CHILD_ID建立一个项目的映射,然后再次循环项目,并通过PARENT_ID将它们添加到映射中适当的父项中。

假设问题中只会有一个父项:

const map = {};

for(const item of data){
  map[item.CHILD_ID] = {...item, children: []};
}

let result;

for(const item of Object.values(map)){
  map[item.PARENT_ID]?.children.push(item) ?? (result = item);
}

$result.textContent = JSON.stringify(result, 0, 4);
<script>
const data = [{PARENT_ID:0,CHILD_ID:1,NAME:"Quality",LEVEL_A:0},{PARENT_ID:1,CHILD_ID:2,NAME:"Machine",LEVEL_A:1},{PARENT_ID:1,CHILD_ID:3,NAME:"Method",LEVEL_A:1},{PARENT_ID:1,CHILD_ID:4,NAME:"Material",LEVEL_A:1},{PARENT_ID:1,CHILD_ID:5,NAME:"Man Power",LEVEL_A:1},{PARENT_ID:1,CHILD_ID:6,NAME:"Measurement",LEVEL_A:1},{PARENT_ID:1,CHILD_ID:7,NAME:"Milieu",LEVEL_A:1},{PARENT_ID:1,CHILD_ID:10,NAME:"Metal Lathe",LEVEL_A:1},{PARENT_ID:2,CHILD_ID:3,NAME:"dasd",LEVEL_A:2},{PARENT_ID:2,CHILD_ID:8,NAME:"Mill",LEVEL_A:2},{PARENT_ID:2,CHILD_ID:9,NAME:"Mixer",LEVEL_A:2},{PARENT_ID:4,CHILD_ID:11,NAME:"Masonite",LEVEL_A:2},{PARENT_ID:4,CHILD_ID:12,NAME:"Meat",LEVEL_A:2},{PARENT_ID:4,CHILD_ID:13,NAME:"Marscapone",LEVEL_A:2},{PARENT_ID:5,CHILD_ID:20,NAME:"Manager",LEVEL_A:2},{PARENT_ID:5,CHILD_ID:21,NAME:"Magician",LEVEL_A:2},{PARENT_ID:5,CHILD_ID:22,NAME:"Magister",LEVEL_A:2},{PARENT_ID:5,CHILD_ID:24,NAME:"Massage Artist",LEVEL_A:2},{PARENT_ID:6,CHILD_ID:25,NAME:"Malleability",LEVEL_A:2},{PARENT_ID:7,CHILD_ID:26,NAME:"Marine",LEVEL_A:2},{PARENT_ID:12,CHILD_ID:19,NAME:"Mutton",LEVEL_A:3},{PARENT_ID:13,CHILD_ID:14,NAME:"Malty",LEVEL_A:3},{PARENT_ID:13,CHILD_ID:15,NAME:"Minty",LEVEL_A:3},{PARENT_ID:14,CHILD_ID:17,NAME:"Minty",LEVEL_A:4},{PARENT_ID:17,CHILD_ID:16,NAME:"spearMint",LEVEL_A:5},{PARENT_ID:17,CHILD_ID:18,NAME:"pepperMint",LEVEL_A:5},{PARENT_ID:22,CHILD_ID:23,NAME:"Malpractice",LEVEL_A:3}];
</script>
<div style="white-space:pre;font-family:monospace" id="$result"></div>
英文:

First build a map of items by CHILD_ID, then loop the items again and add them to an appropriate parent in the map by PARENT_ID.

And the question assumes there could be only 1 parent:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const map = {};
for(const item of data){
map[item.CHILD_ID] = {...item, children: []};
}
let result;
for(const item of Object.values(map)){
map[item.PARENT_ID]?.children.push(item) ?? (result = item);
}
$result.textContent = JSON.stringify(result,0,4);

<!-- language: lang-html -->

&lt;script&gt;
const data=[{PARENT_ID:0,CHILD_ID:1,NAME:&quot;Quality&quot;,LEVEL_A:0},{PARENT_ID:1,CHILD_ID:2,NAME:&quot;Machine&quot;,LEVEL_A:1},{PARENT_ID:1,CHILD_ID:3,NAME:&quot;Method&quot;,LEVEL_A:1},{PARENT_ID:1,CHILD_ID:4,NAME:&quot;Material&quot;,LEVEL_A:1},{PARENT_ID:1,CHILD_ID:5,NAME:&quot;Man Power&quot;,LEVEL_A:1},{PARENT_ID:1,CHILD_ID:6,NAME:&quot;Measurement&quot;,LEVEL_A:1},{PARENT_ID:1,CHILD_ID:7,NAME:&quot;Milieu&quot;,LEVEL_A:1},{PARENT_ID:1,CHILD_ID:10,NAME:&quot;Metal Lathe&quot;,LEVEL_A:1},{PARENT_ID:2,CHILD_ID:3,NAME:&quot;dasd&quot;,LEVEL_A:2},{PARENT_ID:2,CHILD_ID:8,NAME:&quot;Mill&quot;,LEVEL_A:2},{PARENT_ID:2,CHILD_ID:9,NAME:&quot;Mixer&quot;,LEVEL_A:2},{PARENT_ID:4,CHILD_ID:11,NAME:&quot;Masonite&quot;,LEVEL_A:2},{PARENT_ID:4,CHILD_ID:12,NAME:&quot;Meat&quot;,LEVEL_A:2},{PARENT_ID:4,CHILD_ID:13,NAME:&quot;Marscapone&quot;,LEVEL_A:2},{PARENT_ID:5,CHILD_ID:20,NAME:&quot;Manager&quot;,LEVEL_A:2},{PARENT_ID:5,CHILD_ID:21,NAME:&quot;Magician&quot;,LEVEL_A:2},{PARENT_ID:5,CHILD_ID:22,NAME:&quot;Magister&quot;,LEVEL_A:2},{PARENT_ID:5,CHILD_ID:24,NAME:&quot;Massage Artist&quot;,LEVEL_A:2},{PARENT_ID:6,CHILD_ID:25,NAME:&quot;Malleability&quot;,LEVEL_A:2},{PARENT_ID:7,CHILD_ID:26,NAME:&quot;Marine&quot;,LEVEL_A:2},{PARENT_ID:12,CHILD_ID:19,NAME:&quot;Mutton&quot;,LEVEL_A:3},{PARENT_ID:13,CHILD_ID:14,NAME:&quot;Malty&quot;,LEVEL_A:3},{PARENT_ID:13,CHILD_ID:15,NAME:&quot;Minty&quot;,LEVEL_A:3},{PARENT_ID:14,CHILD_ID:17,NAME:&quot;Minty&quot;,LEVEL_A:4},{PARENT_ID:17,CHILD_ID:16,NAME:&quot;spearMint&quot;,LEVEL_A:5},{PARENT_ID:17,CHILD_ID:18,NAME:&quot;pepperMint&quot;,LEVEL_A:5},{PARENT_ID:22,CHILD_ID:23,NAME:&quot;Malpractice&quot;,LEVEL_A:3}];
&lt;/script&gt;
&lt;div style=&quot;white-space:pre;font-family:monospace&quot; id=&quot;$result&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

D3拥有一个名为d3.stratify的函数,专门设计用于将表格数据转换成生成D3树状结构所需的数据结构。因此,您可以像这样使用它:

let stratified = d3
  .stratify()
  .id((d) => d.CHILD_ID)
  .parentId((d) => d.PARENT_ID)(data);

需要注意的是根节点应该没有父节点。考虑到这一点,以下是实际应用示例:

// 此处为示例代码
data = [
  { PARENT_ID: null, CHILD_ID: 1, NAME: "Quality", LEVEL_A: 0 },
  // ... (此处省略了一些数据)
];

let stratified = d3
  .stratify()
  .id((d) => d.CHILD_ID)
  .parentId((d) => d.PARENT_ID)(data);

d3.select('#viz')
  .append(() => graph(d3.hierarchy(stratified), {
    label: (d) => d.data.data.NAME
  }));

// ... (以下是示例代码的其余部分)

在示例中,stratified变量将包含转换后的树状结构数据,然后将其传递给graph函数以生成树形图。

需要注意的是,上述代码片段中包含一些HTML和JavaScript代码,以及D3库的引用,这些部分未翻译。如果需要完整的示例,请查看原始代码。

英文:

D3 has a function called d3.stratify that is designed precisely to manipulate tabular data into the tree structure that you need to generate a a tree with D3. Thus, you should be able to do something like so:

let stratified = d3
.stratify()
.id((d) =&gt; d.CHILD_ID)
.parentId((d) =&gt; d.PARENT_ID)(data);

Note that the root node should have no parent, though. Taking that into account, here it is in action:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

data = [
{ PARENT_ID: null, CHILD_ID: 1, NAME: &quot;Quality&quot;, LEVEL_A: 0 },
{ PARENT_ID: 1, CHILD_ID: 2, NAME: &quot;Machine&quot;, LEVEL_A: 1 },
{ PARENT_ID: 1, CHILD_ID: 3, NAME: &quot;Method&quot;, LEVEL_A: 1 },
{ PARENT_ID: 1, CHILD_ID: 4, NAME: &quot;Material&quot;, LEVEL_A: 1 },
{ PARENT_ID: 1, CHILD_ID: 5, NAME: &quot;Man Power&quot;, LEVEL_A: 1 },
{ PARENT_ID: 1, CHILD_ID: 6, NAME: &quot;Measurement&quot;, LEVEL_A: 1 },
{ PARENT_ID: 1, CHILD_ID: 7, NAME: &quot;Milieu&quot;, LEVEL_A: 1 },
{ PARENT_ID: 1, CHILD_ID: 10, NAME: &quot;Metal Lathe&quot;, LEVEL_A: 1 },
{ PARENT_ID: 2, CHILD_ID: 3, NAME: &quot;dasd&quot;, LEVEL_A: 2 },
{ PARENT_ID: 2, CHILD_ID: 8, NAME: &quot;Mill&quot;, LEVEL_A: 2 },
{ PARENT_ID: 2, CHILD_ID: 9, NAME: &quot;Mixer&quot;, LEVEL_A: 2 },
{ PARENT_ID: 4, CHILD_ID: 11, NAME: &quot;Masonite&quot;, LEVEL_A: 2 },
{ PARENT_ID: 4, CHILD_ID: 12, NAME: &quot;Meat&quot;, LEVEL_A: 2 },
{ PARENT_ID: 4, CHILD_ID: 13, NAME: &quot;Marscapone&quot;, LEVEL_A: 2 },
{ PARENT_ID: 5, CHILD_ID: 20, NAME: &quot;Manager&quot;, LEVEL_A: 2 },
{ PARENT_ID: 5, CHILD_ID: 21, NAME: &quot;Magician&quot;, LEVEL_A: 2 },
{ PARENT_ID: 5, CHILD_ID: 22, NAME: &quot;Magister&quot;, LEVEL_A: 2 },
{ PARENT_ID: 5, CHILD_ID: 24, NAME: &quot;Massage Artist&quot;, LEVEL_A: 2 },
{ PARENT_ID: 6, CHILD_ID: 25, NAME: &quot;Malleability&quot;, LEVEL_A: 2 },
{ PARENT_ID: 7, CHILD_ID: 26, NAME: &quot;Marine&quot;, LEVEL_A: 2 },
{ PARENT_ID: 12, CHILD_ID: 19, NAME: &quot;Mutton&quot;, LEVEL_A: 3 },
{ PARENT_ID: 13, CHILD_ID: 14, NAME: &quot;Malty&quot;, LEVEL_A: 3 },
{ PARENT_ID: 13, CHILD_ID: 15, NAME: &quot;Minty&quot;, LEVEL_A: 3 },
{ PARENT_ID: 14, CHILD_ID: 17, NAME: &quot;Minty&quot;, LEVEL_A: 4 },
{ PARENT_ID: 17, CHILD_ID: 16, NAME: &quot;spearMint&quot;, LEVEL_A: 5 },
{ PARENT_ID: 17, CHILD_ID: 18, NAME: &quot;pepperMint&quot;, LEVEL_A: 5 },
{ PARENT_ID: 22, CHILD_ID: 23, NAME: &quot;Malpractice&quot;, LEVEL_A: 3 }
];
let stratified = d3
.stratify()
.id((d) =&gt; d.CHILD_ID)
.parentId((d) =&gt; d.PARENT_ID)(data);
d3.select(&#39;#viz&#39;)
.append(() =&gt; graph(d3.hierarchy(stratified), {
label: (d) =&gt; d.data.data.NAME}))
// Modified only slightly from
// https://observablehq.com/@d3/d3-hierarchy
function graph(
root,
{ label = (d) =&gt; d.data.id, highlight = () =&gt; false, marginLeft = 40 } = {}
) {
let dx = 12;
let dy = 120;
let width = 600;
let tree = d3
.tree()
.separation(() =&gt; 3)
.nodeSize([dx, dy]);
let treeLink = d3
.linkHorizontal()
.x((d) =&gt; d.y)
.y((d) =&gt; d.x);
root = tree(root);
let x0 = Infinity;
let x1 = -x0;
root.each((d) =&gt; {
if (d.x &gt; x1) x1 = d.x;
if (d.x &lt; x0) x0 = d.x;
});
const svg = d3
.create(&quot;svg&quot;)
.attr(&quot;viewBox&quot;, [0, 0, width, x1 - x0 + dx * 2])
.style(&quot;overflow&quot;, &quot;visible&quot;);
const g = svg
.append(&quot;g&quot;)
.attr(&quot;font-family&quot;, &quot;sans-serif&quot;)
.attr(&quot;font-size&quot;, 10)
.attr(&quot;transform&quot;, `translate(${marginLeft},${dx - x0})`);
const link = g
.append(&quot;g&quot;)
.attr(&quot;fill&quot;, &quot;none&quot;)
.attr(&quot;stroke&quot;, &quot;#555&quot;)
.attr(&quot;stroke-opacity&quot;, 0.4)
.attr(&quot;stroke-width&quot;, 1.5)
.selectAll(&quot;path&quot;)
.data(root.links())
.join(&quot;path&quot;)
.attr(&quot;stroke&quot;, (d) =&gt;
highlight(d.source) &amp;&amp; highlight(d.target) ? &quot;red&quot; : null
)
.attr(&quot;stroke-opacity&quot;, (d) =&gt;
highlight(d.source) &amp;&amp; highlight(d.target) ? 1 : null
)
.attr(&quot;d&quot;, treeLink);
const node = g
.append(&quot;g&quot;)
.attr(&quot;stroke-linejoin&quot;, &quot;round&quot;)
.attr(&quot;stroke-width&quot;, 3)
.selectAll(&quot;g&quot;)
.data(root.descendants())
.join(&quot;g&quot;)
.attr(&quot;transform&quot;, (d) =&gt; `translate(${d.y},${d.x})`);
node
.append(&quot;circle&quot;)
.attr(&quot;fill&quot;, (d) =&gt; (highlight(d) ? &quot;red&quot; : d.children ? &quot;#555&quot; : &quot;#999&quot;))
.attr(&quot;r&quot;, 2.5);
node
.append(&quot;text&quot;)
.attr(&quot;fill&quot;, (d) =&gt; (highlight(d) ? &quot;red&quot; : null))
.attr(&quot;stroke&quot;, &quot;white&quot;)
.attr(&quot;paint-order&quot;, &quot;stroke&quot;)
.attr(&quot;dy&quot;, &quot;0.31em&quot;)
.attr(&quot;x&quot;, (d) =&gt; (d.children ? -6 : 6))
.attr(&quot;text-anchor&quot;, (d) =&gt; (d.children ? &quot;end&quot; : &quot;start&quot;))
.text(label);
return svg.node();
}

<!-- language: lang-html -->

&lt;script src=&quot;https://d3js.org/d3.v7.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;viz&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月14日 03:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定