donut_arc_tween.js 2.66 KB
Newer Older
Felipe Escala Torres committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* ------------------------------------------------------------------------------
 *
 *  # D3.js - arc tween animation
 *
 *  Demo d3.js demonstration of arc tween animation
 *
 *  Version: 1.0
 *  Latest update: August 1, 2015
 *
 * ---------------------------------------------------------------------------- */

$(function () {

    // Initialize chart
    donutTweenAnimation('#d3-donut-arc-tween', 120);

    // Chart setup
    function donutTweenAnimation(element, radius) {


        // Basic setup
        // ------------------------------

        // Define main variables
        var τ = 2 * Math.PI;


        // Create chart
        // ------------------------------

        // Add SVG element
        var container = d3.select(element).append("svg");

        // Add SVG group
        var svg = container
            .attr("width", radius * 2)
            .attr("height", radius * 2)
            .append("g")
                .attr("transform", "translate(" + radius + "," + radius + ")");


        // Construct chart layout
        // ------------------------------

        // Arc
        var arc = d3.svg.arc()
            .outerRadius(radius)
            .innerRadius(radius / 1.4)
            .startAngle(0);


        //
        // Append chart elements
        //

        // Add the background arc, from 0 to 100% (τ).
        var background = svg.append("path")
            .datum({endAngle: τ})
            .style("fill", "#eee")
            .attr("d", arc);

        // Add the foreground arc in orange, currently showing 12.7%.
        var foreground = svg.append("path")
            .datum({endAngle: .127 * τ})
            .style("fill", "#7986CB")
            .attr("d", arc);

        // Start a transition to a new random angle
        setInterval(function() {
            foreground.transition()
                .duration(750)
                .call(arcTween, Math.random() * τ);
        }, 1500);

        // Creates a tween on the specified transition's "d" attribute, transitioning
        // any selected arcs from their current angle to the specified new angle.
        function arcTween(transition, newAngle) {
            transition.attrTween("d", function(d) {

                // Interpolate between the two angles
                var interpolate = d3.interpolate(d.endAngle, newAngle);

                // Return value of the attrTween
                return function(t) {

                    // Calculate the current arc angle based on the transition time, t
                    d.endAngle = interpolate(t);

                    // Lastly, compute the arc path given the updated data
                    return arc(d);
                };
            });
        }
    }
});