作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您能向我解释一下为什么第一个脚本不起作用吗? Firebug 说“d 未定义”。selectall之后val是一个二维数组。
<script type="text/javascript">
setInterval(function() {
d3.json("./cgi-bin/script1.sh?shmid=" + node.value, function(error, txt){
if (error) return console.warn(error);
table = d3.select("#shmid" + node.value);
val = table.selectAll(".val")
val.each(function(d,i){
console.debug(d,i)
d.text(txt.bd[i].val);
});
node = node.next;
})
}, 500);
</script>
工作变体:
<script type="text/javascript">
setInterval(function() {
d3.json("./cgi-bin/script1.sh?shmid=" + node.value, function(error, txt){
if (error) return console.warn(error);
table = d3.select("#shmid" + node.value);
val = table.selectAll(".val")
val.each(function(d,i){
console.debug(d,i)
d3.select(this).text(txt.bd[i].val);
});
node = node.next;
})
}, 500);
</script>
最佳答案
来自 the documentation :
Invokes the specified function for each element in the current selection, passing in the current datum d and index i, with the this context of the current DOM element.
在第一种情况下,您错误地使用了 d
—— 它指的是绑定(bind)到元素的数据,而不是元素本身。在你的例子中,你没有绑定(bind)任何数据,所以没有什么可引用的。
看起来您正在尝试执行以下操作:
table = d3.select("#shmid" + node.value);
table.selectAll(".val").data(txt.bd)
.text(function(d) { return d.val; });
这将首先将数据绑定(bind)到您正在使用的元素,然后使用绑定(bind)的数据设置文本
。
关于d3.js - 如何在 d3 中使用 .each?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31957756/
我是一名优秀的程序员,十分优秀!