更新的數據集,我想創建使用D3使用下面的數據列表:D3:不更新DOM
var dataSet = [
{ label: 'a', value: 10},
{ label: 'b', value: 20},
{ label: 'c', value: 30},
{ label: 'd', value: 40}
];
var circle = svg.selectAll('circle')
.data(dataSet)
.enter()
.append('circle')
.attr({
r:function(d){ return d.value },
cx:function(d, i){ return i * 100 + 50 },
cy:50,
fill: 'red'
});
其中一期工程。現在,一段時間後,我改變數據
dataSet[0].value = 40;
dataSet[1].value = 30;
dataSet[2].value = 20;
dataSet[3].value = 10;
,我想再次提請名單:
setTimeout(function() {
var circle = svg.selectAll('circle')
.data(dataSet, function (d) {
return d.label;
})
.sort(function (a,b){ return d3.ascending(a.value, b.value);})
.enter()
.append('circle')
.attr({
r:function(d){ return d.value },
cx:function(d, i){ return i * 100 + 50 },
cy:50,
fill: 'red'
});
},1000);
然而,這個名單是不是真的更新。任何建議如何解決這個問題?
你的小提琴似乎不工作,所以我已經修復它[這裏](http://jsfiddle.net/Q5Jag/1193/)Thnx –
@JeanlucaScaljeri我已經離開'.sort()',因爲這就是你原來的小提琴。雖然沒有它可能會更有意義:) –