我想在我的垂直条形图中添加标签,显示与当前柱高对应的当前百分比值。
因此,我需要不断更新百分比值,并且还需要过渡来使文本元素与柱状图同步移动。
我尝试过这个:
请查看这个fiddle。
var percentageLabels = svg.selectAll(".percentage-label")
.data(dataset);
percentageLabels.remove();
percentageLabels
.enter()
.append("text")
.attr("class", "percentage-label")
.style("fill", "white")
.text(function(d) {
return d;
})
.attr("y", function(d) {
return y(d);
})
.attr("x", function(d, i) {
return i * (w / dataset.length) + 2.5 / 100 * w + w * 10/100;
})
.transition().duration(1750).ease("linear")
.attr("y", function(d) {
return y(d);
});
请查看这个fiddle。