हमारे पास 6 वर्ग
CircleViewController.h, CircleViewController.m, GraphView.h, GraphView.h, CirclePart.h, CirclePart.m
CirclePart.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CirclePart : NSObject
@property (nonatomic) CGFloat startDegree;
@property (nonatomic) CGFloat endDegree;
@property (nonatomic) UIColor *partColor;
-(id)initWithStartDegree:(CGFloat)startDegree endDegree:(CGFloat)endDegree partColor:(UIColor*)partColor;
@end
CirclePart.m
#import "CirclePart.h"
@implementation CirclePart
-(id)initWithStartDegree:(CGFloat)startDegree endDegree:(CGFloat)endDegree partColor:(UIColor*)partColor
{
self = [super init];
if (self) {
self.startDegree = startDegree;
self.endDegree = endDegree;
self.partColor = partColor;
}
return self;
}
@end
GraphView.h
#import <UIKit/UIKit.h>
#import "CirclePart.h"
@interface GraphView : UIView
@property (nonatomic) CGPoint centrePoint;
@property (nonatomic) CGFloat radius;
@property (nonatomic) CGFloat lineWidth;
@property (nonatomic, strong) NSArray *circleParts;
-(id)initWithFrame:(CGRect)frame CentrePoint:(CGPoint)centrePoint radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth circleParts:(NSArray*)circleParts;
@end
GraphView.m
#import "GraphView.h"
@implementation GraphView
-(id)initWithFrame:(CGRect)frame CentrePoint:(CGPoint)centrePoint radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth circleParts:(NSArray*)circleParts
{
self = [super initWithFrame:frame];
if (self) {
self.centrePoint = centrePoint;
self.radius = radius;
self.lineWidth = lineWidth;
self.circleParts = circleParts;
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self drawCircle];
}
- (void)drawCircle {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, _lineWidth);
for(CirclePart *circlePart in _circleParts)
{
CGContextMoveToPoint(context, _centrePoint.x, _centrePoint.y);
CGContextAddArc(context, _centrePoint.x , _centrePoint.y, _radius, [self radians:circlePart.startDegree], [self radians:circlePart.endDegree], 0);
CGContextSetFillColorWithColor(context, circlePart.partColor.CGColor);
CGContextFillPath(context);
}
}
-(float) radians:(double) degrees {
return degrees * M_PI/180;
}
@end
CircleViewController.h
#import <UIKit/UIKit.h>
@interface CircleViewController : UIViewController
@end
CircleViewController.m
#import "CircleViewController.h"
#import "GraphView.h"
#import "CirclePart.h"
@interface CircleViewController()
@end
@implementation CircleViewController
- (void)viewDidLoad {
[super viewDidLoad];
CirclePart *part1 = [[CirclePart alloc] initWithStartDegree:0 endDegree:120 partColor:[UIColor redColor]];
CirclePart *part2 = [[CirclePart alloc] initWithStartDegree:120 endDegree:250 partColor:[UIColor blueColor]];
CirclePart *part3 = [[CirclePart alloc] initWithStartDegree:250 endDegree:360 partColor:[UIColor grayColor]];
NSArray *circleParts = [[NSArray alloc] initWithObjects:part1, part2, part3, nil];
CGRect rect = CGRectMake(100, 100, 200, 200);
CGPoint circleCenter = CGPointMake(rect.size.width/2, rect.size.height/2);
GraphView *graphView = [[GraphView alloc] initWithFrame:rect CentrePoint:circleCenter radius:80 lineWidth:2 circleParts:circleParts];
graphView.backgroundColor = [UIColor whiteColor];
graphView.layer.borderColor = [UIColor redColor].CGColor;
graphView.layer.borderWidth = 1.0f;
[self.view addSubview:graphView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
एक पाई चार्ट की तरह लग रहा। इसके लिए [कोर प्लॉट] (http://www.raywenderlich.com/13269/how-to-draw-graphs-with-core-plot-part-1) का उपयोग क्यों न करें? – Amar
लोगों को आपके कार्यान्वयन के लिए उचित उत्तर देने के लिए, आप हमें बता सकते हैं कि क्या आप कोरग्राफिक्स फ़ंक्शंस (उन कार्यों को जो 'CGContext' से शुरू करते हैं) का उपयोग अपने' ड्रारेक्ट 'में करते हैं, या' UIBezierPath' 'स्ट्रोक' विधि, या 'कैशपलेयर' का उपयोग करना। – Rob
@Rob वास्तव में मैं कोर ग्राफिक्स कार्यों जैसे CGContext का उपयोग कर रहा हूं। – sujat