[英]Tweening text in d3.js - count up
I am trying to update the text in a d3v4 element - so it counts up -- it appears this.textContent is null?我正在尝试更新 d3v4 元素中的文本 - 所以它计数 - 看起来 this.textContent 为空? I've been trying to use this._current - but nothing appears to work.
我一直在尝试使用 this._current - 但似乎没有任何效果。
http://jsfiddle.net/hgka8w5q/ http://jsfiddle.net/hgka8w5q/
.duration(3000)
.tween("text", function(d) {
console.log(this.textContent)
var i = d3.interpolate(this.textContent, d),
prec = (d + "").split("."),
round = (prec.length > 1) ? Math.pow(10, prec[1].length) : 1;
return function(t) {
this.textContent = Math.round(i(t) * round) / round;
};
});
https://observablehq.com/@d3/transition-texttween https://observablehq.com/@d3/transition-texttween
.transition()
.duration(5000)
.textTween(function(d) {
const i = d3.interpolate(this._current, d);
return function(t) { return format(this._current = i(t)); };
})
this was the solution that worked in d3v4.这是在 d3v4 中有效的解决方案。
var text = svg.append("text")
.data(data)
.attr("x", 0)
.attr("y", 0)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "central")
.text(0)
.transition()
.duration(3000)
.tween("text",function(d) {
var self = this;
var i = d3.interpolate(this.textContent, d.value);
var prec = (d + "").split(".");
var round = (prec.length > 1) ? Math.pow(10, prec[1].length) : 1;
return function(t) {
self.textContent = Math.round(i(t) * round) / round;
};
});
and this code would animate a type text.并且此代码将为类型文本设置动画。
//typing text animation
var self = this;
var newText = "hellob";
var textLength = newText.length;
return function(t) {
self.textContent = newText.slice(0, Math.round(t * textLength));
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.