javascript - D3.js饼图逆向动画

标签 javascript animation d3.js reverse

正如标题所说。如何反转饼图 (D3.js) 中的动画路径。默认情况下,饼图以动画顺时针方向呈现。我该如何扭转它?

见图片示例。

enter image description here

enter image description here

这里是 JS:

    var pie = d3.layout.pie()
        .sort(null)
        .startAngle(1 * Math.PI)
        .endAngle(3 * Math.PI)
        .value(function (d) { return d.percentage; });

        g.append("path")
            .attr("d", arc)
            .style("fill", function (d) { return d.data.color; })
            .attr({
                "fill": function (d) {
                    return d.data.color;
                }
            })
            .transition()
            .duration(1000)
            .attrTween("d", function (d) {
                var i = d3.interpolate(d.startAngle, d.endAngle);
                return function (t) {
                    d.endAngle = i(t);
                    return arc(d);
                }
            });

最佳答案

只需将 endAnglestartAngle 交换即可:

.transition()
.duration(1000)
.attrTween("d", function (d) {
    var i = d3.interpolate(d.endAngle, d.startAngle);
    return function (t) {
        d.startAngle = i(t);
        return arc(d);
    }
});

检查片段:

var dataset = {
  apples: [5345, 2879, 1997, 2437, 4045],
};

var width = 460,
    height = 300,
    radius = Math.min(width, height) / 2;

var color = d3.scale.category20();

var pie = d3.layout.pie()
    .sort(null);

var arc = d3.svg.arc()
    .innerRadius(radius - 100)
    .outerRadius(radius - 50);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var path = svg.selectAll("path")
    .data(pie(dataset.apples))
  .enter().append("path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc)
    .transition()
            .duration(1000)
            .attrTween("d", function (d) {
                var i = d3.interpolate(d.endAngle, d.startAngle);
                return function (t) {
                    d.startAngle = i(t);
                    return arc(d);
                }
            });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

关于javascript - D3.js饼图逆向动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38956484/

相关文章:

javascript - 为什么在网络浏览器中用作字符串时失败说 d3 未定义

javascript - 带 D3 的堆积条形图

javascript - 将字符串添加到 iframe 参数

javascript - 如何在 Lit-HTML 中使用事件监听器添加和删除类?

javascript - 查看 javascript 中的所有超时/间隔?

ios - CALayer 上的隐式动画没有动画

html - 旋转和移动球图像 Html 5

jQuery 滚动 100%(动画)

javascript - 将所选元素移动到末尾

css - 使用 NVD3 图表在轴和文本之间添加一个空格