LpLrich
LpLrich

Reputation: 2533

Can anyone figure out how to manually trigger the animation of this piechart (XYPieChart)?

Can anyone take a look at the XYPieChart files and see if there is a way to manually trigger the animation. I already tried reloadData but that doesn't do it.

You can grab the demo from github and add a button and try various methods to kick the animation off. As I said [self.pieChartLeft reloadData] won't do it.

Upvotes: 2

Views: 378

Answers (1)

Dominik Hadl
Dominik Hadl

Reputation: 3619

As I understand it you are looking for the animation of the chart filling up.

To achieve that, you first have to remove all the elements and reloadData afterwards then wait until the animation finishes and only after that add all the elements again and reloadData again.

So, to show it on the demo, I've just modified the clearSlices method in the ViewController.m file to this:

- (IBAction)clearSlices {
    [_slices removeAllObjects];
    [self.pieChartLeft reloadData];
    [self.pieChartRight reloadData];

    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        for (int i = 0; i < 4; i++)
            [_slices addObject:@(rand()%60+20)];
       [self updateSlices];
    });
}

The delay of course has to be equal to your animation length. Try it out and see if thats what you want, otherwise leave a comment and I'll find a way how to fix it.

I also think that the XYPieChart is a little bit messy and could be improved - there should be methods for animating the chart manually (as what you want to do). Maybe I'll make a fork :)

EDIT
You could tweak your code a little if you don't want to remove the data entirely by adding an instance boolean variable, let's say BOOL _showAnimationOnly or anything to your liking. And then in the delegate method pieChart:valueForSliceAtIndex: you would just provide 0 if this boolean is YES. You have to control that variable of course, setting it on and off at different places (depends on your code). Something like this:

- (IBAction)animateSlices {
    _showAnimationOnly = YES;
    [self.pieChartLeft reloadData];
    [self.pieChartRight reloadData];

    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
       _showAnimationOnly = NO;
       [self.pieChartLeft reloadData];
       [self.pieChartRight reloadData];
    });
}

Upvotes: 2

Related Questions