rendro / easy-pie-chart

easy pie chart is a lightweight plugin to draw simple, animated pie charts for single values
http://rendro.github.io/easy-pie-chart
MIT License
2.07k stars 501 forks source link

Start animation after scroll #155

Open thallysondias opened 9 years ago

thallysondias commented 9 years ago

I want the graphic load after I make the scroll . I tried this code:

//scroll menu
            $(function(){

            $(window).scroll(function(){
                //create instance
                $('.chart').easyPieChart({
                    animate: 2000
                });
                //update instance after 5 sec
                if($(window).scrollTop() > $(window).height() + 600) { 
                    $('.chart').data('easyPieChart').update(100);
                } 
            });
        });

The problem is that when it comes to the point he does not make the loading smoothly. Only appears with the full graphic. 0 to 100...

craig-mrare commented 7 years ago

Hey @thallysondias I had the same issue and figured out that because you're firing the update function on every scroll event, the plugin can't animate it. I got around this by setting a boolean to check whether the chart had already been updated:


var isActive = false;

if($(window).scrollTop() > $(window).height() + 600) {
    if(isActive === false){ 
        $('.chart').data('easyPieChart').update(100);
        isActive = true;
    }
} 

Hope this helps :)

MarioPerini commented 5 years ago

on scroll events should be debounced in general

pfuhrmann commented 5 years ago

The way to do this is to create an initial chart which is not animated without the barColor and then reset it + recreate again with the barColor and animation once scrolling.

Something like:

// prepare chart
$t.easyPieChart({
  animate    : { enabled : false },
  barColor   : false,
});
$(window).scroll(function(){
  // your logic
  $t.easyPieChart({
    animate    : { enabled : true },
    barColor   : '#fff',
  });
});