1

I recently decided to use nested json, however, I cannot access them. I have seen some examples of different way to return data using the data method e.g.:

 var node = svg.selectAll("g.node")
 .data(nodes, function(d) {return d.world;})

  var node = svg.selectAll("g.node")
 .data(nodes)

However, this has had no affect. Therefore I've also tried:

 .text(function(d) { return d.world.Name; })

But I still havent had any luck!

Here's a snippet of the nested json:

           var nodes = {"Galaxy":"Milkyway","life":true,"world": [
         {"Name": 'Amy', "Country": 'USA', "translate":"translate(190,83)" }, 
         {"Name": 'Nathan', "Country": 'USA', "type": 'square', "translate":  "translate(190,83)"}, 
         {"Name": 'Lewis', "Country": 'USA', "type": 'circle', "translate":  "translate(20,183)" }, 
         {"Name": 'Tom', "Country": 'Japan', "type": 'diamond', "translate":  "translate(-300,-200)"},
         {"Name": 'Matt', "Country": 'Russia', "type": 'cross', "translate": "translate(10,183)"},
         {"Name": 'Frank', "Country": 'France', "type": 'triangle-up', "translate": "translate(30,183)"},
         {"Name": 'Bob', "Country": 'Japan', "type": 'diamond', "translate": "translate(10,13)"}
]}; 

thanks in advance

3
  • The form of .data function you used in the first instance is meant to be used for object consistency by assigning keys to each data item. .data does not follow the same accessor pattern as d3.max, d3.mean, etc. Commented Mar 7, 2013 at 14:10
  • Hi, thanks for reply. is it possible that you could show me an example? Commented Mar 7, 2013 at 15:51
  • This example from mbostock himself would be helpful to understand how the key functionality works: bost.ocks.org/mike/constancy Commented Mar 7, 2013 at 20:37

1 Answer 1

1

I believe I've found an appropriate solution to my problem:

   var node = svg.selectAll("g.node")
                 .data(nodes.world)

Please, if you have another I'm open to other ways of doing things. Thanks :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.