There is no built in way to do it, but you can achieve it in all kinds of ways, including
Scope:
var data = ['a','b','c','d']
d3.select('.parent').selectAll('.child')
.data(data)
.enter()
.append('div')
.attr('class', 'child')
.text(function(d,i) {
return "previous letter is " + data[i-1];
});
Linking (works even if they're Strings, as in this example):
var data = ['a','b','c','d']
for(var i = 0; i < data.length; i++) { data[i].previous = data[i-1]; }
d3.select('.parent').selectAll('.child')
.data(data)
...
.text(function(d,i) {
return "previous letter is " + d.previous
});
Via the parent node (Experimental):
var data = ['a','b','c','d']
d3.select('.parent').selectAll('.child')
.data(data)
...
.text(function(d,i) {
var parentData = d3.select(this.parentNode).selectAll('.child').data();
// NOTE: parentData === data is false, but parentData still deep equals data
return "previous letter is " + parentData[i-1];
});
Related to the last example, you can even try to find the sibling DOM node immediately preceding this node. Something like
...
.text(function(d,i) {
var previousChild = d3.select(this.parentNode).select('.child:nth-child(' + i + ')')
return "previous letter is " + previousChild.datum();
})
but the last two can fail in all kinds of ways, like if the DOM nodes aren't ordered the same as data
, or if there are other unrelated DOM nodes within the parent.