All the examples I've seen for representing hierarchical data in d3 involve nesting elements: For example, divs within divs, or table cells within table rows.
What's the "right" way to show hierarchical data where the resulting DOM elements are siblings instead? For example, different levels of headings (h1, h2, etc).
My first attempt (http://jsfiddle.net/herbcaudill/dTd99/) takes this data:
var data = [{
"key" : "1",
"values" : [{
"key" : "101",
"title" : "Sub Section 1.A"
}, {
"key" : "102",
"title" : "Sub Section 1.B"
}, {
"key" : "103",
"title" : "Sub Section 1.C"
}
],
"title" : "Section 1"
}, {
"key" : "2",
"values" : [{
"key" : "201",
"title" : "Sub Section 2.A"
}, {
"key" : "202",
"title" : "Sub Section 2.B"
}, {
"key" : "203",
"title" : "Sub Section 2.C"
}
],
"title" : "Section 2"
}]
... and attempts to turn it into headings and subheadings using standard examples that assume we're nesting elements, like this:
d3.select("body").selectAll("h1")
.data(data)
.enter()
.append("h1")
.text(function(d) {return d.title })
.selectAll("h2")
.data(function(d) { return d.values })
.enter()
.append("h2")
.text(function(d) {return d.title})
Of course I end up with nested elements, which is not what I want:
<h1>Section 1
<h2>Sub Section 1.A</h2>
<h2>Sub Section 1.B</h2>
<h2>Sub Section 1.C</h2>
</h1>
<h1>Section 2
<h2>Sub Section 2.A</h2>
<h2>Sub Section 2.B</h2>
<h2>Sub Section 2.C</h2>
</h1>
How can I get this instead?
<h1>Section 1</h1>
<h2>Sub Section 1.A</h2>
<h2>Sub Section 1.B</h2>
<h2>Sub Section 1.C</h2>
<h1>Section 2</h1>
<h2>Sub Section 2.A</h2>
<h2>Sub Section 2.B</h2>
<h2>Sub Section 2.C</h2>