D3获取.data()中的上一个元素 [英] D3 get previous element in .data()
本文介绍了D3获取.data()中的上一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获得在d3中传递给 .data()
的数据集的上一个元素的值?
How can I get the value of the previous element of the dataset passed to .data()
in d3?
我知道在回调中我不能做像
I know in a callback I cant do something like
function(d,i){console.log(d,i); })
打印数据元素及其索引到控制台。但是如何引用前面的元素?
for example, to print the data element and its index to the console. But how can i reference the previous element?
像 d [i-1]
或某物吗?
推荐答案
您可以像这样获得上一个元素的值。
You can get the value of previous element like this.
var texts = svg.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("x",function(d){ return d.x; })
.attr("y",function(d){ return d.y; })
.text(function(d){ return d.name; });
texts.attr(...,function(d,i){
......
var prevData = texts.data()[i-1]; //check whether i>0 before this code
.......
});
这是一个小例子 JSFiddle ,将鼠标悬停在文本上以查看功能。
Here is a small example JSFiddle , Mouse over the texts to see the functionality.
这篇关于D3获取.data()中的上一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文