D3.js donut 过渡

标签 d3.js transition updates donut-chart

我正在研究一个 d3 donut ,并坚持如何更新 donut 值,例如,如果您将值从 42 更改为 17,则该值将回流。
我可以删除 svg,但随后它会从零位置重写新值 (17)。

我希望它从 42 说倒流到 17。

var path = svg.selectAll("path")
        .data(pie(dataset.lower))
        .enter().append("path")
        .attr("class", function(d, i) { return "color" + i })
        .attr("d", arc)
        .each(function(d) { this._current = d; }); // store the initial values

这是我的 jsfidle 的链接 http://jsfiddle.net/yr595n96/

我希望您能提供任何帮助。

谢谢

最佳答案

继承人你的新按钮点击:

$("#next").click(function () {
    percent = 17;
    var progress = 0;
    var timeout = setTimeout(function () {
        clearTimeout(timeout);
        var randNumber = Math.random() * (100 - 0 + 1) + 0;
        //path = path.data(pie(calcPercent(17))); // update the data << change this
        path = path.data(pie(calcPercent(randNumber)));
        path.transition().duration(duration).attrTween("d", function (a) {
            // Store the displayed angles in _current.
            // Then, interpolate from _current to the new angles.
            // During the transition, _current is updated in-place by d3.interpolate.
            var i  = d3.interpolate(this._current, a);
            var i2 = d3.interpolate(progress, randNumber)
            this._current = i(0);
            return function(t) {
                text.text( format(i2(t) / 100) );
                return arc(i(t));
            };
        }); // redraw the arcs
    }, 100);
});

第 8 行的注意事项:
path = path.data(pie(calcPercent(randNumber)));

您需要传递新数据才能过渡到。我在这里使用了一个随机数只是为了向您展示任何数字都有效。我为此创建了一个变量并将其传递给“路径”和文本:“i2”> d3.interpolate。

您可以随时将其更改为 17 以适合您的要求:)

更新 fiddle :http://jsfiddle.net/yr595n96/3/

关于D3.js donut 过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651833/

相关文章:

javascript - D3 圆弧的一侧

javascript - 当鼠标悬停在圆圈上时显示标签

css - 在 css transition 中如何淡化一个图像而不是整个容器?

ios - 褪色的 UIImageView 过渡

sql-server - 在 SQL Server 2008 中仅更新同一表中的特定列后更新触发器中的一列

arrays - 更新 mongodb 嵌套数组文档中的第 n 个文档

javascript - 有限缩放的 d3 图

javascript - D3.js 中的 array_replace?

android - Activity 之间的过渡动​​画创建一个白尾神器

ios - 单击下一个和上一个UIButton时在UILabel中更改日期