2011-01-22 9 views
0

私のカテゴリーの円グラフをパーセンテージで表示したいと思います。iPhoneで円グラフを作成する方法は?

どのように私は、カテゴリの割合の円グラフを作成することができますか?

+0

http://stackoverflow.com/質問/ 13231244 /描画円グラフ - ios-quartz-2d/30004287#30004287円グラフを描画する単一行のコードが含まれています – iYoung

答えて

3

このことを行うことができるというAPIがあります。

  1. MIMChart
  2. CorePlot効果的ではなく、便利なライブラリ。
1

これは、他の誰かのコードのように見える場合、私はウェブサイトでそれを行う方法を働いていたので、それはだと警告し、私は長くないiPhoneを起動した後にこれをした追加の警告と役に立つかもしれません。どのビットが非効率的か間違っているかを教えてくれる人は歓迎します、私はまだ学んでいます。

static inline float radians(double degrees) { return degrees * M_PI/180; } 

// making a simple pac man shape 
- (UIImage*)getImage { 

    UIImage* image; 
    if(self.completion == 100.0f) { 
     image = [UIImage imageNamed:@"completedTaskIcon.png"]; 
    } else { 
     UIGraphicsBeginImageContext(CGSizeMake(SIDELENGTH, SIDELENGTH)); 

     CGContextRef context = UIGraphicsGetCurrentContext(); 

     // the image should have a clear background 
     [[UIColor clearColor] set]; 
     CGRect myRect = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH); 
     UIRectFill(myRect); 

     // color was hopefully set before this method called 
     [self.color set]; 

     // centre point is required 
     CGFloat midx = SIDELENGTH/2; 
     CGFloat midy = SIDELENGTH/2; 
     // radius of the arc 
     CGFloat radius = (SIDELENGTH/2) * 0.60f; 

     // pie background 
     CGContextSetFillColor(context, CGColorGetComponents([[UIColor orangeColor] CGColor])); 
     CGContextBeginPath(context); 
     CGContextMoveToPoint(context, midx + radius, midy); 
     CGContextAddArc(context, midx, midy, radius, radians(0), radians(360), 0); 
     CGContextFillPath(context); 

     // pie segment 
     CGContextSetFillColor(context, CGColorGetComponents([[UIColor blueColor] CGColor])); 
     CGContextBeginPath(context); 
     CGContextMoveToPoint(context, midx, midy); 
     CGContextAddLineToPoint(context, midx + radius, midy); 
     CGContextAddArc(context, midx, midy, radius, radians(0), radians(360 * (self.completion/100.0f)), 0); 
     CGContextFillPath(context); 
     image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

    return image; 
} 
0

他の回答に提案されているライブラリのほかに、私が使った2つの本当に良いオープンソースのパイチャートクラスがあります。彼らは、非常にシンプルなのGitHubでそれらを見て、とても素敵に見える:

あなたが私の答えを参照することができます
関連する問題