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; });
d3.js
add a comment |
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; });
d3.js
your tween function returns a d3 selection. it should justreturn i;
. Also look if it should not beattrTween('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
add a comment |
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; });
d3.js
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
d3.js
asked Nov 22 at 15:51
Basit
4,0343696169
4,0343696169
your tween function returns a d3 selection. it should justreturn i;
. Also look if it should not beattrTween('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
add a comment |
your tween function returns a d3 selection. it should justreturn i;
. Also look if it should not beattrTween('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
add a comment |
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 :)
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
add a comment |
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>
@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 usedthis
but didn't used3.select(that)
. Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted theYaroslav 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
add a comment |
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 :)
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
add a comment |
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 :)
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
add a comment |
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 :)
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 :)
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
add a comment |
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
add a comment |
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>
@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 usedthis
but didn't used3.select(that)
. Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted theYaroslav 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
add a comment |
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>
@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 usedthis
but didn't used3.select(that)
. Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted theYaroslav 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
add a comment |
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>
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>
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 usedthis
but didn't used3.select(that)
. Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted theYaroslav 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
add a comment |
@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 usedthis
but didn't used3.select(that)
. Yes you are right. I am very sorry. It is working. Please forgive me. Actually I accepted theYaroslav 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
your tween function returns a d3 selection. it should just
return i;
. Also look if it should not beattrTween('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