I use d3 to animate text to show the user's progress in completing a task. For example, if they completed 32.51% of the task, the text will animate from 0% to 32.51% in 2 seconds or so.
To do this, I use the d3 attrTween method for the svg text element in combination with d3.interpolate. Interpolation works fine, but I have a little problem with text formatting. I would like 4 digits to always be displayed in the text, therefore 0% = 00.00%, 4.31% = 04.31%, etc. It would be nice to be able to do this without having to publish what the interpolator returns. In other words, without having to accept the return percentage and check if there are 4 digits and add a zero pad on both sides before placing it in the DOM.
As a test, I tried to specify the format I would like by setting the a and b values ββfor the interpolator in this way to d3.interpolate("00.00", "30.00") , but the final text is "30" when trailing zeros are cut off.
Any suggestions?
source share