1

Can't seem to find the answer to this question.

Does d3.js need the data to be in a particular format. For example:

var dataset = {
"people": [
  {
    "name": "person1",
    "projects": [
      {
        "name": "project1"
      },
      {
        "name": "project3"
      }
    ]
  },
  {
    "name": "person2",
    "projects": [
      {
        "name": "project1"
      },
      {
        "name": "project2"
      }
    ]
  }
 ]
};

And then using this:

d3.select('body')
.selectAll('p')
  .data(dataset)
  .enter()
  .append('p')
  .text(function(d){
    return d;
  }); 

Will not return anything.

Hope someone can help.

The docs and other tutorials all say you can use the above but always use the array as an example/demo.

1

1 Answer 1

3

As @MikeAtkins says you are trying to data bind the outer object. Try:

var dataset = {
"people": [
  {
    "name": "person1",
    "projects": [
      ...
    ]
  },
  ...
 ]
};

d3.select('body')
.selectAll('p')
  .data(dataset.people)
  .enter()
  .append('p')
  .text(function(d){
    return d.name;
  }); 

If you want to bind to the NESTED arrays (i.e. each project for each person) you should see this tutorial.

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.