"values"라는 데이터 배열에 대한 히스토그램을 생성하는 jsFiddle here에 몇 가지 코드가 있습니다. 그게 다 잘되고 잘됐다.D3로 막대 그래프 애니메이션하기
"newData"라는 새로운 데이터 배열로이 막대 그래프를 업데이트하려고 할 때 문제가 발생합니다. 나는 enter()
, update()
, exit()
D3 전략을 고수하려고 노력하고 있습니다. 애니메이션은 실제로 발생하지만 바이올린으로 볼 수 있듯이 모든 것을 오른쪽 상단 모서리에 밀어 넣습니다. 누군가이 코드의이 부분 (업데이트)에서 내가 잘못하고있는 것을 지적 할 수 있습니까?
//Animations
d3.select('#new')
.on('click', function(d,i) {
var newHist = d3.layout.histogram().bins(x.ticks(bins))(newData);
var rect = svg.selectAll(".bar")
.data(values, function(d) { return d; });
// enter
rect.enter().insert("g", "g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(" + x(d) + "," + y(d) + ")"; });
rect.enter().append("rect")
.attr("x", 1)
.attr("width", w)
.attr("height", function(d) { return y(d); });
rect.enter().append("text")
.attr("dy", ".75em")
.attr("y", 6)
.attr("x", x(histogram[0].dx)/2)
.attr("text-anchor", "middle")
.text(function(d) { return formatCount(d); });
// update
svg.selectAll('.bar')
.data(newHist)
.transition()
.duration(3000)
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
svg.selectAll("rect")
.data(newHist)
.transition()
.duration(3000)
.attr("height", function(d) { return height - y(d.y); });
svg.selectAll("text")
.data(newHist)
.transition()
.duration(3000)
.text(function(d) { return formatCount(d.y); });
// exit
rect.exit()
.remove();
});
코드 전체가 위에 링크 된 JSFiddle에 있습니다. 감사! 위의 코드와 바이올린을 보면
는