Note that there are some explanatory texts on larger screens.

plurals
  1. POD3 - accessing nested data for the purpose of creating a navigation tree
    primarykey
    data
    text
    <p>So I have been using d3's zoom circle(<a href="http://mbostock.github.com/d3/talk/20111116/pack-hierarchy.html" rel="nofollow noreferrer">http://mbostock.github.com/d3/talk/20111116/pack-hierarchy.html</a>):</p> <p><img src="https://i.stack.imgur.com/OqgNL.gif" alt="enter image description here"></p> <p>The data is passed to the d3 script as a json array with a few different levels of arrays and objects. </p> <pre><code>object = { class: "show", name: "Game of Thrones", children : [ { class: "family", name: "Starks", children: [ { class: "members", name: "Rob" }, { class: "members", name: "Jon Snow" } ], }, { class: "family", name: "Lannisters" children: [ { class: "members", name: "Ser Jaime" }, { class: "members", name: "Cersei" } ], } ], } </code></pre> <p>I had no problem displaying the circles. I am now trying to create a navigation display on the side that maps out the hierarchy of the data. Ideally all I want is something like this:</p> <pre><code>&lt;ul&gt; &lt;li&gt; Game of Thrones &lt;ul&gt; &lt;li&gt; Starks &lt;ul&gt; &lt;li&gt; Rob &lt;/li&gt; &lt;li&gt; Jon Snow &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt; Lannisters &lt;ul&gt; &lt;li&gt; Ser Jaime &lt;/li&gt; &lt;li&gt; Cersei &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>It's a basic list structure. The only thing I've been able to do is access 1 level down in the data. I used 'divs' to just try to the the structure down first. </p> <pre><code>var sentimentData = data.children; var navBox = d3.select("body").append("body:div"); navBox .attr("class", "navBox"); var sentimentNav = navBox.selectAll('div') .data(sentimentData) .enter() .append('div') .text(function(d){ return d.name; }); </code></pre> <p>I haven't been able to proceed anymore levels past that. I was thinking a recursive way would probably be the best way. I tried this function below but it just appended divs to the top div and not the parent node.</p> <pre><code>function buildNav(d) { if (d.children) { children = d.children; d3.select('body').append('div') .data(children) .enter() .append('div') .attr("class", function(d) {return d.name; }); children.forEach(buildNav); d._children = d.children; d.children = null; } } buildNav(data); </code></pre> <p>Any suggestions to how to append the children to their parents, or how to access data multiple levels down would be greatly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload