Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Pie Chart Library/DLPieChart.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
#import <UIKit/UIKit.h>

@class DLPieChart;

@protocol DLPieChartDataSource <NSObject>

@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 <NSObject>

@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 <DLPieChartDelegate, DLPieChartDataSource>
Expand All @@ -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;

Expand All @@ -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;
98 changes: 71 additions & 27 deletions Pie Chart Library/DLPieChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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];
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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)))];
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>BB8715F0-94F3-463B-9A21-DA59281C7A3E</string>
<key>IDESourceControlProjectName</key>
<string>PieChart</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>EABBA475-0CD2-4703-8D75-571C423FC6E9</key>
<string>ssh://github.com/lduraes/piechart.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>PieChart.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>EABBA475-0CD2-4703-8D75-571C423FC6E9</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>ssh://github.com/lduraes/piechart.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>EABBA475-0CD2-4703-8D75-571C423FC6E9</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>EABBA475-0CD2-4703-8D75-571C423FC6E9</string>
<key>IDESourceControlWCCName</key>
<string>piechart</string>
</dict>
</array>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0500"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C82E1221886CB4A00EACB7D"
BuildableName = "PieChart.app"
BlueprintName = "PieChart"
ReferencedContainer = "container:PieChart.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C82E1431886CB4A00EACB7D"
BuildableName = "PieChartTests.xctest"
BlueprintName = "PieChartTests"
ReferencedContainer = "container:PieChart.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C82E1221886CB4A00EACB7D"
BuildableName = "PieChart.app"
BlueprintName = "PieChart"
ReferencedContainer = "container:PieChart.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C82E1221886CB4A00EACB7D"
BuildableName = "PieChart.app"
BlueprintName = "PieChart"
ReferencedContainer = "container:PieChart.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C82E1221886CB4A00EACB7D"
BuildableName = "PieChart.app"
BlueprintName = "PieChart"
ReferencedContainer = "container:PieChart.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>PieChart.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>9C82E1221886CB4A00EACB7D</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>9C82E1431886CB4A00EACB7D</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
Loading