Showing
8 changed files
with
116 additions
and
14 deletions
| @@ -82,6 +82,7 @@ | @@ -82,6 +82,7 @@ | ||
| 82 | 9F656B50184A4E34002E5675 /* UICountingLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UICountingLabel.m; path = PNChart/ThirdPart/UICountingLabel.m; sourceTree = "<group>"; }; | 82 | 9F656B50184A4E34002E5675 /* UICountingLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UICountingLabel.m; path = PNChart/ThirdPart/UICountingLabel.m; sourceTree = "<group>"; }; |
| 83 | 9FA23B0E184A5944002DBBA4 /* PCChartsTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PCChartsTableViewController.h; sourceTree = "<group>"; }; | 83 | 9FA23B0E184A5944002DBBA4 /* PCChartsTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PCChartsTableViewController.h; sourceTree = "<group>"; }; |
| 84 | 9FA23B0F184A5944002DBBA4 /* PCChartsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PCChartsTableViewController.m; sourceTree = "<group>"; }; | 84 | 9FA23B0F184A5944002DBBA4 /* PCChartsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PCChartsTableViewController.m; sourceTree = "<group>"; }; |
| 85 | + 9FE9CFB818581102005B8223 /* PNChartDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PNChartDelegate.h; path = PNChart/PNChartDelegate.h; sourceTree = "<group>"; }; | ||
| 85 | /* End PBXFileReference section */ | 86 | /* End PBXFileReference section */ |
| 86 | 87 | ||
| 87 | /* Begin PBXFrameworksBuildPhase section */ | 88 | /* Begin PBXFrameworksBuildPhase section */ |
| @@ -192,6 +193,7 @@ | @@ -192,6 +193,7 @@ | ||
| 192 | 0AF7A8A0182AAAAA003645C4 /* PNChart.m */, | 193 | 0AF7A8A0182AAAAA003645C4 /* PNChart.m */, |
| 193 | 9F656B48184A4B83002E5675 /* PNBarChart */, | 194 | 9F656B48184A4B83002E5675 /* PNBarChart */, |
| 194 | 9F656B4B184A4BC9002E5675 /* PNCircleChart */, | 195 | 9F656B4B184A4BC9002E5675 /* PNCircleChart */, |
| 196 | + 9FE9CFB818581102005B8223 /* PNChartDelegate.h */, | ||
| 195 | ); | 197 | ); |
| 196 | name = PNChart; | 198 | name = PNChart; |
| 197 | sourceTree = "<group>"; | 199 | sourceTree = "<group>"; |
| @@ -7,7 +7,8 @@ | @@ -7,7 +7,8 @@ | ||
| 7 | // | 7 | // |
| 8 | 8 | ||
| 9 | #import <UIKit/UIKit.h> | 9 | #import <UIKit/UIKit.h> |
| 10 | +#import "PNChartDelegate.h" | ||
| 10 | 11 | ||
| 11 | -@interface PCChartsTableViewController : UITableViewController | 12 | +@interface PCChartsTableViewController : UITableViewController<PNChartDelegate> |
| 12 | 13 | ||
| 13 | @end | 14 | @end |
| @@ -63,6 +63,7 @@ | @@ -63,6 +63,7 @@ | ||
| 63 | [lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]]; | 63 | [lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]]; |
| 64 | [lineChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]]; | 64 | [lineChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]]; |
| 65 | [lineChart strokeChart]; | 65 | [lineChart strokeChart]; |
| 66 | + lineChart.delegate = self; | ||
| 66 | 67 | ||
| 67 | [viewController.view addSubview:lineChartLabel]; | 68 | [viewController.view addSubview:lineChartLabel]; |
| 68 | [viewController.view addSubview:lineChart]; | 69 | [viewController.view addSubview:lineChart]; |
| @@ -115,8 +116,14 @@ | @@ -115,8 +116,14 @@ | ||
| 115 | viewController.title = @"Circle Chart"; | 116 | viewController.title = @"Circle Chart"; |
| 116 | } | 117 | } |
| 117 | 118 | ||
| 119 | +} | ||
| 118 | 120 | ||
| 121 | +-(void)userClickedOnLineKeyPoint:(CGPoint)point andPointIndex:(NSInteger)index{ | ||
| 122 | + NSLog(@"Click Key on line %f, %f and index is %d",point.x, point.y,index); | ||
| 119 | } | 123 | } |
| 120 | 124 | ||
| 125 | +-(void)userClickedOnLinePoint:(CGPoint)point { | ||
| 126 | + NSLog(@"Click on line %f, %f",point.x, point.y); | ||
| 127 | +} | ||
| 121 | 128 | ||
| 122 | @end | 129 | @end |
| @@ -13,6 +13,7 @@ | @@ -13,6 +13,7 @@ | ||
| 13 | #import "PNLineChart.h" | 13 | #import "PNLineChart.h" |
| 14 | #import "PNBarChart.h" | 14 | #import "PNBarChart.h" |
| 15 | #import "PNCircleChart.h" | 15 | #import "PNCircleChart.h" |
| 16 | +#import "PNChartDelegate.h" | ||
| 16 | 17 | ||
| 17 | typedef enum { | 18 | typedef enum { |
| 18 | /** Solid line chart style */ | 19 | /** Solid line chart style */ |
| @@ -25,7 +26,14 @@ typedef enum { | @@ -25,7 +26,14 @@ typedef enum { | ||
| 25 | } PNChartType; | 26 | } PNChartType; |
| 26 | 27 | ||
| 27 | 28 | ||
| 28 | -@interface PNChart : UIView | 29 | +@interface PNChart : UIView<PNChartDelegate> |
| 30 | + | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * PNChart delegate. | ||
| 34 | + */ | ||
| 35 | + | ||
| 36 | +@property(nonatomic,retain) id<PNChartDelegate> delegate; | ||
| 29 | 37 | ||
| 30 | /** | 38 | /** |
| 31 | * This method will call and troke the line in animation. | 39 | * This method will call and troke the line in animation. |
| @@ -35,6 +35,8 @@ | @@ -35,6 +35,8 @@ | ||
| 35 | [_lineChart setXLabels:_xLabels]; | 35 | [_lineChart setXLabels:_xLabels]; |
| 36 | [_lineChart setStrokeColor:_strokeColor]; | 36 | [_lineChart setStrokeColor:_strokeColor]; |
| 37 | [_lineChart strokeChart]; | 37 | [_lineChart strokeChart]; |
| 38 | + _lineChart.delegate = self; | ||
| 39 | + | ||
| 38 | 40 | ||
| 39 | }else if (self.type == PNBarType) | 41 | }else if (self.type == PNBarType) |
| 40 | { | 42 | { |
| @@ -87,6 +89,12 @@ | @@ -87,6 +89,12 @@ | ||
| 87 | 89 | ||
| 88 | } | 90 | } |
| 89 | 91 | ||
| 92 | +-(void)userClickedOnLineKeyPoint:(CGPoint)point andPointIndex:(NSInteger)index{ | ||
| 93 | + [_delegate userClickedOnLineKeyPoint:point andPointIndex:index]; | ||
| 94 | +} | ||
| 90 | 95 | ||
| 96 | +-(void)userClickedOnLinePoint:(CGPoint)point { | ||
| 97 | + [_delegate userClickedOnLinePoint:point]; | ||
| 98 | +} | ||
| 91 | 99 | ||
| 92 | @end | 100 | @end |
PNChartDemo/PNChart/PNChartDelegate.h
0 → 100644
| 1 | +// | ||
| 2 | +// PNChartDelegate.h | ||
| 3 | +// PNChartDemo | ||
| 4 | +// | ||
| 5 | +// Created by kevinzhow on 13-12-11. | ||
| 6 | +// Copyright (c) 2013年 kevinzhow. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import <Foundation/Foundation.h> | ||
| 10 | + | ||
| 11 | +@protocol PNChartDelegate <NSObject> | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * When user click on the chart line | ||
| 15 | + * | ||
| 16 | + */ | ||
| 17 | +- (void)userClickedOnLinePoint:(CGPoint )point; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * When user click on the chart line key point | ||
| 21 | + * | ||
| 22 | + */ | ||
| 23 | +- (void)userClickedOnLineKeyPoint:(CGPoint )point andPointIndex:(NSInteger)index; | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +@end |
| @@ -9,6 +9,7 @@ | @@ -9,6 +9,7 @@ | ||
| 9 | 9 | ||
| 10 | #import <UIKit/UIKit.h> | 10 | #import <UIKit/UIKit.h> |
| 11 | #import <QuartzCore/QuartzCore.h> | 11 | #import <QuartzCore/QuartzCore.h> |
| 12 | +#import "PNChartDelegate.h" | ||
| 12 | 13 | ||
| 13 | #define chartMargin 10 | 14 | #define chartMargin 10 |
| 14 | #define yLabelMargin 15 | 15 | #define yLabelMargin 15 |
| @@ -20,7 +21,15 @@ | @@ -20,7 +21,15 @@ | ||
| 20 | * This method will call and troke the line in animation | 21 | * This method will call and troke the line in animation |
| 21 | */ | 22 | */ |
| 22 | 23 | ||
| 23 | --(void)strokeChart; | 24 | +- (void)strokeChart; |
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * This method will get the index user touched | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | +- (void)userTouchedOnPoint:(void (^)(NSInteger *pointIndex))getTouched; | ||
| 31 | + | ||
| 32 | +@property(nonatomic,retain) id<PNChartDelegate> delegate; | ||
| 24 | 33 | ||
| 25 | @property (strong, nonatomic) NSArray * xLabels; | 34 | @property (strong, nonatomic) NSArray * xLabels; |
| 26 | 35 | ||
| @@ -28,6 +37,8 @@ | @@ -28,6 +37,8 @@ | ||
| 28 | 37 | ||
| 29 | @property (strong, nonatomic) NSArray * yValues; | 38 | @property (strong, nonatomic) NSArray * yValues; |
| 30 | 39 | ||
| 40 | +@property (strong, nonatomic) NSMutableArray * pathPoints; | ||
| 41 | + | ||
| 31 | @property (nonatomic) CGFloat xLabelWidth; | 42 | @property (nonatomic) CGFloat xLabelWidth; |
| 32 | 43 | ||
| 33 | @property (nonatomic) int yValueMax; | 44 | @property (nonatomic) int yValueMax; |
| @@ -38,4 +49,6 @@ | @@ -38,4 +49,6 @@ | ||
| 38 | 49 | ||
| 39 | @property (nonatomic) BOOL showLabel; | 50 | @property (nonatomic) BOOL showLabel; |
| 40 | 51 | ||
| 52 | +@property (nonatomic, strong) UIBezierPath *progressline; | ||
| 53 | + | ||
| 41 | @end | 54 | @end |
| @@ -26,6 +26,8 @@ | @@ -26,6 +26,8 @@ | ||
| 26 | _chartLine.lineWidth = 3.0; | 26 | _chartLine.lineWidth = 3.0; |
| 27 | _chartLine.strokeEnd = 0.0; | 27 | _chartLine.strokeEnd = 0.0; |
| 28 | _showLabel = YES; | 28 | _showLabel = YES; |
| 29 | + _pathPoints = [[NSMutableArray alloc] init]; | ||
| 30 | + self.userInteractionEnabled = YES; | ||
| 29 | [self.layer addSublayer:_chartLine]; | 31 | [self.layer addSublayer:_chartLine]; |
| 30 | } | 32 | } |
| 31 | 33 | ||
| @@ -94,7 +96,6 @@ | @@ -94,7 +96,6 @@ | ||
| 94 | } | 96 | } |
| 95 | } | 97 | } |
| 96 | 98 | ||
| 97 | - | ||
| 98 | } | 99 | } |
| 99 | 100 | ||
| 100 | -(void)setStrokeColor:(UIColor *)strokeColor | 101 | -(void)setStrokeColor:(UIColor *)strokeColor |
| @@ -103,11 +104,46 @@ | @@ -103,11 +104,46 @@ | ||
| 103 | _chartLine.strokeColor = [strokeColor CGColor]; | 104 | _chartLine.strokeColor = [strokeColor CGColor]; |
| 104 | } | 105 | } |
| 105 | 106 | ||
| 107 | +-(void)userTouchedOnPoint:(void (^)(NSInteger *))getTouched | ||
| 108 | +{ | ||
| 109 | + | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | + | ||
| 113 | +-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | ||
| 114 | +{ | ||
| 115 | + [self chechPoint:touches withEvent:event]; | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | ||
| 119 | +{ | ||
| 120 | + [self chechPoint:touches withEvent:event]; | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +-(void)chechPoint:(NSSet *)touches withEvent:(UIEvent *)event | ||
| 124 | +{ | ||
| 125 | + UITouch *touch = [touches anyObject]; | ||
| 126 | + CGPoint touchPoint = [touch locationInView:self]; | ||
| 127 | + CGPathRef originalPath = _progressline.CGPath; | ||
| 128 | + CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(originalPath, NULL, 3.0, kCGLineCapRound, kCGLineJoinRound, 3.0); | ||
| 129 | + BOOL pathContainsPoint = CGPathContainsPoint(strokedPath, NULL, touchPoint, NO); | ||
| 130 | + if (pathContainsPoint) | ||
| 131 | + { | ||
| 132 | + [_delegate userClickedOnLinePoint:touchPoint]; | ||
| 133 | + for (NSValue *val in _pathPoints) { | ||
| 134 | + CGPoint p = [val CGPointValue]; | ||
| 135 | + if (p.x + 3.0 > touchPoint.x && p.x - 3.0 < touchPoint.x && p.y + 3.0 > touchPoint.y && p.y - 3.0 < touchPoint.y ) { | ||
| 136 | + [_delegate userClickedOnLineKeyPoint:touchPoint andPointIndex:[_pathPoints indexOfObject:val]]; | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | +} | ||
| 141 | + | ||
| 106 | -(void)strokeChart | 142 | -(void)strokeChart |
| 107 | { | 143 | { |
| 108 | UIGraphicsBeginImageContext(self.frame.size); | 144 | UIGraphicsBeginImageContext(self.frame.size); |
| 109 | 145 | ||
| 110 | - UIBezierPath *progressline = [UIBezierPath bezierPath]; | 146 | + _progressline = [UIBezierPath bezierPath]; |
| 111 | 147 | ||
| 112 | CGFloat firstValue = [[_yValues objectAtIndex:0] floatValue]; | 148 | CGFloat firstValue = [[_yValues objectAtIndex:0] floatValue]; |
| 113 | 149 | ||
| @@ -122,10 +158,11 @@ | @@ -122,10 +158,11 @@ | ||
| 122 | float grade = (float)firstValue / (float)_yValueMax; | 158 | float grade = (float)firstValue / (float)_yValueMax; |
| 123 | 159 | ||
| 124 | 160 | ||
| 125 | - [progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + xLabelHeight)]; | 161 | + [_progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + xLabelHeight)]; |
| 126 | - [progressline setLineWidth:3.0]; | 162 | + [_pathPoints addObject:[NSValue valueWithCGPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + xLabelHeight)]]; |
| 127 | - [progressline setLineCapStyle:kCGLineCapRound]; | 163 | + [_progressline setLineWidth:3.0]; |
| 128 | - [progressline setLineJoinStyle:kCGLineJoinRound]; | 164 | + [_progressline setLineCapStyle:kCGLineCapRound]; |
| 165 | + [_progressline setLineJoinStyle:kCGLineJoinRound]; | ||
| 129 | NSInteger index = 0; | 166 | NSInteger index = 0; |
| 130 | for (NSString * valueString in _yValues) { | 167 | for (NSString * valueString in _yValues) { |
| 131 | float value = [valueString floatValue]; | 168 | float value = [valueString floatValue]; |
| @@ -135,16 +172,16 @@ | @@ -135,16 +172,16 @@ | ||
| 135 | 172 | ||
| 136 | 173 | ||
| 137 | CGPoint point = CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth /2.0, chartCavanHeight - (grade * chartCavanHeight) + xLabelHeight); | 174 | CGPoint point = CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth /2.0, chartCavanHeight - (grade * chartCavanHeight) + xLabelHeight); |
| 138 | - | 175 | + [_pathPoints addObject:[NSValue valueWithCGPoint:point]]; |
| 139 | - [progressline addLineToPoint:point]; | 176 | + [_progressline addLineToPoint:point]; |
| 140 | - [progressline moveToPoint:point]; | 177 | + [_progressline moveToPoint:point]; |
| 141 | } | 178 | } |
| 142 | 179 | ||
| 143 | index += 1; | 180 | index += 1; |
| 144 | } | 181 | } |
| 145 | - [progressline stroke]; | 182 | + [_progressline stroke]; |
| 146 | 183 | ||
| 147 | - _chartLine.path = progressline.CGPath; | 184 | + _chartLine.path = _progressline.CGPath; |
| 148 | if (_strokeColor) { | 185 | if (_strokeColor) { |
| 149 | _chartLine.strokeColor = [_strokeColor CGColor]; | 186 | _chartLine.strokeColor = [_strokeColor CGColor]; |
| 150 | }else{ | 187 | }else{ |
-
Please register or login to post a comment