【发布时间】:2016-08-03 12:51:20
【问题描述】:
使用 D3.js v4,我尝试在按钮单击时更新圆的半径。
问题在于,每次更新时都会重绘圆圈而不是 smooth transitions between the radii(从“radius1”变为 0,然后才变为“radius2”)。
这是完整的代码: https://jsfiddle.net/4r6hp9my/
这是圈子更新代码sn-p:
var circles = svg.selectAll('circle').data(dataset);
var enter = circles
.enter()
.append('circle')
.attrs({
cx: w/2,
cy: h/2,
fill: colorsScale,
r: function(d,i){
if(i == myCounter){
var x = rScale(d)
return x;
}
}
});
d3.select('#theButton')
.on('click',function(){
myCounter++
if(myCounter == dataset.length){
myCounter = 0;
};
updateData()
});
function updateData(){
circles
.merge(enter)
.transition()
.attr('r',function(d,i){
if(i == myCounter){
return rScale(d);
}
});
labels
.text(function(d,i){
if(i == myCounter){
return d;
}
});
};
【问题讨论】:
-
第一次打开小提琴时你认为有多少个圆圈?
-
不确定我是否理解这个问题,应该只有一个圆,每次点击按钮都会更新其半径(基于一维数组中的值)
-
:) 打开检查器并查看 svg 元素内部。您已将 9 个圆圈附加到 svg。也就是说,每个
dataset元素为 1。 -
我的意思是;您每次点击都会更新这些圆圈的半径。他们每个人的半径都从无过渡到数据集中的值。这就是为什么它的动画就像每次点击都会创建一个新的圆圈。
标签: javascript transition d3.js