简体   繁体   English

d3.js树状图,叶子呈现为键值对

[英]d3.js dendrogram with leaves rendered as key-value pairs

Working with d3.js and I have hierarchical data and working with this example d3 dendrogram which nearly meets my needs, but instead of a text string as the leaf, I need to display a table. 使用d3.js并具有分层数据,并使用此示例d3树状图几乎可以满足我的需求,但是我需要显示一个表格,而不是像文本字符串那样显示叶子。 The data follows this pattern: 数据遵循以下模式:

{"name": "root",
 "children": [
  {"name": "dtable",
   "children": [
    {"label": "la01", "tag":"section", "title":"Section One"}
    {"label": "la02", "tag":"section", "title":"Section Two"}
    {"label": "la0201", "tag":"subsection", "title":"Subsection Two:One"}
    ]
  }
}

I'd like the leaf to be rendered something like this: 我希望将叶子渲染成这样:

----------------------------------------------
| label    | tag        | title              |
----------------------------------------------
| la01     | section    | Section One        |
| la02     | section    | Section Two        |
| la0201   | subsection | Subsection Two:One |
----------------------------------------------

The d3 example has this snippet which displays the leaf as a text-string. d3示例包含此代码段,该代码段将叶子显示为文本字符串。 I'm at a loss on how to reengineer or replace the code to show a table instead: 我不知道如何重新设计或替换代码以显示表格:

nodeEnter.append("svg:text")
  .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
  .attr("dy", ".35em")
  .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
  .text(function(d) { return d.name; })
  .style("fill-opacity", 1e-6);

Rendering something as large as a table might be difficult to achieve space-wise. 渲染与表格一样大的内容可能很难在空间上实现。 The easiest way would be to append a foreignObject element instead of the text element and then create a HTML table that contains the data you need to show. 最简单的方法是附加一个foreignObject元素而不是text元素,然后创建一个包含需要显示的数据的HTML table

See here for an example of how to use foreignObject . 有关如何使用foreignObject的示例,请参见此处 You may also find this tutorial helpful for creating the actual table. 您可能还会发现本教程对创建实际表很有帮助。

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

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