d3数组中缺少第一个值 [英] d3 Missing first value in array

查看:200
本文介绍了d3数组中缺少第一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我有一个很简单的问题,但我不能弄清楚。我的数组如下:

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]}
]]]

我使用下面的代码得到A,B,C,和D

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.

推荐答案

您还需要选择不存在的元素选择正常工作。也就是说,您的代码应为

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; });

此时,匹配数据的选择只包含一个元素(#tooltippos ),其与数据的第一元素匹配。因此,这不是在输入选择和不绘制。

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.

这篇关于d3数组中缺少第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆