kevinzhow

Add Support multible line callback

@@ -137,12 +137,12 @@ @@ -137,12 +137,12 @@
137 137
138 } 138 }
139 139
140 --(void)userClickedOnLineKeyPoint:(CGPoint)point andPointIndex:(NSInteger)index{ 140 +-(void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex{
141 - NSLog(@"Click Key on line %f, %f and index is %ld",point.x, point.y,(long)index); 141 + NSLog(@"Click Key on line %f, %f line index is %d and point index is %d",point.x, point.y,(int)lineIndex, (int)pointIndex);
142 } 142 }
143 143
144 --(void)userClickedOnLinePoint:(CGPoint)point { 144 +-(void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex{
145 - NSLog(@"Click on line %f, %f",point.x, point.y); 145 + NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex);
146 } 146 }
147 147
148 @end 148 @end
@@ -14,13 +14,13 @@ @@ -14,13 +14,13 @@
14 * When user click on the chart line 14 * When user click on the chart line
15 * 15 *
16 */ 16 */
17 -- (void)userClickedOnLinePoint:(CGPoint )point; 17 +- (void)userClickedOnLinePoint:(CGPoint )point lineIndex:(NSInteger)lineIndex;
18 18
19 /** 19 /**
20 * When user click on the chart line key point 20 * When user click on the chart line key point
21 * 21 *
22 */ 22 */
23 -- (void)userClickedOnLineKeyPoint:(CGPoint )point andPointIndex:(NSInteger)index; 23 +- (void)userClickedOnLineKeyPoint:(CGPoint )point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex;
24 24
25 25
26 @end 26 @end
@@ -46,6 +46,4 @@ @@ -46,6 +46,4 @@
46 46
47 @property (nonatomic) BOOL showLabel; 47 @property (nonatomic) BOOL showLabel;
48 48
49 -@property (nonatomic, strong) UIBezierPath *progressline;  
50 -  
51 @end 49 @end
@@ -20,6 +20,8 @@ @@ -20,6 +20,8 @@
20 20
21 @property (nonatomic,strong) NSMutableArray *chartLineArray; // Array[CAShapeLayer] 21 @property (nonatomic,strong) NSMutableArray *chartLineArray; // Array[CAShapeLayer]
22 22
  23 +@property (strong, nonatomic) NSMutableArray *chartPath; //Array of line path, one for each line.
  24 +
23 - (void)setDefaultValues; 25 - (void)setDefaultValues;
24 26
25 @end 27 @end
@@ -90,43 +92,53 @@ @@ -90,43 +92,53 @@
90 92
91 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 93 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
92 { 94 {
93 - [self chechPoint:touches withEvent:event]; 95 + [self touchPoint:touches withEvent:event];
94 } 96 }
95 97
96 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 98 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
97 { 99 {
98 - [self chechPoint:touches withEvent:event]; 100 + [self touchPoint:touches withEvent:event];
99 } 101 }
100 102
101 --(void)chechPoint:(NSSet *)touches withEvent:(UIEvent *)event 103 +-(void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event
102 { 104 {
  105 + //Get the point user touched
103 UITouch *touch = [touches anyObject]; 106 UITouch *touch = [touches anyObject];
104 CGPoint touchPoint = [touch locationInView:self]; 107 CGPoint touchPoint = [touch locationInView:self];
105 - CGPathRef originalPath = _progressline.CGPath; 108 + for (UIBezierPath *path in _chartPath) {
106 - CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(originalPath, NULL, 3.0, kCGLineCapRound, kCGLineJoinRound, 3.0); 109 + CGPathRef originalPath = path.CGPath;
107 - BOOL pathContainsPoint = CGPathContainsPoint(strokedPath, NULL, touchPoint, NO); 110 + CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(originalPath, NULL, 3.0, kCGLineCapRound, kCGLineJoinRound, 3.0);
108 - if (pathContainsPoint) 111 + BOOL pathContainsPoint = CGPathContainsPoint(strokedPath, NULL, touchPoint, NO);
109 - { 112 + if (pathContainsPoint)
110 - [_delegate userClickedOnLinePoint:touchPoint]; 113 + {
111 - for (NSValue *val in _pathPoints) { 114 + [_delegate userClickedOnLinePoint:touchPoint lineIndex:[_chartPath indexOfObject:path]];
112 - CGPoint p = [val CGPointValue]; 115 + for (NSArray *linePointsArray in _pathPoints) {
113 - 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 ) { 116 + for (NSValue *val in linePointsArray) {
114 - [_delegate userClickedOnLineKeyPoint:touchPoint andPointIndex:[_pathPoints indexOfObject:val]]; 117 + CGPoint p = [val CGPointValue];
  118 + 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 ) {
  119 + //Call the delegate and pass the point and index of the point
  120 + [_delegate userClickedOnLineKeyPoint:touchPoint lineIndex:[_pathPoints indexOfObject:linePointsArray] andPointIndex:[linePointsArray indexOfObject:val]];
  121 + }
  122 + }
115 } 123 }
  124 +
116 } 125 }
117 } 126 }
  127 +
118 } 128 }
119 129
120 -(void)strokeChart 130 -(void)strokeChart
121 { 131 {
122 - for (NSUInteger a = 0; a < self.chartData.count; a++) { 132 + _chartPath = [[NSMutableArray alloc] init];
123 - PNLineChartData *chartData = self.chartData[a]; 133 + //Draw each line
124 - CAShapeLayer *chartLine = (CAShapeLayer *) self.chartLineArray[a]; 134 + for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) {
  135 + PNLineChartData *chartData = self.chartData[lineIndex];
  136 + CAShapeLayer *chartLine = (CAShapeLayer *) self.chartLineArray[lineIndex];
125 137
126 UIGraphicsBeginImageContext(self.frame.size); 138 UIGraphicsBeginImageContext(self.frame.size);
127 - 139 + UIBezierPath * progressline = [UIBezierPath bezierPath];
128 - _progressline = [UIBezierPath bezierPath]; 140 + [_chartPath addObject:progressline];
129 - 141 +
130 PNLineChartDataItem *firstDataItem = chartData.getData(0); 142 PNLineChartDataItem *firstDataItem = chartData.getData(0);
131 CGFloat firstValue = firstDataItem.y; 143 CGFloat firstValue = firstDataItem.y;
132 144
@@ -138,13 +150,14 @@ @@ -138,13 +150,14 @@
138 } 150 }
139 151
140 CGFloat grade = (float)firstValue / _yValueMax; 152 CGFloat grade = (float)firstValue / _yValueMax;
141 - 153 + NSMutableArray * linePointsArray = [[NSMutableArray alloc] init];
142 - [_progressline moveToPoint:CGPointMake( xPosition, _chartCavanHeight - grade * _chartCavanHeight + _xLabelHeight)]; 154 + [progressline moveToPoint:CGPointMake( xPosition, _chartCavanHeight - grade * _chartCavanHeight + _xLabelHeight)];
143 - [_pathPoints addObject:[NSValue valueWithCGPoint:CGPointMake( xPosition, _chartCavanHeight - grade * _chartCavanHeight + _xLabelHeight)]]; 155 + [linePointsArray addObject:[NSValue valueWithCGPoint:CGPointMake( xPosition, _chartCavanHeight - grade * _chartCavanHeight + _xLabelHeight)]];
144 - [_progressline setLineWidth:3.0]; 156 + [progressline setLineWidth:3.0];
145 - [_progressline setLineCapStyle:kCGLineCapRound]; 157 + [progressline setLineCapStyle:kCGLineCapRound];
146 - [_progressline setLineJoinStyle:kCGLineJoinRound]; 158 + [progressline setLineJoinStyle:kCGLineJoinRound];
147 - 159 +
  160 +
148 NSInteger index = 0; 161 NSInteger index = 0;
149 for (NSUInteger i = 0; i < chartData.itemCount; i++) { 162 for (NSUInteger i = 0; i < chartData.itemCount; i++) {
150 163
@@ -154,13 +167,13 @@ @@ -154,13 +167,13 @@
154 CGFloat innerGrade = value / _yValueMax; 167 CGFloat innerGrade = value / _yValueMax;
155 if (index != 0) { 168 if (index != 0) {
156 CGPoint point = CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth / 2.0, _chartCavanHeight - (innerGrade * _chartCavanHeight) + _xLabelHeight); 169 CGPoint point = CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth / 2.0, _chartCavanHeight - (innerGrade * _chartCavanHeight) + _xLabelHeight);
157 - [_pathPoints addObject:[NSValue valueWithCGPoint:point]]; 170 + [linePointsArray addObject:[NSValue valueWithCGPoint:point]];
158 - [_progressline addLineToPoint:point]; 171 + [progressline addLineToPoint:point];
159 - [_progressline moveToPoint:point]; 172 + [progressline moveToPoint:point];
160 } 173 }
161 index += 1; 174 index += 1;
162 } 175 }
163 - 176 + [_pathPoints addObject:[linePointsArray copy]];
164 // setup the color of the chart line 177 // setup the color of the chart line
165 if (chartData.color) { 178 if (chartData.color) {
166 chartLine.strokeColor = [chartData.color CGColor]; 179 chartLine.strokeColor = [chartData.color CGColor];
@@ -168,9 +181,9 @@ @@ -168,9 +181,9 @@
168 chartLine.strokeColor = [PNGreen CGColor]; 181 chartLine.strokeColor = [PNGreen CGColor];
169 } 182 }
170 183
171 - [_progressline stroke]; 184 + [progressline stroke];
172 185
173 - chartLine.path = _progressline.CGPath; 186 + chartLine.path = progressline.CGPath;
174 187
175 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 188 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
176 pathAnimation.duration = 1.0; 189 pathAnimation.duration = 1.0;