4

I think I have a question that's pretty simple but I can't figure it out. My array is as follows:

var wordcount = [1, 2, 3, [{name: 'A', values: [0,1,3,9, 8, 7]},
                           {name: 'B', values: [0, 10, 7, 1, 1, 11]}, 
                           {name: 'C', values: [3, 1, 4, 4, 4, 17]},
                           {name: 'D', values: [4, 77, 2, 13, 11, 13]}
]]]

and I'm using the following code to get A, B, C, and D

        d3.select("#tooltippos")                    
          .data(d[3])
          .enter()
          .append("div")
          .text(function(d) { return d.name; });

but I keep missing the first letter. Only B, C, and D show up in the divs.

1 Answer 1

10

You need to select the non-existent elements as well for the selections to work properly. That is, your code should be

d3.select("#tooltippos").selectAll("div")                
      .data(d[3])
      .enter()
      .append("div")
      .text(function(d) { return d.name; });

At the moment, the selection you're matching data against contains only the one element ("#tooltippos"), which is matched with the first element of the data. Hence, this is not in the enter selection and not drawn.

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.