How do I animate the appearance of a bar chart in Core Plot?
Asked Answered
K

5

6

When first displaying a bar chart using Core Plot, I'd like the bars to grow upward until they reach their correct heights.

How would you create such an animation using this framework?

Katherinakatherine answered 5/4, 2011 at 13:20 Comment(0)
P
11

This is what I did:

  1. In my viewDidLoad controller's method I created the CPTXYGraph:

    graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    
  2. I added animation to the empty graph:

    CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
  3. Then in animationDidStop delegate's method I added the plot data and animation to the graph:

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    [anim setDuration:2.0f];
    
    anim.toValue = [NSNumber numberWithFloat:1.0f];
    anim.fromValue = [NSNumber numberWithFloat:0.0f];
    anim.removedOnCompletion = NO;
    anim.delegate = self;
    anim.fillMode = kCAFillModeForwards;
    
    gPlot.anchorPoint = CGPointMake(0.0, 0.0);
    
    [gPlot addAnimation:anim forKey:@"grow"];
    
    [graph addPlot:gPlot ];// IMPORTANT here I added the plot data to the graph :) .
    
Planarian answered 20/7, 2012 at 21:30 Comment(0)
C
3

Add animation for your CPTBarPlot as follows:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
[animation setDuration:1];
CATransform3D transform = CATransform3DMakeScale(1, 0.0001, 1);
// offsetY=[PlotDisplayAreaUnderXAxisHeight]-[PlotDisplayAreaHeight]/2
transform = CATransform3DConcat(transform, CATransform3DMakeTranslation(0, offsetY, 0));
animation.fromValue = [NSValue valueWithCATransform3D:transform];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
[barPlot addAnimation:animation forKey:@"barGrowth"];
Carven answered 17/12, 2012 at 12:20 Comment(0)
S
2

Thanks Michele for your great answer.

I have uploaded an example from her answer to Github for those who would want to download the demo and start working right away!

https://github.com/mayuur/CoreplotAnimation

Enjoy Coding!!!

Seeker answered 21/1, 2013 at 18:48 Comment(0)
P
1

This is very similar to: How to animate the pie charts developed using core-plot library?

Perpend answered 5/4, 2011 at 18:3 Comment(0)
U
0

Add opacity animation like this...

CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[xSquaredPlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];
Ulpian answered 10/9, 2014 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.