D3: Strange behavior with transition() when set a value of percentage

Created on 13 Sep 2013  路  3Comments  路  Source: d3/d3

some code go like this:

var percent=80
d3.select("#some-div")
.style("width",0)
.transition()
.duration("800")
.style("width",percent+"%")

If I calculate it as an exact value first, it'll be OK.

Most helpful comment

In case someone lands here - this is my working styleTween:

.styleTween('width', function (d, i, a) {
    var from = this.style.width, // 10% 
        to = x(d); //85%
    return d3.interpolateString(from, to);
});

All 3 comments

This is the expected behavior. As described in Working with Transitions and the API reference, transition.style uses computed style values. In the case of the width style, that鈥檚 in pixels rather than percentages. So you either need to use styleTween to specify the starting value explicitly, or specify the ending style value in a manner consistent with its computed value, i.e., pixels.

Thanks very much!

In case someone lands here - this is my working styleTween:

.styleTween('width', function (d, i, a) {
    var from = this.style.width, // 10% 
        to = x(d); //85%
    return d3.interpolateString(from, to);
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

majorye picture majorye  路  4Comments

IPWright83 picture IPWright83  路  4Comments

mbostock picture mbostock  路  6Comments

seonixx picture seonixx  路  4Comments

ro-ka picture ro-ka  路  4Comments