【发布时间】:2016-10-17 10:15:52
【问题描述】:
我想重复使用选择。 但更新方法不起作用。 为什么会这样?
第一个选择是否不能连续接受两个以上的数据绑定?
// 1. prepair data
var enterData = [120, 70, 300, 80, 220];
var updateData = [300, 0, 30, 70, 320];
var exitData = [300, 80, 90, 40];
// 2. select virtual element and assign it variable
var rects = d3.select("#myGraph")
.selectAll("rect");
// 3. enter method working.
function enter (rects, data){
console.log("enter method!");
rects.data(data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("width", function(d,i){
return d;
})
.attr("height", 20)
.attr("x", 0)
.attr("y", function(d, i){
return i * 25
})
}
// 4. but update not working
function update (rects, data){
console.log("update method!");
// 5. without below selection phrase
// var rects = d3.select("#myGraph")
// .selectAll("rect");
rects.data(data)
.attr("width", function(d,i){
return d;
})
}
function exit (rects, data){
console.log("exit method!");
rects.data(data).exit().remove();
}
setTimeout( function(){
enter(rects ,enterData)
}, 1500 )
setTimeout( function(){
update(rects, updateData)
}, 3000 )
setTimeout( function(){
exit(rects, exitData)
}, 4500 )
我知道如何解决它。但我不知道为什么。
【问题讨论】: