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

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

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:

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

to:

  1. {
  2. "PARENT_ID": 0,
  3. "CHILD_ID": 1,
  4. "NAME": "Quality",
  5. "LEVEL_A": 0,
  6. "children": [
  7. {
  8. "PARENT_ID": 1,
  9. "CHILD_ID": 2,
  10. "NAME": "Machine",
  11. "LEVEL_A": 1,
  12. "children": [
  13. {
  14. "PARENT_ID": 2,
  15. "CHILD_ID": 3,
  16. "NAME": "Banana",
  17. "LEVEL_A": 2,
  18. "children": []
  19. },
  20. {
  21. "PARENT_ID": 2,
  22. "CHILD_ID": 8,
  23. "NAME": "Mill",
  24. "LEVEL_A": 2,
  25. "children": []
  26. },
  27. {
  28. "PARENT_ID": 2,
  29. "CHILD_ID": 9,
  30. "NAME": "Mixer",
  31. "LEVEL_A": 2,
  32. "children": []
  33. }
  34. ]
  35. },
  36. {
  37. "PARENT_ID": 1,
  38. "CHILD_ID": 3,
  39. "NAME": "Method",
  40. "LEVEL_A": 1,
  41. "children": []
  42. },
  43. {
  44. "PARENT_ID": 1,
  45. "CHILD_ID": 4,
  46. "NAME": "Material",
  47. "LEVEL_A": 1,
  48. "children": [
  49. {
  50. "PARENT_ID": 4,
  51. "CHILD_ID": 11,
  52. "NAME": "Masonite",
  53. "LEVEL_A": 2,
  54. "children": []
  55. },
  56. {
  57. "PARENT_ID": 4,
  58. "CHILD_ID": 12,
  59. "NAME": "Meat",
  60. "LEVEL_A": 2,
  61. "children": [
  62. {
  63. "PARENT_ID": 12,
  64. "CHILD_ID": 19,
  65. "NAME": "Mutton",
  66. "LEVEL_A": 3,
  67. "children": []
  68. }
  69. ]
  70. },
  71. {
  72. "PARENT_ID": 4,
  73. "CHILD_ID": 13,
  74. "NAME": "Marscapone",
  75. "LEVEL_A": 2,
  76. "children": [
  77. {
  78. "PARENT_ID": 13,
  79. "CHILD_ID": 14,
  80. "NAME": "Malty",
  81. "LEVEL_A": 3,
  82. "children": [
  83. {
  84. "PARENT_ID": 14,
  85. "CHILD_ID": 17,
  86. "NAME": "Minty",
  87. "LEVEL_A": 4,
  88. "children": [
  89. {
  90. "PARENT_ID": 17,
  91. "CHILD_ID": 16,
  92. "NAME": "spearMint",
  93. "LEVEL_A": 5,
  94. "children": []
  95. },
  96. {
  97. "PARENT_ID": 17,
  98. "CHILD_ID": 18,
  99. "NAME": "pepperMint",
  100. "LEVEL_A": 5,
  101. "children": []
  102. }
  103. ]
  104. }
  105. ]
  106. },
  107. {
  108. "PARENT_ID": 13,
  109. "CHILD_ID": 15,
  110. "NAME": "Minty",
  111. "LEVEL_A": 3,
  112. "children": []
  113. }
  114. ]
  115. }
  116. ]
  117. },
  118. {
  119. "PARENT_ID": 1,
  120. "CHILD_ID": 5,
  121. "NAME": "Man Power",
  122. "LEVEL_A": 1,
  123. "children": [
  124. {
  125. "PARENT_ID": 5,
  126. "CHILD_ID": 20,
  127. "NAME": "Manager",
  128. "LEVEL_A": 2,
  129. "children": []
  130. },
  131. {
  132. "PARENT_ID": 5,
  133. "CHILD_ID": 21,
  134. "NAME": "Magician",
  135. "LEVEL_A": 2,
  136. "children": []
  137. },
  138. {
  139. "PARENT_ID": 5,
  140. "CHILD_ID": 22,
  141. "NAME": "Magister",
  142. "LEVEL_A": 2,
  143. "children": [
  144. {
  145. "PARENT_ID": 22,
  146. "CHILD_ID": 23,
  147. "NAME": "Malpractice",
  148. "LEVEL_A": 3,
  149. "children": []
  150. }
  151. ]
  152. },
  153. {
  154. "PARENT_ID": 5,
  155. "CHILD_ID": 24,
  156. "NAME": "Massage Artist",
  157. "LEVEL_A": 2,
  158. "children": []
  159. }
  160. ]
  161. },
  162. {
  163. "PARENT_ID": 1,
  164. "CHILD_ID": 6,
  165. "NAME": "Measurement",
  166. "LEVEL_A": 1,
  167. "children": [
  168. {
  169. "PARENT_ID": 6,
  170. "CHILD_ID": 25,
  171. "NAME": "Malleability",
  172. "LEVEL_A": 2,
  173. "children": []
  174. }
  175. ]
  176. },
  177. {
  178. "PARENT_ID": 1,
  179. "CHILD_ID": 7,
  180. "NAME": "Milieu",
  181. "LEVEL_A": 1,
  182. "children": [
  183. {
  184. "PARENT_ID": 7,
  185. "CHILD_ID": 26,
  186. "NAME": "Marine",
  187. "LEVEL_A": 2,
  188. "children": []
  189. }
  190. ]
  191. },
  192. {
  193. "PARENT_ID": 1,
  194. "CHILD_ID": 10,
  195. "NAME": "Metal Lathe",
  196. "LEVEL_A": 1,
  197. "children": []
  198. }
  199. ]
  200. }

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将它们添加到映射中适当的父项中。

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

  1. const map = {};
  2. for(const item of data){
  3. map[item.CHILD_ID] = {...item, children: []};
  4. }
  5. let result;
  6. for(const item of Object.values(map)){
  7. map[item.PARENT_ID]?.children.push(item) ?? (result = item);
  8. }
  9. $result.textContent = JSON.stringify(result, 0, 4);
  1. <script>
  2. 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}];
  3. </script>
  4. <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 -->

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

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

  1. &lt;script&gt;
  2. 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}];
  3. &lt;/script&gt;
  4. &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树状结构所需的数据结构。因此,您可以像这样使用它:

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

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

  1. // 此处为示例代码
  2. data = [
  3. { PARENT_ID: null, CHILD_ID: 1, NAME: "Quality", LEVEL_A: 0 },
  4. // ... (此处省略了一些数据)
  5. ];
  6. let stratified = d3
  7. .stratify()
  8. .id((d) => d.CHILD_ID)
  9. .parentId((d) => d.PARENT_ID)(data);
  10. d3.select('#viz')
  11. .append(() => graph(d3.hierarchy(stratified), {
  12. label: (d) => d.data.data.NAME
  13. }));
  14. // ... (以下是示例代码的其余部分)

在示例中,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:

  1. let stratified = d3
  2. .stratify()
  3. .id((d) =&gt; d.CHILD_ID)
  4. .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 -->

  1. data = [
  2. { PARENT_ID: null, CHILD_ID: 1, NAME: &quot;Quality&quot;, LEVEL_A: 0 },
  3. { PARENT_ID: 1, CHILD_ID: 2, NAME: &quot;Machine&quot;, LEVEL_A: 1 },
  4. { PARENT_ID: 1, CHILD_ID: 3, NAME: &quot;Method&quot;, LEVEL_A: 1 },
  5. { PARENT_ID: 1, CHILD_ID: 4, NAME: &quot;Material&quot;, LEVEL_A: 1 },
  6. { PARENT_ID: 1, CHILD_ID: 5, NAME: &quot;Man Power&quot;, LEVEL_A: 1 },
  7. { PARENT_ID: 1, CHILD_ID: 6, NAME: &quot;Measurement&quot;, LEVEL_A: 1 },
  8. { PARENT_ID: 1, CHILD_ID: 7, NAME: &quot;Milieu&quot;, LEVEL_A: 1 },
  9. { PARENT_ID: 1, CHILD_ID: 10, NAME: &quot;Metal Lathe&quot;, LEVEL_A: 1 },
  10. { PARENT_ID: 2, CHILD_ID: 3, NAME: &quot;dasd&quot;, LEVEL_A: 2 },
  11. { PARENT_ID: 2, CHILD_ID: 8, NAME: &quot;Mill&quot;, LEVEL_A: 2 },
  12. { PARENT_ID: 2, CHILD_ID: 9, NAME: &quot;Mixer&quot;, LEVEL_A: 2 },
  13. { PARENT_ID: 4, CHILD_ID: 11, NAME: &quot;Masonite&quot;, LEVEL_A: 2 },
  14. { PARENT_ID: 4, CHILD_ID: 12, NAME: &quot;Meat&quot;, LEVEL_A: 2 },
  15. { PARENT_ID: 4, CHILD_ID: 13, NAME: &quot;Marscapone&quot;, LEVEL_A: 2 },
  16. { PARENT_ID: 5, CHILD_ID: 20, NAME: &quot;Manager&quot;, LEVEL_A: 2 },
  17. { PARENT_ID: 5, CHILD_ID: 21, NAME: &quot;Magician&quot;, LEVEL_A: 2 },
  18. { PARENT_ID: 5, CHILD_ID: 22, NAME: &quot;Magister&quot;, LEVEL_A: 2 },
  19. { PARENT_ID: 5, CHILD_ID: 24, NAME: &quot;Massage Artist&quot;, LEVEL_A: 2 },
  20. { PARENT_ID: 6, CHILD_ID: 25, NAME: &quot;Malleability&quot;, LEVEL_A: 2 },
  21. { PARENT_ID: 7, CHILD_ID: 26, NAME: &quot;Marine&quot;, LEVEL_A: 2 },
  22. { PARENT_ID: 12, CHILD_ID: 19, NAME: &quot;Mutton&quot;, LEVEL_A: 3 },
  23. { PARENT_ID: 13, CHILD_ID: 14, NAME: &quot;Malty&quot;, LEVEL_A: 3 },
  24. { PARENT_ID: 13, CHILD_ID: 15, NAME: &quot;Minty&quot;, LEVEL_A: 3 },
  25. { PARENT_ID: 14, CHILD_ID: 17, NAME: &quot;Minty&quot;, LEVEL_A: 4 },
  26. { PARENT_ID: 17, CHILD_ID: 16, NAME: &quot;spearMint&quot;, LEVEL_A: 5 },
  27. { PARENT_ID: 17, CHILD_ID: 18, NAME: &quot;pepperMint&quot;, LEVEL_A: 5 },
  28. { PARENT_ID: 22, CHILD_ID: 23, NAME: &quot;Malpractice&quot;, LEVEL_A: 3 }
  29. ];
  30. let stratified = d3
  31. .stratify()
  32. .id((d) =&gt; d.CHILD_ID)
  33. .parentId((d) =&gt; d.PARENT_ID)(data);
  34. d3.select(&#39;#viz&#39;)
  35. .append(() =&gt; graph(d3.hierarchy(stratified), {
  36. label: (d) =&gt; d.data.data.NAME}))
  37. // Modified only slightly from
  38. // https://observablehq.com/@d3/d3-hierarchy
  39. function graph(
  40. root,
  41. { label = (d) =&gt; d.data.id, highlight = () =&gt; false, marginLeft = 40 } = {}
  42. ) {
  43. let dx = 12;
  44. let dy = 120;
  45. let width = 600;
  46. let tree = d3
  47. .tree()
  48. .separation(() =&gt; 3)
  49. .nodeSize([dx, dy]);
  50. let treeLink = d3
  51. .linkHorizontal()
  52. .x((d) =&gt; d.y)
  53. .y((d) =&gt; d.x);
  54. root = tree(root);
  55. let x0 = Infinity;
  56. let x1 = -x0;
  57. root.each((d) =&gt; {
  58. if (d.x &gt; x1) x1 = d.x;
  59. if (d.x &lt; x0) x0 = d.x;
  60. });
  61. const svg = d3
  62. .create(&quot;svg&quot;)
  63. .attr(&quot;viewBox&quot;, [0, 0, width, x1 - x0 + dx * 2])
  64. .style(&quot;overflow&quot;, &quot;visible&quot;);
  65. const g = svg
  66. .append(&quot;g&quot;)
  67. .attr(&quot;font-family&quot;, &quot;sans-serif&quot;)
  68. .attr(&quot;font-size&quot;, 10)
  69. .attr(&quot;transform&quot;, `translate(${marginLeft},${dx - x0})`);
  70. const link = g
  71. .append(&quot;g&quot;)
  72. .attr(&quot;fill&quot;, &quot;none&quot;)
  73. .attr(&quot;stroke&quot;, &quot;#555&quot;)
  74. .attr(&quot;stroke-opacity&quot;, 0.4)
  75. .attr(&quot;stroke-width&quot;, 1.5)
  76. .selectAll(&quot;path&quot;)
  77. .data(root.links())
  78. .join(&quot;path&quot;)
  79. .attr(&quot;stroke&quot;, (d) =&gt;
  80. highlight(d.source) &amp;&amp; highlight(d.target) ? &quot;red&quot; : null
  81. )
  82. .attr(&quot;stroke-opacity&quot;, (d) =&gt;
  83. highlight(d.source) &amp;&amp; highlight(d.target) ? 1 : null
  84. )
  85. .attr(&quot;d&quot;, treeLink);
  86. const node = g
  87. .append(&quot;g&quot;)
  88. .attr(&quot;stroke-linejoin&quot;, &quot;round&quot;)
  89. .attr(&quot;stroke-width&quot;, 3)
  90. .selectAll(&quot;g&quot;)
  91. .data(root.descendants())
  92. .join(&quot;g&quot;)
  93. .attr(&quot;transform&quot;, (d) =&gt; `translate(${d.y},${d.x})`);
  94. node
  95. .append(&quot;circle&quot;)
  96. .attr(&quot;fill&quot;, (d) =&gt; (highlight(d) ? &quot;red&quot; : d.children ? &quot;#555&quot; : &quot;#999&quot;))
  97. .attr(&quot;r&quot;, 2.5);
  98. node
  99. .append(&quot;text&quot;)
  100. .attr(&quot;fill&quot;, (d) =&gt; (highlight(d) ? &quot;red&quot; : null))
  101. .attr(&quot;stroke&quot;, &quot;white&quot;)
  102. .attr(&quot;paint-order&quot;, &quot;stroke&quot;)
  103. .attr(&quot;dy&quot;, &quot;0.31em&quot;)
  104. .attr(&quot;x&quot;, (d) =&gt; (d.children ? -6 : 6))
  105. .attr(&quot;text-anchor&quot;, (d) =&gt; (d.children ? &quot;end&quot; : &quot;start&quot;))
  106. .text(label);
  107. return svg.node();
  108. }

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

  1. &lt;script src=&quot;https://d3js.org/d3.v7.min.js&quot;&gt;&lt;/script&gt;
  2. &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:

确定