How to apply text transition to number in d3











up vote
0
down vote

favorite












I am new to d3. I created a bar chart. Append text and percentage in the bar chart with animation. When bar chart draw then the number and percentage go from bottom to top to the desired location. Here is the code



svg.selectAll(".bar")
.data(data)
.enter()
.append("g")
.attr("class", "g rect")
.append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.label); })
.attr("y", h)
.on("mouseover", onMouseOver) //Add listener for the mouseover event
... // attach other events
.transition()
.ease(d3.easeLinear)
.duration(2000)
.delay(function (d, i) {
return i * 50;
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)); })
.attr("width", x.bandwidth() - 15) // v4’s console.log(bands.bandwidth()) replaced v3’s console.log(bands.rangeband()).
.attr("height", function(d) { return h - y(d.percentage.slice(0, -1)); }) // use .slice to remove percentage sign at the end of number
.attr("fill", function(d) { return d.color; });

var legend = svg.append("g");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.value })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 15; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1) / 2);}) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.style("stroke", "papayawhip")
.style("fill", "papayawhip");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Now I want to apply text transition. Like instead of just printing say 90%(d.percentage). I want that it starts from 0 and goes to d.percentage gradually. How can I apply text transition in this case. I tried the following but it didn't work



svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
d3.select(this).text(i(t));
};
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });









share|improve this question






















  • your tween function returns a d3 selection. it should just return i;. Also look if it should not be attrTween('text', ....)
    – rioV8
    Nov 22 at 16:01












  • I tried to return I but it is not working. I tried this .tween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; }). But still not working ...
    – Basit
    Nov 22 at 16:05










  • and if you do .attrTween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; })
    – rioV8
    Nov 22 at 16:09










  • .attrTween('text') is also not working ...
    – Basit
    Nov 22 at 16:14















up vote
0
down vote

favorite












I am new to d3. I created a bar chart. Append text and percentage in the bar chart with animation. When bar chart draw then the number and percentage go from bottom to top to the desired location. Here is the code



svg.selectAll(".bar")
.data(data)
.enter()
.append("g")
.attr("class", "g rect")
.append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.label); })
.attr("y", h)
.on("mouseover", onMouseOver) //Add listener for the mouseover event
... // attach other events
.transition()
.ease(d3.easeLinear)
.duration(2000)
.delay(function (d, i) {
return i * 50;
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)); })
.attr("width", x.bandwidth() - 15) // v4’s console.log(bands.bandwidth()) replaced v3’s console.log(bands.rangeband()).
.attr("height", function(d) { return h - y(d.percentage.slice(0, -1)); }) // use .slice to remove percentage sign at the end of number
.attr("fill", function(d) { return d.color; });

var legend = svg.append("g");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.value })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 15; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1) / 2);}) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.style("stroke", "papayawhip")
.style("fill", "papayawhip");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Now I want to apply text transition. Like instead of just printing say 90%(d.percentage). I want that it starts from 0 and goes to d.percentage gradually. How can I apply text transition in this case. I tried the following but it didn't work



svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
d3.select(this).text(i(t));
};
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });









share|improve this question






















  • your tween function returns a d3 selection. it should just return i;. Also look if it should not be attrTween('text', ....)
    – rioV8
    Nov 22 at 16:01












  • I tried to return I but it is not working. I tried this .tween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; }). But still not working ...
    – Basit
    Nov 22 at 16:05










  • and if you do .attrTween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; })
    – rioV8
    Nov 22 at 16:09










  • .attrTween('text') is also not working ...
    – Basit
    Nov 22 at 16:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am new to d3. I created a bar chart. Append text and percentage in the bar chart with animation. When bar chart draw then the number and percentage go from bottom to top to the desired location. Here is the code



svg.selectAll(".bar")
.data(data)
.enter()
.append("g")
.attr("class", "g rect")
.append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.label); })
.attr("y", h)
.on("mouseover", onMouseOver) //Add listener for the mouseover event
... // attach other events
.transition()
.ease(d3.easeLinear)
.duration(2000)
.delay(function (d, i) {
return i * 50;
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)); })
.attr("width", x.bandwidth() - 15) // v4’s console.log(bands.bandwidth()) replaced v3’s console.log(bands.rangeband()).
.attr("height", function(d) { return h - y(d.percentage.slice(0, -1)); }) // use .slice to remove percentage sign at the end of number
.attr("fill", function(d) { return d.color; });

var legend = svg.append("g");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.value })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 15; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1) / 2);}) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.style("stroke", "papayawhip")
.style("fill", "papayawhip");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Now I want to apply text transition. Like instead of just printing say 90%(d.percentage). I want that it starts from 0 and goes to d.percentage gradually. How can I apply text transition in this case. I tried the following but it didn't work



svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
d3.select(this).text(i(t));
};
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });









share|improve this question













I am new to d3. I created a bar chart. Append text and percentage in the bar chart with animation. When bar chart draw then the number and percentage go from bottom to top to the desired location. Here is the code



svg.selectAll(".bar")
.data(data)
.enter()
.append("g")
.attr("class", "g rect")
.append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.label); })
.attr("y", h)
.on("mouseover", onMouseOver) //Add listener for the mouseover event
... // attach other events
.transition()
.ease(d3.easeLinear)
.duration(2000)
.delay(function (d, i) {
return i * 50;
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)); })
.attr("width", x.bandwidth() - 15) // v4’s console.log(bands.bandwidth()) replaced v3’s console.log(bands.rangeband()).
.attr("height", function(d) { return h - y(d.percentage.slice(0, -1)); }) // use .slice to remove percentage sign at the end of number
.attr("fill", function(d) { return d.color; });

var legend = svg.append("g");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.value })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 15; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1) / 2);}) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.style("stroke", "papayawhip")
.style("fill", "papayawhip");

svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Now I want to apply text transition. Like instead of just printing say 90%(d.percentage). I want that it starts from 0 and goes to d.percentage gradually. How can I apply text transition in this case. I tried the following but it didn't work



svg.selectAll(".g.rect").append("text")
.text(function(d) { return d.percentage; })
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
d3.select(this).text(i(t));
};
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });






d3.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 15:51









Basit

4,0343696169




4,0343696169












  • your tween function returns a d3 selection. it should just return i;. Also look if it should not be attrTween('text', ....)
    – rioV8
    Nov 22 at 16:01












  • I tried to return I but it is not working. I tried this .tween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; }). But still not working ...
    – Basit
    Nov 22 at 16:05










  • and if you do .attrTween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; })
    – rioV8
    Nov 22 at 16:09










  • .attrTween('text') is also not working ...
    – Basit
    Nov 22 at 16:14


















  • your tween function returns a d3 selection. it should just return i;. Also look if it should not be attrTween('text', ....)
    – rioV8
    Nov 22 at 16:01












  • I tried to return I but it is not working. I tried this .tween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; }). But still not working ...
    – Basit
    Nov 22 at 16:05










  • and if you do .attrTween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; })
    – rioV8
    Nov 22 at 16:09










  • .attrTween('text') is also not working ...
    – Basit
    Nov 22 at 16:14
















your tween function returns a d3 selection. it should just return i;. Also look if it should not be attrTween('text', ....)
– rioV8
Nov 22 at 16:01






your tween function returns a d3 selection. it should just return i;. Also look if it should not be attrTween('text', ....)
– rioV8
Nov 22 at 16:01














I tried to return I but it is not working. I tried this .tween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; }). But still not working ...
– Basit
Nov 22 at 16:05




I tried to return I but it is not working. I tried this .tween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; }). But still not working ...
– Basit
Nov 22 at 16:05












and if you do .attrTween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; })
– rioV8
Nov 22 at 16:09




and if you do .attrTween("text", function(d) { var i = d3.interpolate(0, d.percentage.slice(0, -1)); return i; })
– rioV8
Nov 22 at 16:09












.attrTween('text') is also not working ...
– Basit
Nov 22 at 16:14




.attrTween('text') is also not working ...
– Basit
Nov 22 at 16:14












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Your problem is that you return function(t) { ... } and try to access this of parent function inside. The solution is to return arrow function, which does not shadow this value:



return t => d3.select(this).text(i(t));


(by the way, you may also want to round percentage before printing)



-------------Edit --------------



Here is the working code



var numberFormat = d3.format("d");

svg.selectAll(".g.rect").append("text")
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var element = d3.select(this);
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
var percent = numberFormat(i(t));
element.text(percent + "%");
};
//return t => element.text(format(i(t)));
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Thanks :)






share|improve this answer























  • yes you are right. It is working. But on older browsers arrow function is not recognized. On IE 9. But you are right. Arrow function is working on Edge or Chrome. But not on IE 9
    – Basit
    Nov 22 at 16:36


















up vote
1
down vote













The problem is the this value.



Save it in the closure (that).



Use a number interpolator so you can round the result to a number of decimals.






var ptag = d3.select('body').append('p');

ptag.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var that = this;
var i = d3.interpolate(0, 90); // Number(d.percentage.slice(0, -1))
return function(t) {
d3.select(that).text(i(t).toFixed(2));
};
})

<script src="https://d3js.org/d3.v5.min.js"></script>








share|improve this answer





















  • @Basit: if I press "Run Code Snippet" it works (in Chrome).
    – rioV8
    Nov 22 at 16:45










  • Ah Sorry. Yes you are right it is working. I used this but didn't use d3.select(that). Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted the Yaroslav Sergienko answer because I tried it before your provided answer. And it answer worked. Then I modified it for closure and it worked on my code too and then your answer came. But please I am again sorry for my wrong comment. I can see if I can remove it. Thanks for your help friend :)
    – Basit
    Nov 22 at 16:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434499%2fhow-to-apply-text-transition-to-number-in-d3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Your problem is that you return function(t) { ... } and try to access this of parent function inside. The solution is to return arrow function, which does not shadow this value:



return t => d3.select(this).text(i(t));


(by the way, you may also want to round percentage before printing)



-------------Edit --------------



Here is the working code



var numberFormat = d3.format("d");

svg.selectAll(".g.rect").append("text")
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var element = d3.select(this);
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
var percent = numberFormat(i(t));
element.text(percent + "%");
};
//return t => element.text(format(i(t)));
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Thanks :)






share|improve this answer























  • yes you are right. It is working. But on older browsers arrow function is not recognized. On IE 9. But you are right. Arrow function is working on Edge or Chrome. But not on IE 9
    – Basit
    Nov 22 at 16:36















up vote
0
down vote



accepted










Your problem is that you return function(t) { ... } and try to access this of parent function inside. The solution is to return arrow function, which does not shadow this value:



return t => d3.select(this).text(i(t));


(by the way, you may also want to round percentage before printing)



-------------Edit --------------



Here is the working code



var numberFormat = d3.format("d");

svg.selectAll(".g.rect").append("text")
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var element = d3.select(this);
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
var percent = numberFormat(i(t));
element.text(percent + "%");
};
//return t => element.text(format(i(t)));
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Thanks :)






share|improve this answer























  • yes you are right. It is working. But on older browsers arrow function is not recognized. On IE 9. But you are right. Arrow function is working on Edge or Chrome. But not on IE 9
    – Basit
    Nov 22 at 16:36













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Your problem is that you return function(t) { ... } and try to access this of parent function inside. The solution is to return arrow function, which does not shadow this value:



return t => d3.select(this).text(i(t));


(by the way, you may also want to round percentage before printing)



-------------Edit --------------



Here is the working code



var numberFormat = d3.format("d");

svg.selectAll(".g.rect").append("text")
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var element = d3.select(this);
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
var percent = numberFormat(i(t));
element.text(percent + "%");
};
//return t => element.text(format(i(t)));
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Thanks :)






share|improve this answer














Your problem is that you return function(t) { ... } and try to access this of parent function inside. The solution is to return arrow function, which does not shadow this value:



return t => d3.select(this).text(i(t));


(by the way, you may also want to round percentage before printing)



-------------Edit --------------



Here is the working code



var numberFormat = d3.format("d");

svg.selectAll(".g.rect").append("text")
.attr("x", function(d) { return x(d.label) + x.bandwidth() / 2 - 20; })
.attr("y", h)
.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var element = d3.select(this);
var i = d3.interpolate(0, d.percentage.slice(0, -1));
return function(t) {
var percent = numberFormat(i(t));
element.text(percent + "%");
};
//return t => element.text(format(i(t)));
})
.attr("y", function(d) { return y(d.percentage.slice(0, -1)) - 10; }) // use slice to remove percentage sign from the end of number
.attr("dy", ".35em")
.attr("fill", function(d) { return d.color; });


Thanks :)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 16:59









Basit

4,0343696169




4,0343696169










answered Nov 22 at 16:13









Yaroslav Sergienko

40016




40016












  • yes you are right. It is working. But on older browsers arrow function is not recognized. On IE 9. But you are right. Arrow function is working on Edge or Chrome. But not on IE 9
    – Basit
    Nov 22 at 16:36


















  • yes you are right. It is working. But on older browsers arrow function is not recognized. On IE 9. But you are right. Arrow function is working on Edge or Chrome. But not on IE 9
    – Basit
    Nov 22 at 16:36
















yes you are right. It is working. But on older browsers arrow function is not recognized. On IE 9. But you are right. Arrow function is working on Edge or Chrome. But not on IE 9
– Basit
Nov 22 at 16:36




yes you are right. It is working. But on older browsers arrow function is not recognized. On IE 9. But you are right. Arrow function is working on Edge or Chrome. But not on IE 9
– Basit
Nov 22 at 16:36












up vote
1
down vote













The problem is the this value.



Save it in the closure (that).



Use a number interpolator so you can round the result to a number of decimals.






var ptag = d3.select('body').append('p');

ptag.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var that = this;
var i = d3.interpolate(0, 90); // Number(d.percentage.slice(0, -1))
return function(t) {
d3.select(that).text(i(t).toFixed(2));
};
})

<script src="https://d3js.org/d3.v5.min.js"></script>








share|improve this answer





















  • @Basit: if I press "Run Code Snippet" it works (in Chrome).
    – rioV8
    Nov 22 at 16:45










  • Ah Sorry. Yes you are right it is working. I used this but didn't use d3.select(that). Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted the Yaroslav Sergienko answer because I tried it before your provided answer. And it answer worked. Then I modified it for closure and it worked on my code too and then your answer came. But please I am again sorry for my wrong comment. I can see if I can remove it. Thanks for your help friend :)
    – Basit
    Nov 22 at 16:55















up vote
1
down vote













The problem is the this value.



Save it in the closure (that).



Use a number interpolator so you can round the result to a number of decimals.






var ptag = d3.select('body').append('p');

ptag.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var that = this;
var i = d3.interpolate(0, 90); // Number(d.percentage.slice(0, -1))
return function(t) {
d3.select(that).text(i(t).toFixed(2));
};
})

<script src="https://d3js.org/d3.v5.min.js"></script>








share|improve this answer





















  • @Basit: if I press "Run Code Snippet" it works (in Chrome).
    – rioV8
    Nov 22 at 16:45










  • Ah Sorry. Yes you are right it is working. I used this but didn't use d3.select(that). Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted the Yaroslav Sergienko answer because I tried it before your provided answer. And it answer worked. Then I modified it for closure and it worked on my code too and then your answer came. But please I am again sorry for my wrong comment. I can see if I can remove it. Thanks for your help friend :)
    – Basit
    Nov 22 at 16:55













up vote
1
down vote










up vote
1
down vote









The problem is the this value.



Save it in the closure (that).



Use a number interpolator so you can round the result to a number of decimals.






var ptag = d3.select('body').append('p');

ptag.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var that = this;
var i = d3.interpolate(0, 90); // Number(d.percentage.slice(0, -1))
return function(t) {
d3.select(that).text(i(t).toFixed(2));
};
})

<script src="https://d3js.org/d3.v5.min.js"></script>








share|improve this answer












The problem is the this value.



Save it in the closure (that).



Use a number interpolator so you can round the result to a number of decimals.






var ptag = d3.select('body').append('p');

ptag.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var that = this;
var i = d3.interpolate(0, 90); // Number(d.percentage.slice(0, -1))
return function(t) {
d3.select(that).text(i(t).toFixed(2));
};
})

<script src="https://d3js.org/d3.v5.min.js"></script>








var ptag = d3.select('body').append('p');

ptag.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var that = this;
var i = d3.interpolate(0, 90); // Number(d.percentage.slice(0, -1))
return function(t) {
d3.select(that).text(i(t).toFixed(2));
};
})

<script src="https://d3js.org/d3.v5.min.js"></script>





var ptag = d3.select('body').append('p');

ptag.transition()
.ease(d3.easeLinear)
.duration(2000)
.tween("text", function(d) {
var that = this;
var i = d3.interpolate(0, 90); // Number(d.percentage.slice(0, -1))
return function(t) {
d3.select(that).text(i(t).toFixed(2));
};
})

<script src="https://d3js.org/d3.v5.min.js"></script>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 16:21









rioV8

3,8262211




3,8262211












  • @Basit: if I press "Run Code Snippet" it works (in Chrome).
    – rioV8
    Nov 22 at 16:45










  • Ah Sorry. Yes you are right it is working. I used this but didn't use d3.select(that). Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted the Yaroslav Sergienko answer because I tried it before your provided answer. And it answer worked. Then I modified it for closure and it worked on my code too and then your answer came. But please I am again sorry for my wrong comment. I can see if I can remove it. Thanks for your help friend :)
    – Basit
    Nov 22 at 16:55


















  • @Basit: if I press "Run Code Snippet" it works (in Chrome).
    – rioV8
    Nov 22 at 16:45










  • Ah Sorry. Yes you are right it is working. I used this but didn't use d3.select(that). Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted the Yaroslav Sergienko answer because I tried it before your provided answer. And it answer worked. Then I modified it for closure and it worked on my code too and then your answer came. But please I am again sorry for my wrong comment. I can see if I can remove it. Thanks for your help friend :)
    – Basit
    Nov 22 at 16:55
















@Basit: if I press "Run Code Snippet" it works (in Chrome).
– rioV8
Nov 22 at 16:45




@Basit: if I press "Run Code Snippet" it works (in Chrome).
– rioV8
Nov 22 at 16:45












Ah Sorry. Yes you are right it is working. I used this but didn't use d3.select(that). Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted the Yaroslav Sergienko answer because I tried it before your provided answer. And it answer worked. Then I modified it for closure and it worked on my code too and then your answer came. But please I am again sorry for my wrong comment. I can see if I can remove it. Thanks for your help friend :)
– Basit
Nov 22 at 16:55




Ah Sorry. Yes you are right it is working. I used this but didn't use d3.select(that). Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted the Yaroslav Sergienko answer because I tried it before your provided answer. And it answer worked. Then I modified it for closure and it worked on my code too and then your answer came. But please I am again sorry for my wrong comment. I can see if I can remove it. Thanks for your help friend :)
– Basit
Nov 22 at 16:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434499%2fhow-to-apply-text-transition-to-number-in-d3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu