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

查看:15
本文介绍了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; });

但我总是漏掉第一个字母.div 中仅显示 B、C 和 D.

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天全站免登陆