Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

d3.js - d3 Missing first value in array

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...