kevinzhow

Add PNChartDelegate for callback support #4

... ... @@ -82,6 +82,7 @@
9F656B50184A4E34002E5675 /* UICountingLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UICountingLabel.m; path = PNChart/ThirdPart/UICountingLabel.m; sourceTree = "<group>"; };
9FA23B0E184A5944002DBBA4 /* PCChartsTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PCChartsTableViewController.h; sourceTree = "<group>"; };
9FA23B0F184A5944002DBBA4 /* PCChartsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PCChartsTableViewController.m; sourceTree = "<group>"; };
9FE9CFB818581102005B8223 /* PNChartDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PNChartDelegate.h; path = PNChart/PNChartDelegate.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
... ... @@ -192,6 +193,7 @@
0AF7A8A0182AAAAA003645C4 /* PNChart.m */,
9F656B48184A4B83002E5675 /* PNBarChart */,
9F656B4B184A4BC9002E5675 /* PNCircleChart */,
9FE9CFB818581102005B8223 /* PNChartDelegate.h */,
);
name = PNChart;
sourceTree = "<group>";
... ...
... ... @@ -7,7 +7,8 @@
//
#import <UIKit/UIKit.h>
#import "PNChartDelegate.h"
@interface PCChartsTableViewController : UITableViewController
@interface PCChartsTableViewController : UITableViewController<PNChartDelegate>
@end
... ...
... ... @@ -63,6 +63,7 @@
[lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
[lineChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
[lineChart strokeChart];
lineChart.delegate = self;
[viewController.view addSubview:lineChartLabel];
[viewController.view addSubview:lineChart];
... ... @@ -115,8 +116,14 @@
viewController.title = @"Circle Chart";
}
}
-(void)userClickedOnLineKeyPoint:(CGPoint)point andPointIndex:(NSInteger)index{
NSLog(@"Click Key on line %f, %f and index is %d",point.x, point.y,index);
}
-(void)userClickedOnLinePoint:(CGPoint)point {
NSLog(@"Click on line %f, %f",point.x, point.y);
}
@end
... ...
... ... @@ -13,6 +13,7 @@
#import "PNLineChart.h"
#import "PNBarChart.h"
#import "PNCircleChart.h"
#import "PNChartDelegate.h"
typedef enum {
/** Solid line chart style */
... ... @@ -25,7 +26,14 @@ typedef enum {
} PNChartType;
@interface PNChart : UIView
@interface PNChart : UIView<PNChartDelegate>
/**
* PNChart delegate.
*/
@property(nonatomic,retain) id<PNChartDelegate> delegate;
/**
* This method will call and troke the line in animation.
... ...
... ... @@ -35,6 +35,8 @@
[_lineChart setXLabels:_xLabels];
[_lineChart setStrokeColor:_strokeColor];
[_lineChart strokeChart];
_lineChart.delegate = self;
}else if (self.type == PNBarType)
{
... ... @@ -87,6 +89,12 @@
}
-(void)userClickedOnLineKeyPoint:(CGPoint)point andPointIndex:(NSInteger)index{
[_delegate userClickedOnLineKeyPoint:point andPointIndex:index];
}
-(void)userClickedOnLinePoint:(CGPoint)point {
[_delegate userClickedOnLinePoint:point];
}
@end
... ...
//
// PNChartDelegate.h
// PNChartDemo
//
// Created by kevinzhow on 13-12-11.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol PNChartDelegate <NSObject>
/**
* When user click on the chart line
*
*/
- (void)userClickedOnLinePoint:(CGPoint )point;
/**
* When user click on the chart line key point
*
*/
- (void)userClickedOnLineKeyPoint:(CGPoint )point andPointIndex:(NSInteger)index;
@end
... ...
... ... @@ -9,6 +9,7 @@
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "PNChartDelegate.h"
#define chartMargin 10
#define yLabelMargin 15
... ... @@ -20,7 +21,15 @@
* This method will call and troke the line in animation
*/
-(void)strokeChart;
- (void)strokeChart;
/**
* This method will get the index user touched
*/
- (void)userTouchedOnPoint:(void (^)(NSInteger *pointIndex))getTouched;
@property(nonatomic,retain) id<PNChartDelegate> delegate;
@property (strong, nonatomic) NSArray * xLabels;
... ... @@ -28,6 +37,8 @@
@property (strong, nonatomic) NSArray * yValues;
@property (strong, nonatomic) NSMutableArray * pathPoints;
@property (nonatomic) CGFloat xLabelWidth;
@property (nonatomic) int yValueMax;
... ... @@ -38,4 +49,6 @@
@property (nonatomic) BOOL showLabel;
@property (nonatomic, strong) UIBezierPath *progressline;
@end
... ...
... ... @@ -26,6 +26,8 @@
_chartLine.lineWidth = 3.0;
_chartLine.strokeEnd = 0.0;
_showLabel = YES;
_pathPoints = [[NSMutableArray alloc] init];
self.userInteractionEnabled = YES;
[self.layer addSublayer:_chartLine];
}
... ... @@ -94,7 +96,6 @@
}
}
}
-(void)setStrokeColor:(UIColor *)strokeColor
... ... @@ -103,11 +104,46 @@
_chartLine.strokeColor = [strokeColor CGColor];
}
-(void)userTouchedOnPoint:(void (^)(NSInteger *))getTouched
{
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self chechPoint:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self chechPoint:touches withEvent:event];
}
-(void)chechPoint:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
CGPathRef originalPath = _progressline.CGPath;
CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(originalPath, NULL, 3.0, kCGLineCapRound, kCGLineJoinRound, 3.0);
BOOL pathContainsPoint = CGPathContainsPoint(strokedPath, NULL, touchPoint, NO);
if (pathContainsPoint)
{
[_delegate userClickedOnLinePoint:touchPoint];
for (NSValue *val in _pathPoints) {
CGPoint p = [val CGPointValue];
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 ) {
[_delegate userClickedOnLineKeyPoint:touchPoint andPointIndex:[_pathPoints indexOfObject:val]];
}
}
}
}
-(void)strokeChart
{
UIGraphicsBeginImageContext(self.frame.size);
UIBezierPath *progressline = [UIBezierPath bezierPath];
_progressline = [UIBezierPath bezierPath];
CGFloat firstValue = [[_yValues objectAtIndex:0] floatValue];
... ... @@ -122,10 +158,11 @@
float grade = (float)firstValue / (float)_yValueMax;
[progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + xLabelHeight)];
[progressline setLineWidth:3.0];
[progressline setLineCapStyle:kCGLineCapRound];
[progressline setLineJoinStyle:kCGLineJoinRound];
[_progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + xLabelHeight)];
[_pathPoints addObject:[NSValue valueWithCGPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + xLabelHeight)]];
[_progressline setLineWidth:3.0];
[_progressline setLineCapStyle:kCGLineCapRound];
[_progressline setLineJoinStyle:kCGLineJoinRound];
NSInteger index = 0;
for (NSString * valueString in _yValues) {
float value = [valueString floatValue];
... ... @@ -135,16 +172,16 @@
CGPoint point = CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth /2.0, chartCavanHeight - (grade * chartCavanHeight) + xLabelHeight);
[progressline addLineToPoint:point];
[progressline moveToPoint:point];
[_pathPoints addObject:[NSValue valueWithCGPoint:point]];
[_progressline addLineToPoint:point];
[_progressline moveToPoint:point];
}
index += 1;
}
[progressline stroke];
[_progressline stroke];
_chartLine.path = progressline.CGPath;
_chartLine.path = _progressline.CGPath;
if (_strokeColor) {
_chartLine.strokeColor = [_strokeColor CGColor];
}else{
... ...