diff --git a/Pie Chart Library/DLPieChart.h b/Pie Chart Library/DLPieChart.h index ac1e0ea..825c8bf 100755 --- a/Pie Chart Library/DLPieChart.h +++ b/Pie Chart Library/DLPieChart.h @@ -9,21 +9,27 @@ #import @class DLPieChart; + @protocol DLPieChartDataSource + @required - (NSUInteger)numberOfSlicesInPieChart:(DLPieChart *)pieChart; - (CGFloat)pieChart:(DLPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index; + @optional - (UIColor *)pieChart:(DLPieChart *)pieChart colorForSliceAtIndex:(NSUInteger)index; - (NSString *)pieChart:(DLPieChart *)pieChart textForSliceAtIndex:(NSUInteger)index; + @end @protocol DLPieChartDelegate + @optional - (void)pieChart:(DLPieChart *)pieChart willSelectSliceAtIndex:(NSUInteger)index; - (void)pieChart:(DLPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index; - (void)pieChart:(DLPieChart *)pieChart willDeselectSliceAtIndex:(NSUInteger)index; - (void)pieChart:(DLPieChart *)pieChart didDeselectSliceAtIndex:(NSUInteger)index; + @end @interface DLPieChart : UIView @@ -42,10 +48,13 @@ @property(nonatomic, assign) CGFloat selectedSliceStroke; @property(nonatomic, assign) CGFloat selectedSliceOffsetRadius; @property(nonatomic, assign) BOOL showPercentage; +@property (nonatomic ,retain) NSMutableArray *DLDataArray; +@property (nonatomic, retain) NSMutableArray *DLColorsArray; +@property (nonatomic, retain) DLPieChart *DLPieChartView; + - (id)initWithFrame:(CGRect)frame Center:(CGPoint)center Radius:(CGFloat)radius; - (void)reloadData; - (void)setPieBackgroundColor:(UIColor *)color; - - (void)setSliceSelectedAtIndex:(NSInteger)index; - (void)setSliceDeselectedAtIndex:(NSInteger)index; @@ -59,8 +68,4 @@ -(void)drawLegends:(DLPieChart *)layerHostingView dataArray:(NSMutableArray*)dataArray; -@property (nonatomic ,retain) NSMutableArray *DLDataArray; -@property (nonatomic, retain) NSMutableArray *DLColorsArray; -@property (nonatomic, retain) DLPieChart *DLPieChartView; - @end; diff --git a/Pie Chart Library/DLPieChart.m b/Pie Chart Library/DLPieChart.m index 4d6e6ff..5c4295a 100755 --- a/Pie Chart Library/DLPieChart.m +++ b/Pie Chart Library/DLPieChart.m @@ -12,36 +12,41 @@ #define OFFSET 20 @interface SliceLayer : CAShapeLayer + @property (nonatomic, assign) CGFloat value; @property (nonatomic, assign) CGFloat percentage; @property (nonatomic, assign) double startAngle; @property (nonatomic, assign) double endAngle; @property (nonatomic, assign) BOOL isSelected; @property (nonatomic, strong) NSString *text; + - (void)createArcAnimationForKey:(NSString *)key fromValue:(NSNumber *)from toValue:(NSNumber *)to Delegate:(id)delegate; + @end @implementation SliceLayer -@synthesize text = _text; -@synthesize value = _value; -@synthesize percentage = _percentage; -@synthesize startAngle = _startAngle; -@synthesize endAngle = _endAngle; -@synthesize isSelected = _isSelected; + +//@synthesize text = _text; +//@synthesize value = _value; +//@synthesize percentage = _percentage; +//@synthesize startAngle = _startAngle; +//@synthesize endAngle = _endAngle; +//@synthesize isSelected = _isSelected; - (NSString*)description { return [NSString stringWithFormat:@"value:%f, percentage:%0.0f, start:%f, end:%f", _value, _percentage, _startAngle/M_PI*180, _endAngle/M_PI*180]; } + + (BOOL)needsDisplayForKey:(NSString *)key { if ([key isEqualToString:@"startAngle"] || [key isEqualToString:@"endAngle"]) { return YES; } - else { - return [super needsDisplayForKey:key]; - } + + return [super needsDisplayForKey:key]; } + - (id)initWithLayer:(id)layer { if (self = [super initWithLayer:layer]) @@ -53,6 +58,7 @@ - (id)initWithLayer:(id)layer } return self; } + - (void)createArcAnimationForKey:(NSString *)key fromValue:(NSNumber *)from toValue:(NSNumber *)to Delegate:(id)delegate { CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key]; @@ -65,14 +71,17 @@ - (void)createArcAnimationForKey:(NSString *)key fromValue:(NSNumber *)from toVa [self addAnimation:arcAnimation forKey:key]; [self setValue:to forKey:key]; } + @end @interface DLPieChart (Private) + - (void)updateTimerFired:(NSTimer *)timer; - (SliceLayer *)createSliceLayer; - (CGSize)sizeThatFitsString:(NSString *)string; - (void)updateLabelForLayer:(SliceLayer *)pieLayer value:(CGFloat)value; - (void)notifyDelegateOfSelectionChangeFrom:(NSUInteger)previousSelection to:(NSUInteger)newSelection; + @end @implementation DLPieChart @@ -88,21 +97,20 @@ @implementation DLPieChart static NSUInteger kDefaultSliceZOrder = 100; -@synthesize dataSource = _dataSource; -@synthesize delegate = _delegate; -@synthesize startPieAngle = _startPieAngle; -@synthesize animationSpeed = _animationSpeed; -@synthesize pieCenter = _pieCenter; -@synthesize pieRadius = _pieRadius; -@synthesize showLabel = _showLabel; -@synthesize labelFont = _labelFont; -@synthesize labelColor = _labelColor; -@synthesize labelShadowColor = _labelShadowColor; -@synthesize labelRadius = _labelRadius; -@synthesize selectedSliceStroke = _selectedSliceStroke; -@synthesize selectedSliceOffsetRadius = _selectedSliceOffsetRadius; -@synthesize showPercentage = _showPercentage; - +//@synthesize dataSource = _dataSource; +//@synthesize delegate = _delegate; +//@synthesize startPieAngle = _startPieAngle; +//@synthesize animationSpeed = _animationSpeed; +//@synthesize pieCenter = _pieCenter; +//@synthesize pieRadius = _pieRadius; +//@synthesize showLabel = _showLabel; +//@synthesize labelFont = _labelFont; +//@synthesize labelColor = _labelColor; +//@synthesize labelShadowColor = _labelShadowColor; +//@synthesize labelRadius = _labelRadius; +//@synthesize selectedSliceStroke = _selectedSliceStroke; +//@synthesize selectedSliceOffsetRadius = _selectedSliceOffsetRadius; +//@synthesize showPercentage = _showPercentage; @synthesize DLDataArray, DLColorsArray,DLPieChartView; static CGPathRef CGPathCreateArc(CGPoint center, CGFloat radius, CGFloat startAngle, CGFloat endAngle) @@ -223,7 +231,19 @@ - (void)setShowPercentage:(BOOL)showPercentage label = [NSString stringWithFormat:@"%0.0f", layer.percentage*100]; else label = (layer.text)?layer.text:[NSString stringWithFormat:@"%0.0f", layer.value]; - CGSize size = [label sizeWithFont:self.labelFont]; + + //CGSize size = [label sizeWithFont:self.labelFont]; + CGSize size; + + if(getIOSVersion() >= 7) { + size = [label sizeWithAttributes:@{NSFontAttributeName:self.labelFont}]; + } + else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + size = [label sizeWithFont:self.labelFont]; +#pragma clang diagnostic pop + } if(M_PI*2*_labelRadius*layer.percentage < MAX(size.width,size.height)) { @@ -636,7 +656,20 @@ - (SliceLayer *)createSliceLayer [textLayer setShadowOpacity:1.0f]; [textLayer setShadowRadius:2.0f]; } - CGSize size = [@"0" sizeWithFont:self.labelFont]; + + //CGSize size = [@"0" sizeWithFont:self.labelFont]; + CGSize size; + + if(getIOSVersion() >= 7) { + size = [@"0" sizeWithAttributes:@{NSFontAttributeName:self.labelFont}]; + } + else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + size = [@"0" sizeWithFont:self.labelFont]; +#pragma clang diagnostic pop + } + [CATransaction setDisableActions:YES]; [textLayer setFrame:CGRectMake(0, 0, size.width, size.height)]; [textLayer setPosition:CGPointMake(_pieCenter.x + (_labelRadius * cos(0)), _pieCenter.y + (_labelRadius * sin(0)))]; @@ -656,7 +689,18 @@ - (void)updateLabelForLayer:(SliceLayer *)pieLayer value:(CGFloat)value else label = (pieLayer.text)?pieLayer.text:[NSString stringWithFormat:@"%0.0f", value]; - CGSize size = [label sizeWithFont:self.labelFont]; + //CGSize size = [label sizeWithFont:self.labelFont]; + CGSize size; + + if(getIOSVersion() >= 7) { + size = [label sizeWithAttributes:@{NSFontAttributeName:self.labelFont}]; + } + else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + size = [label sizeWithFont:self.labelFont]; +#pragma clang diagnostic pop + } [CATransaction setDisableActions:YES]; if(M_PI*2*_labelRadius*pieLayer.percentage < MAX(size.width,size.height) || value <= 0) diff --git a/PieChart.xcodeproj/project.xcworkspace/xcshareddata/PieChart.xccheckout b/PieChart.xcodeproj/project.xcworkspace/xcshareddata/PieChart.xccheckout new file mode 100644 index 0000000..4d53026 --- /dev/null +++ b/PieChart.xcodeproj/project.xcworkspace/xcshareddata/PieChart.xccheckout @@ -0,0 +1,41 @@ + + + + + IDESourceControlProjectFavoriteDictionaryKey + + IDESourceControlProjectIdentifier + BB8715F0-94F3-463B-9A21-DA59281C7A3E + IDESourceControlProjectName + PieChart + IDESourceControlProjectOriginsDictionary + + EABBA475-0CD2-4703-8D75-571C423FC6E9 + ssh://github.com/lduraes/piechart.git + + IDESourceControlProjectPath + PieChart.xcodeproj/project.xcworkspace + IDESourceControlProjectRelativeInstallPathDictionary + + EABBA475-0CD2-4703-8D75-571C423FC6E9 + ../.. + + IDESourceControlProjectURL + ssh://github.com/lduraes/piechart.git + IDESourceControlProjectVersion + 110 + IDESourceControlProjectWCCIdentifier + EABBA475-0CD2-4703-8D75-571C423FC6E9 + IDESourceControlProjectWCConfigurations + + + IDESourceControlRepositoryExtensionIdentifierKey + public.vcs.git + IDESourceControlWCCIdentifierKey + EABBA475-0CD2-4703-8D75-571C423FC6E9 + IDESourceControlWCCName + piechart + + + + diff --git a/PieChart.xcodeproj/project.xcworkspace/xcuserdata/lduraes.xcuserdatad/UserInterfaceState.xcuserstate b/PieChart.xcodeproj/project.xcworkspace/xcuserdata/lduraes.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..fdf889a Binary files /dev/null and b/PieChart.xcodeproj/project.xcworkspace/xcuserdata/lduraes.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/PieChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/PieChart.xcscheme b/PieChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/PieChart.xcscheme new file mode 100644 index 0000000..aa4fcaa --- /dev/null +++ b/PieChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/PieChart.xcscheme @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PieChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/xcschememanagement.plist b/PieChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..d7c4571 --- /dev/null +++ b/PieChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,27 @@ + + + + + SchemeUserState + + PieChart.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 9C82E1221886CB4A00EACB7D + + primary + + + 9C82E1431886CB4A00EACB7D + + primary + + + + + diff --git a/PieChart/PieChart-Prefix.pch b/PieChart/PieChart-Prefix.pch index 82a2bb4..dc27559 100644 --- a/PieChart/PieChart-Prefix.pch +++ b/PieChart/PieChart-Prefix.pch @@ -14,3 +14,5 @@ #import #import #endif + +#define getIOSVersion() [[UIDevice currentDevice].systemVersion floatValue] diff --git a/PieChart/ViewController.m b/PieChart/ViewController.m index 97483ea..19c6031 100644 --- a/PieChart/ViewController.m +++ b/PieChart/ViewController.m @@ -8,18 +8,25 @@ #import "ViewController.h" - @interface ViewController () @end @implementation ViewController +#pragma mark - DLPieChartDelegate methods + +-(void)pieChart:(DLPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index { + int value = [[self.pieChart.DLDataArray objectAtIndex:index] integerValue]; + + NSLog(@"Changed value >> %i",value); +} + +#pragma mark - Lifecycle methods + - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. - NSMutableArray * arrayChart = [NSMutableArray new]; NSMutableArray * arrayColors = [NSMutableArray new]; @@ -44,21 +51,6 @@ - (void)viewDidLoad self.pieChart.layer.borderWidth = 2; self.pieChart.layer.cornerRadius = 10; self.pieChart.delegate = self; - -} - -- (void)didReceiveMemoryWarning -{ - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - -- (void)pieChart:(DLPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index { - int value = [[self.pieChart.DLDataArray objectAtIndex:index] integerValue]; - - NSLog(@"Tocou valor %i", value); - - } @end