【问题标题】:How to animate the pie charts developed using core-plot library?如何为使用 core-plot 库开发的饼图制作动画?
【发布时间】:2011-03-09 05:27:32
【问题描述】:

在 iPhone 应用程序 Roambi 中,显示的饼图可以以动画方式随用户旋转,就像旋转圆盘一样。我们可以用它来做很多事情。

有人提到 Roambi 应用是使用 core-plot 库开发的:

What library use Roambi app iPhone to draw chart?

如何操作使用 Core plot 开发的饼图?

【问题讨论】:

  • 如果你只是在做饼图,你可能想看看 XYPieChart。它有动画并且看起来比 CorePlot 更好(在我看来)。

标签: iphone core-plot


【解决方案1】:

快速浏览一下 Core Plot 源代码会发现 CPPieChart 的继承如下所示:

CPPieChart:CPPlot:CPAnnotationHostLayer:CPLayer:CALayer

所以你可以看到最后,CPPieChart 只是一个严重的子类CALayer。我在这里可能完全不正确,但没有任何迹象表明它不能像其他任何CALayer 一样被动画化。尝试以下代码将图层旋转 360 度:

CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D transform = CATransform3DMakeRotation(DegreesToRadians(360), 0, 0, 1);
rotation.toValue = [NSValue valueWithCATransform3D:transform];
rotation.duration = 10.0f;
[pieChart addAnimation:rotation forKey:@"rotation"];

如果您能做到这一点,那么只需从加速度计读取值并将其转换为旋转角度即可。

【讨论】:

    【解决方案2】:

    对于一个简单的饼图,即使有上述动画,Core Plot 也不是必需的。如果您只想要 Core Plot 解决方案,请忽略此答案。但是,这里有一个简单的解决方案,不需要外部包甚至任何框架。

    创建一个名为 PieChartView 的 UIView。在 PieChartView.h 中:

    #import <UIKit/UIKit.h>
    
    @interface PieChartView : UIView {
        float zeroAngle;
        NSMutableArray *valueArray;
        NSMutableArray *colorArray;
    }
    @property (nonatomic) float zeroAngle;
    @property (nonatomic, retain) NSMutableArray *valueArray;
    @property (nonatomic, retain) NSMutableArray *colorArray;
    @end
    

    然后在 PieChartView.m 中实现:

    #import "PieChartView.h"
    
    @implementation PieChartView
    @synthesize zeroAngle;
    @synthesize valueArray, colorArray;
    
    - (id)initWithFrame:(CGRect)frame {
        if ((self = [super initWithFrame:frame])) {
        }
        return self;
    }
    
    
    - (void)drawRect:(CGRect)rect {
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        // DETERMINE NUMBER OF WEDGES OF PIE
        int wedges = [valueArray count];
        if (wedges > [colorArray count]) {
            NSLog(@"INSUFFICENT COLORS FOR PIE CHART: Please add %d additional colors.",wedges-[colorArray count]);
            for (int i=[colorArray count] ; i<wedges ; ++i) {
                [colorArray addObject:[UIColor whiteColor]];
            }
        }
    
        // DETERMINE TOTAL VOLUME OF PIE
        float sum = 0.0;
        for (int i=0; i<wedges ; ++i) {
            sum += [[valueArray objectAtIndex:i] floatValue];
        }
        float frac = 2.0*M_PI/sum;
    
        // DETERMINE CENTER AND MAXIMUM RADIUS OF INSCRIBED CIRCLE
        int center_x = rect.size.width/2.0;
        int center_y = rect.size.height/2.0;
        int radius = (center_x > center_y ? center_x : center_y);
    
        // DRAW WEDGES CLOCKWISE FROM POSITIVE X-AXIS
        float startAngle=zeroAngle;
        float endAngle=zeroAngle;
        for (int i=0; i<wedges; ++i) {
            startAngle = endAngle;
            endAngle += [[valueArray objectAtIndex:i] floatValue]*frac;
            [[colorArray objectAtIndex:i] setFill];
            CGContextMoveToPoint(context, center_x, center_y);
            CGContextAddArc(context, center_x, center_y, radius, startAngle, endAngle, 0);
            CGContextClosePath(context);
            CGContextFillPath(context);
        }
    }
    
    - (void)dealloc {
        [valueArray release];
        [colorArray release];
        [super dealloc];
    }
    @end
    

    现在您可以以任何您希望实现所需动画的方式将浮动 zeroAngle 绑定到加速度计。

    【讨论】:

      【解决方案3】:

      我不知道CorePlot,在不久的将来它可能对我有用,所以我只是看了一下。 CorePlot 很有趣并且是活跃的。

      关于 CorePlot 的动画:

      1. CorePlot 文档提到了 Animation system,但它还没有实现...
      2. 即使基于Core Animation,也并不意味着CorePlot 图形可以轻松制作动画。

      Interesting reading on the topic on google groups.

      以下是您的选择(按我的偏好顺序):

      1. 寻找另一个像 Roambi 一样处理动画的绘图库。
      2. 基于 CorePlot 实现您的动画(意味着对该框架和Core Animation 有很好的理解)
      3. 自己做所有事情,这取决于你想要什么,再次,可能没那么难......但是Core Animation需要技能,再次。

      我怀疑 Roambi 是否使用 CorePlot,但如果他们这样做了,他们会将其用作具有 TONS 自定义内部代码的基础...

      【讨论】:

      • @PengOne 感谢编辑我糟糕的语法,英语显然不是我的主要语言;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多