Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reference main XYPieChart files in demo instead of keeping a copy.

  • Loading branch information...
commit a0dbad3f651f117f5071a4ac782582a486062cc6 1 parent 28d8787
@nschum nschum authored
View
12 Demo/XYPieChart.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 2424BF2C1607437C0000B539 /* XYPieChart.m in Sources */ = {isa = PBXBuildFile; fileRef = 2424BF2B1607437C0000B539 /* XYPieChart.m */; };
8768825C1501D2A60097BE69 /* icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 8768825B1501D2A60097BE69 /* icon.png */; };
87FEAA1C14F84E3000ED38A2 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 87FEAA1B14F84E3000ED38A2 /* UIKit.framework */; };
87FEAA1E14F84E3000ED38A2 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 87FEAA1D14F84E3000ED38A2 /* Foundation.framework */; };
@@ -16,11 +17,12 @@
87FEAA2C14F84E3100ED38A2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 87FEAA2B14F84E3100ED38A2 /* AppDelegate.m */; };
87FEAA2F14F84E3100ED38A2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 87FEAA2E14F84E3100ED38A2 /* ViewController.m */; };
87FEAA3214F84E3100ED38A2 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 87FEAA3014F84E3100ED38A2 /* ViewController.xib */; };
- 87FEAA3A14F850D000ED38A2 /* XYPieChart.m in Sources */ = {isa = PBXBuildFile; fileRef = 87FEAA3914F850D000ED38A2 /* XYPieChart.m */; };
87FEAA3C14FAC66D00ED38A2 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 87FEAA3B14FAC66D00ED38A2 /* QuartzCore.framework */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 2424BF2A1607437C0000B539 /* XYPieChart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XYPieChart.h; path = ../../XYPieChart/XYPieChart.h; sourceTree = "<group>"; };
+ 2424BF2B1607437C0000B539 /* XYPieChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XYPieChart.m; path = ../../XYPieChart/XYPieChart.m; sourceTree = "<group>"; };
8768825B1501D2A60097BE69 /* icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = icon.png; sourceTree = "<group>"; };
87FEAA1714F84E3000ED38A2 /* XYPieChart.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = XYPieChart.app; sourceTree = BUILT_PRODUCTS_DIR; };
87FEAA1B14F84E3000ED38A2 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
@@ -35,8 +37,6 @@
87FEAA2D14F84E3100ED38A2 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
87FEAA2E14F84E3100ED38A2 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
87FEAA3114F84E3100ED38A2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = "<group>"; };
- 87FEAA3814F850D000ED38A2 /* XYPieChart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XYPieChart.h; sourceTree = "<group>"; };
- 87FEAA3914F850D000ED38A2 /* XYPieChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XYPieChart.m; sourceTree = "<group>"; };
87FEAA3B14FAC66D00ED38A2 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */
@@ -87,8 +87,8 @@
87FEAA2114F84E3000ED38A2 /* XYPieChart */ = {
isa = PBXGroup;
children = (
- 87FEAA3814F850D000ED38A2 /* XYPieChart.h */,
- 87FEAA3914F850D000ED38A2 /* XYPieChart.m */,
+ 2424BF2A1607437C0000B539 /* XYPieChart.h */,
+ 2424BF2B1607437C0000B539 /* XYPieChart.m */,
87FEAA2D14F84E3100ED38A2 /* ViewController.h */,
87FEAA2E14F84E3100ED38A2 /* ViewController.m */,
87FEAA3014F84E3100ED38A2 /* ViewController.xib */,
@@ -177,7 +177,7 @@
87FEAA2814F84E3100ED38A2 /* main.m in Sources */,
87FEAA2C14F84E3100ED38A2 /* AppDelegate.m in Sources */,
87FEAA2F14F84E3100ED38A2 /* ViewController.m in Sources */,
- 87FEAA3A14F850D000ED38A2 /* XYPieChart.m in Sources */,
+ 2424BF2C1607437C0000B539 /* XYPieChart.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
67 Demo/XYPieChart/XYPieChart.h
@@ -1,67 +0,0 @@
-//
-// XYPieChart.h
-// XYPieChart
-//
-// Created by XY Feng on 2/24/12.
-// Copyright (c) 2012 Xiaoyang Feng. All rights reserved.
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-
-#import <UIKit/UIKit.h>
-
-@class XYPieChart;
-@protocol XYPieChartDataSource <NSObject>
-@required
-- (NSUInteger)numberOfSlicesInPieChart:(XYPieChart *)pieChart;
-- (CGFloat)pieChart:(XYPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index;
-@optional
-- (UIColor *)pieChart:(XYPieChart *)pieChart colorForSliceAtIndex:(NSUInteger)index;
-- (NSString *)pieChart:(XYPieChart *)pieChart textForSliceAtIndex:(NSUInteger)index;
-@end
-
-@protocol XYPieChartDelegate <NSObject>
-@optional
-- (void)pieChart:(XYPieChart *)pieChart willSelectSliceAtIndex:(NSUInteger)index;
-- (void)pieChart:(XYPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index;
-- (void)pieChart:(XYPieChart *)pieChart willDeselectSliceAtIndex:(NSUInteger)index;
-- (void)pieChart:(XYPieChart *)pieChart didDeselectSliceAtIndex:(NSUInteger)index;
-@end
-
-@interface XYPieChart : UIView
-@property(nonatomic, weak) id<XYPieChartDataSource> dataSource;
-@property(nonatomic, weak) id<XYPieChartDelegate> delegate;
-@property(nonatomic, assign) CGFloat startPieAngle;
-@property(nonatomic, assign) CGFloat animationSpeed;
-@property(nonatomic, assign) CGPoint pieCenter;
-@property(nonatomic, assign) CGFloat pieRadius;
-@property(nonatomic, assign) BOOL showLabel;
-@property(nonatomic, strong) UIFont *labelFont;
-@property(nonatomic, strong) UIColor *labelColor;
-@property(nonatomic, strong) UIColor *labelShadowColor;
-@property(nonatomic, assign) CGFloat labelRadius;
-@property(nonatomic, assign) CGFloat selectedSliceStroke;
-@property(nonatomic, assign) CGFloat selectedSliceOffsetRadius;
-@property(nonatomic, assign) BOOL showPercentage;
-- (id)initWithFrame:(CGRect)frame Center:(CGPoint)center Radius:(CGFloat)radius;
-- (void)reloadData;
-- (void)setPieBackgroundColor:(UIColor *)color;
-@end;
View
675 Demo/XYPieChart/XYPieChart.m
@@ -1,675 +0,0 @@
-//
-// XYPieChart.m
-// XYPieChart
-//
-// Created by XY Feng on 2/24/12.
-// Copyright (c) 2012 Xiaoyang Feng. All rights reserved.
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-
-#import "XYPieChart.h"
-#import <QuartzCore/QuartzCore.h>
-
-@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;
-- (NSString*)description
-{
- return [NSString stringWithFormat:@"value:%f, percentage:%0.0f, start:%d, end:%d", _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];
- }
-}
-- (id)initWithLayer:(id)layer
-{
- if (self = [super initWithLayer:layer])
- {
- if ([layer isKindOfClass:[SliceLayer class]]) {
- self.startAngle = [(SliceLayer *)layer startAngle];
- self.endAngle = [(SliceLayer *)layer endAngle];
- }
- }
- return self;
-}
-- (void)createArcAnimationForKey:(NSString *)key fromValue:(NSNumber *)from toValue:(NSNumber *)to Delegate:(id)delegate
-{
- CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key];
- NSNumber *currentAngle = [[self presentationLayer] valueForKey:key];
- if(!currentAngle) currentAngle = from;
- [arcAnimation setFromValue:currentAngle];
- [arcAnimation setToValue:to];
- [arcAnimation setDelegate:delegate];
- [arcAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
- [self addAnimation:arcAnimation forKey:key];
- [self setValue:to forKey:key];
-}
-@end
-
-@interface XYPieChart (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 XYPieChart
-{
- NSInteger _selectedSliceIndex;
- //pie view, contains all slices
- UIView *_pieView;
-
- //animation control
- NSTimer *_animationTimer;
- NSMutableArray *_animations;
-}
-
-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;
-
-static CGPathRef CGPathCreateArc(CGPoint center, CGFloat radius, CGFloat startAngle, CGFloat endAngle)
-{
- CGMutablePathRef path = CGPathCreateMutable();
- CGPathMoveToPoint(path, NULL, center.x, center.y);
-
- CGPathAddArc(path, NULL, center.x, center.y, radius, startAngle, endAngle, 0);
- CGPathCloseSubpath(path);
-
- return path;
-}
-
-- (id)initWithFrame:(CGRect)frame
-{
- self = [super initWithFrame:frame];
- if (self)
- {
- self.backgroundColor = [UIColor clearColor];
- _pieView = [[UIView alloc] initWithFrame:frame];
- [_pieView setBackgroundColor:[UIColor clearColor]];
- [self addSubview:_pieView];
-
- _selectedSliceIndex = -1;
- _animations = [[NSMutableArray alloc] init];
-
- _animationSpeed = 0.5;
- _startPieAngle = M_PI_2*3;
- _selectedSliceStroke = 3.0;
-
- self.pieRadius = MIN(frame.size.width/2, frame.size.height/2) - 10;
- self.pieCenter = CGPointMake(frame.size.width/2, frame.size.height/2);
- self.labelFont = [UIFont boldSystemFontOfSize:MAX((int)self.pieRadius/10, 5)];
- _labelColor = [UIColor whiteColor];
- _labelRadius = _pieRadius/2;
- _selectedSliceOffsetRadius = MAX(10, _pieRadius/10);
-
- _showLabel = YES;
- _showPercentage = YES;
- }
- return self;
-}
-
-- (id)initWithFrame:(CGRect)frame Center:(CGPoint)center Radius:(CGFloat)radius
-{
- self = [self initWithFrame:frame];
- if (self)
- {
- self.pieCenter = center;
- self.pieRadius = radius;
- }
- return self;
-}
-
-- (id)initWithCoder:(NSCoder *)aDecoder
-{
- self = [super initWithCoder:aDecoder];
- if(self)
- {
- _pieView = [[UIView alloc] initWithFrame:self.bounds];
- [_pieView setBackgroundColor:[UIColor clearColor]];
- [self insertSubview:_pieView atIndex:0];
-
- _selectedSliceIndex = -1;
- _animations = [[NSMutableArray alloc] init];
-
- _animationSpeed = 0.5;
- _startPieAngle = M_PI_2*3;
- _selectedSliceStroke = 3.0;
-
- CGRect bounds = [[self layer] bounds];
- self.pieRadius = MIN(bounds.size.width/2, bounds.size.height/2) - 10;
- self.pieCenter = CGPointMake(bounds.size.width/2, bounds.size.height/2);
- self.labelFont = [UIFont boldSystemFontOfSize:MAX((int)self.pieRadius/10, 5)];
- _labelColor = [UIColor whiteColor];
- _labelRadius = _pieRadius/2;
- _selectedSliceOffsetRadius = MAX(10, _pieRadius/10);
-
- _showLabel = YES;
- _showPercentage = YES;
- }
- return self;
-}
-
-- (void)setPieCenter:(CGPoint)pieCenter
-{
- [_pieView setCenter:pieCenter];
- _pieCenter = CGPointMake(_pieView.frame.size.width/2, _pieView.frame.size.height/2);
-}
-
-- (void)setPieRadius:(CGFloat)pieRadius
-{
- _pieRadius = pieRadius;
- CGRect frame = CGRectMake(_pieCenter.x-pieRadius, _pieCenter.y-pieRadius, pieRadius*2, pieRadius*2);
- _pieCenter = CGPointMake(frame.size.width/2, frame.size.height/2);
- [_pieView setFrame:frame];
- [_pieView.layer setCornerRadius:_pieRadius];
-}
-
-- (void)setPieBackgroundColor:(UIColor *)color
-{
- [_pieView setBackgroundColor:color];
-}
-
-#pragma mark - manage settings
-
-- (void)setShowPercentage:(BOOL)showPercentage
-{
- _showPercentage = showPercentage;
- for(SliceLayer *layer in _pieView.layer.sublayers)
- {
- CATextLayer *textLayer = [[layer sublayers] objectAtIndex:0];
- [textLayer setHidden:!_showLabel];
- if(!_showLabel) return;
- NSString *label;
- if(_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];
-
- if(M_PI*2*_labelRadius*layer.percentage < MAX(size.width,size.height))
- {
- [textLayer setString:@""];
- }
- else
- {
- [textLayer setString:label];
- [textLayer setBounds:CGRectMake(0, 0, size.width, size.height)];
- }
- }
-}
-
-- (void)setSliceSelectedAtIndex:(NSInteger)index
-{
- if(_selectedSliceOffsetRadius <= 0)
- return;
- SliceLayer *layer = [_pieView.layer.sublayers objectAtIndex:index];
- if (layer) {
- CGPoint currPos = layer.position;
- double middleAngle = (layer.startAngle + layer.endAngle)/2.0;
- CGPoint newPos = CGPointMake(currPos.x + _selectedSliceOffsetRadius*cos(middleAngle), currPos.y + _selectedSliceOffsetRadius*sin(middleAngle));
- layer.position = newPos;
- layer.isSelected = YES;
- }
-}
-
-- (void)setSliceDeselectedAtIndex:(NSInteger)index
-{
- if(_selectedSliceOffsetRadius <= 0)
- return;
- SliceLayer *layer = [_pieView.layer.sublayers objectAtIndex:index];
- if (layer) {
- layer.position = CGPointMake(0, 0);
- layer.isSelected = NO;
- }
-}
-
-#pragma mark - Pie Reload Data With Animation
-
-- (void)reloadData
-{
- if (_dataSource && !_animationTimer)
- {
- CALayer *parentLayer = [_pieView layer];
- NSArray *slicelayers = [parentLayer sublayers];
-
- _selectedSliceIndex = -1;
- [slicelayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- SliceLayer *layer = (SliceLayer *)obj;
- if(layer.isSelected)
- [self setSliceDeselectedAtIndex:idx];
- }];
-
- double startToAngle = 0.0;
- double endToAngle = startToAngle;
-
- NSUInteger sliceCount = [_dataSource numberOfSlicesInPieChart:self];
-
- double sum = 0.0;
- double values[sliceCount];
- for (int index = 0; index < sliceCount; index++) {
- values[index] = [_dataSource pieChart:self valueForSliceAtIndex:index];
- sum += values[index];
- }
-
- double angles[sliceCount];
- for (int index = 0; index < sliceCount; index++) {
- double div;
- if (sum == 0)
- div = 0;
- else
- div = values[index] / sum;
- angles[index] = M_PI * 2 * div;
- }
-
- [CATransaction begin];
- [CATransaction setAnimationDuration:_animationSpeed];
-
- [_pieView setUserInteractionEnabled:NO];
-
- __block NSMutableArray *layersToRemove = nil;
- [CATransaction setCompletionBlock:^{
-
- [layersToRemove enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- [obj removeFromSuperlayer];
- }];
-
- [layersToRemove removeAllObjects];
-
- for(SliceLayer *layer in _pieView.layer.sublayers)
- {
- [layer setZPosition:kDefaultSliceZOrder];
- }
-
- [_pieView setUserInteractionEnabled:YES];
- }];
-
- BOOL isOnStart = ([slicelayers count] == 0 && sliceCount);
- NSInteger diff = sliceCount - [slicelayers count];
- layersToRemove = [NSMutableArray arrayWithArray:slicelayers];
-
- BOOL isOnEnd = ([slicelayers count] && (sliceCount == 0 || sum <= 0));
- if(isOnEnd)
- {
- for(SliceLayer *layer in _pieView.layer.sublayers){
- [self updateLabelForLayer:layer value:0];
- [layer createArcAnimationForKey:@"startAngle"
- fromValue:[NSNumber numberWithDouble:_startPieAngle]
- toValue:[NSNumber numberWithDouble:_startPieAngle]
- Delegate:self];
- [layer createArcAnimationForKey:@"endAngle"
- fromValue:[NSNumber numberWithDouble:_startPieAngle]
- toValue:[NSNumber numberWithDouble:_startPieAngle]
- Delegate:self];
- }
- [CATransaction commit];
- return;
- }
-
- for(int index = 0; index < sliceCount; index ++)
- {
- SliceLayer *layer;
- double angle = angles[index];
- endToAngle += angle;
- double startFromAngle = _startPieAngle + startToAngle;
- double endFromAngle = _startPieAngle + endToAngle;
-
- if( index >= [slicelayers count] )
- {
- layer = [self createSliceLayer];
- if (isOnStart)
- startFromAngle = endFromAngle = _startPieAngle;
- [parentLayer addSublayer:layer];
- diff--;
- }
- else
- {
- SliceLayer *onelayer = [slicelayers objectAtIndex:index];
- if(diff == 0 || onelayer.value == (CGFloat)values[index])
- {
- layer = onelayer;
- [layersToRemove removeObject:layer];
- }
- else if(diff > 0)
- {
- layer = [self createSliceLayer];
- [parentLayer insertSublayer:layer atIndex:index];
- diff--;
- }
- else if(diff < 0)
- {
- while(diff < 0)
- {
- [onelayer removeFromSuperlayer];
- [parentLayer addSublayer:onelayer];
- diff++;
- onelayer = [slicelayers objectAtIndex:index];
- if(onelayer.value == (CGFloat)values[index] || diff == 0)
- {
- layer = onelayer;
- [layersToRemove removeObject:layer];
- break;
- }
- }
- }
- }
-
- layer.value = values[index];
- layer.percentage = (sum)?layer.value/sum:0;
- UIColor *color = nil;
- if([_dataSource respondsToSelector:@selector(pieChart:colorForSliceAtIndex:)])
- {
- color = [_dataSource pieChart:self colorForSliceAtIndex:index];
- }
-
- if(!color)
- {
- color = [UIColor colorWithHue:((index/8)%20)/20.0+0.02 saturation:(index%8+3)/10.0 brightness:91/100.0 alpha:1];
- }
-
- [layer setFillColor:color.CGColor];
- if([_dataSource respondsToSelector:@selector(pieChart:textForSliceAtIndex:)])
- {
- layer.text = [_dataSource pieChart:self textForSliceAtIndex:index];
- }
-
- [self updateLabelForLayer:layer value:values[index]];
- [layer createArcAnimationForKey:@"startAngle"
- fromValue:[NSNumber numberWithDouble:startFromAngle]
- toValue:[NSNumber numberWithDouble:startToAngle+_startPieAngle]
- Delegate:self];
- [layer createArcAnimationForKey:@"endAngle"
- fromValue:[NSNumber numberWithDouble:endFromAngle]
- toValue:[NSNumber numberWithDouble:endToAngle+_startPieAngle]
- Delegate:self];
- startToAngle = endToAngle;
- }
- [CATransaction setDisableActions:YES];
- for(SliceLayer *layer in layersToRemove)
- {
- [layer setFillColor:[self backgroundColor].CGColor];
- [layer setDelegate:nil];
- [layer setZPosition:0];
- CATextLayer *textLayer = [[layer sublayers] objectAtIndex:0];
- [textLayer setHidden:YES];
- }
- [CATransaction setDisableActions:NO];
- [CATransaction commit];
- }
-}
-
-#pragma mark - Animation Delegate + Run Loop Timer
-
-- (void)updateTimerFired:(NSTimer *)timer;
-{
- CALayer *parentLayer = [_pieView layer];
- NSArray *pieLayers = [parentLayer sublayers];
-
- [pieLayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
-
- NSNumber *presentationLayerStartAngle = [[obj presentationLayer] valueForKey:@"startAngle"];
- CGFloat interpolatedStartAngle = [presentationLayerStartAngle doubleValue];
-
- NSNumber *presentationLayerEndAngle = [[obj presentationLayer] valueForKey:@"endAngle"];
- CGFloat interpolatedEndAngle = [presentationLayerEndAngle doubleValue];
-
- CGPathRef path = CGPathCreateArc(_pieCenter, _pieRadius, interpolatedStartAngle, interpolatedEndAngle);
- [obj setPath:path];
- CFRelease(path);
-
- {
- CALayer *labelLayer = [[obj sublayers] objectAtIndex:0];
- CGFloat interpolatedMidAngle = (interpolatedEndAngle + interpolatedStartAngle) / 2;
- [CATransaction setDisableActions:YES];
- [labelLayer setPosition:CGPointMake(_pieCenter.x + (_labelRadius * cos(interpolatedMidAngle)), _pieCenter.y + (_labelRadius * sin(interpolatedMidAngle)))];
- [CATransaction setDisableActions:NO];
- }
- }];
-}
-
-- (void)animationDidStart:(CAAnimation *)anim
-{
- if (_animationTimer == nil) {
- static float timeInterval = 1.0/60.0;
- _animationTimer= [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(updateTimerFired:) userInfo:nil repeats:YES];
- }
-
- [_animations addObject:anim];
-}
-
-- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)animationCompleted
-{
- [_animations removeObject:anim];
-
- if ([_animations count] == 0) {
- [_animationTimer invalidate];
- _animationTimer = nil;
- }
-}
-
-#pragma mark - Touch Handing (Selection Notification)
-
-- (NSInteger)getCurrentSelectedOnTouch:(CGPoint)point
-{
- __block NSUInteger selectedIndex = -1;
-
- CGAffineTransform transform = CGAffineTransformIdentity;
-
- CALayer *parentLayer = [_pieView layer];
- NSArray *pieLayers = [parentLayer sublayers];
-
- [pieLayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- SliceLayer *pieLayer = (SliceLayer *)obj;
- CGPathRef path = [pieLayer path];
-
- if (CGPathContainsPoint(path, &transform, point, 0)) {
- [pieLayer setLineWidth:_selectedSliceStroke];
- [pieLayer setStrokeColor:[UIColor whiteColor].CGColor];
- [pieLayer setLineJoin:kCALineJoinBevel];
- [pieLayer setZPosition:MAXFLOAT];
- selectedIndex = idx;
- } else {
- [pieLayer setZPosition:kDefaultSliceZOrder];
- [pieLayer setLineWidth:0.0];
- }
- }];
- return selectedIndex;
-}
-
-- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
-{
- [self touchesMoved:touches withEvent:event];
-}
-
-- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
-{
- UITouch *touch = [touches anyObject];
- CGPoint point = [touch locationInView:_pieView];
- [self getCurrentSelectedOnTouch:point];
-}
-
-- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
-{
- UITouch *touch = [touches anyObject];
- CGPoint point = [touch locationInView:_pieView];
- NSInteger selectedIndex = [self getCurrentSelectedOnTouch:point];
- [self notifyDelegateOfSelectionChangeFrom:_selectedSliceIndex to:selectedIndex];
- [self touchesCancelled:touches withEvent:event];
-}
-
-- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
-{
- CALayer *parentLayer = [_pieView layer];
- NSArray *pieLayers = [parentLayer sublayers];
-
- for (SliceLayer *pieLayer in pieLayers) {
- [pieLayer setZPosition:kDefaultSliceZOrder];
- [pieLayer setLineWidth:0.0];
- }
-}
-
-#pragma mark - Selection Notification
-
-- (void)notifyDelegateOfSelectionChangeFrom:(NSUInteger)previousSelection to:(NSUInteger)newSelection
-{
- if (previousSelection != newSelection)
- {
- if (previousSelection != -1 && [_delegate respondsToSelector:@selector(pieChart:willDeselectSliceAtIndex:)])
- {
- [_delegate pieChart:self willDeselectSliceAtIndex:previousSelection];
- }
-
- _selectedSliceIndex = newSelection;
-
- if (newSelection != -1)
- {
- if([_delegate respondsToSelector:@selector(pieChart:willSelectSliceAtIndex:)])
- [_delegate pieChart:self willSelectSliceAtIndex:newSelection];
- if(previousSelection != -1 && [_delegate respondsToSelector:@selector(pieChart:didDeselectSliceAtIndex:)])
- [_delegate pieChart:self didDeselectSliceAtIndex:previousSelection];
- if([_delegate respondsToSelector:@selector(pieChart:didSelectSliceAtIndex:)])
- [_delegate pieChart:self didSelectSliceAtIndex:newSelection];
- [self setSliceSelectedAtIndex:newSelection];
- }
-
- if(previousSelection != -1)
- {
- [self setSliceDeselectedAtIndex:previousSelection];
- if([_delegate respondsToSelector:@selector(pieChart:didDeselectSliceAtIndex:)])
- [_delegate pieChart:self didDeselectSliceAtIndex:previousSelection];
- }
- }
- else if (newSelection != -1)
- {
- SliceLayer *layer = [_pieView.layer.sublayers objectAtIndex:newSelection];
- if(_selectedSliceOffsetRadius > 0 && layer){
-
- if (layer.isSelected) {
- if ([_delegate respondsToSelector:@selector(pieChart:willDeselectSliceAtIndex:)])
- [_delegate pieChart:self willDeselectSliceAtIndex:newSelection];
- [self setSliceDeselectedAtIndex:newSelection];
- if (newSelection != -1 && [_delegate respondsToSelector:@selector(pieChart:didDeselectSliceAtIndex:)])
- [_delegate pieChart:self didDeselectSliceAtIndex:newSelection];
- }
- else {
- if ([_delegate respondsToSelector:@selector(pieChart:willSelectSliceAtIndex:)])
- [_delegate pieChart:self willSelectSliceAtIndex:newSelection];
- [self setSliceSelectedAtIndex:newSelection];
- if (newSelection != -1 && [_delegate respondsToSelector:@selector(pieChart:didSelectSliceAtIndex:)])
- [_delegate pieChart:self didSelectSliceAtIndex:newSelection];
- }
- }
- }
-}
-
-#pragma mark - Pie Layer Creation Method
-
-- (SliceLayer *)createSliceLayer
-{
- SliceLayer *pieLayer = [SliceLayer layer];
- [pieLayer setZPosition:0];
- [pieLayer setStrokeColor:NULL];
- CATextLayer *textLayer = [CATextLayer layer];
- textLayer.contentsScale = [[UIScreen mainScreen] scale];
- CGFontRef font = CGFontCreateWithFontName((__bridge CFStringRef)[self.labelFont fontName]);
- [textLayer setFont:font];
- CFRelease(font);
- [textLayer setFontSize:self.labelFont.pointSize];
- [textLayer setAnchorPoint:CGPointMake(0.5, 0.5)];
- [textLayer setAlignmentMode:kCAAlignmentCenter];
- [textLayer setBackgroundColor:[UIColor clearColor].CGColor];
- [textLayer setForegroundColor:self.labelColor.CGColor];
- if (self.labelShadowColor) {
- [textLayer setShadowColor:self.labelShadowColor.CGColor];
- [textLayer setShadowOffset:CGSizeZero];
- [textLayer setShadowOpacity:1.0f];
- [textLayer setShadowRadius:2.0f];
- }
- CGSize size = [@"0" sizeWithFont:self.labelFont];
- [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)))];
- [CATransaction setDisableActions:NO];
- [pieLayer addSublayer:textLayer];
- return pieLayer;
-}
-
-- (void)updateLabelForLayer:(SliceLayer *)pieLayer value:(CGFloat)value
-{
- CATextLayer *textLayer = [[pieLayer sublayers] objectAtIndex:0];
- [textLayer setHidden:!_showLabel];
- if(!_showLabel) return;
- NSString *label;
- if(_showPercentage)
- label = [NSString stringWithFormat:@"%0.0f", pieLayer.percentage*100];
- else
- label = (pieLayer.text)?pieLayer.text:[NSString stringWithFormat:@"%0.0f", value];
-
- CGSize size = [label sizeWithFont:self.labelFont];
-
- [CATransaction setDisableActions:YES];
- if(M_PI*2*_labelRadius*pieLayer.percentage < MAX(size.width,size.height) || value <= 0)
- {
- [textLayer setString:@""];
- }
- else
- {
- [textLayer setString:label];
- [textLayer setBounds:CGRectMake(0, 0, size.width, size.height)];
- }
- [CATransaction setDisableActions:NO];
-}
-
-@end
Please sign in to comment.
Something went wrong with that request. Please try again.