Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 Missing first value in array

Tags:

arrays

d3.js

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.

like image 303
B T Avatar asked Dec 26 '22 18:12

B T


1 Answers

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.

like image 132
Lars Kotthoff Avatar answered Feb 14 '23 15:02

Lars Kotthoff