diff --git a/Classes/JBBarChartView.m b/Classes/JBBarChartView.m index 44c1ddc8..839cbffc 100644 --- a/Classes/JBBarChartView.m +++ b/Classes/JBBarChartView.m @@ -38,6 +38,7 @@ static UIColor *kJBBarChartViewDefaultBarColor = nil; // Touch helpers - (NSInteger)barViewIndexForPoint:(CGPoint)point; - (UIView *)barViewForForPoint:(CGPoint)point; +- (void)touchesBeganOrMovedWithTouches:(NSSet *)touches; - (void)touchesEndedOrCancelledWithTouches:(NSSet *)touches; // Setters @@ -363,6 +364,33 @@ static UIColor *kJBBarChartViewDefaultBarColor = nil; return barView; } +- (void)touchesBeganOrMovedWithTouches:(NSSet *)touches +{ + if (self.state == JBChartViewStateCollapsed) + { + return; + } + + UITouch *touch = [touches anyObject]; + CGPoint touchPoint = [touch locationInView:self]; + UIView *barView = [self barViewForForPoint:touchPoint]; + if (barView == nil) + { + [self setVerticalSelectionViewVisible:NO animated:YES]; + return; + } + CGRect barViewFrame = barView.frame; + CGRect selectionViewFrame = self.verticalSelectionView.frame; + selectionViewFrame.origin.x = barViewFrame.origin.x; + self.verticalSelectionView.frame = selectionViewFrame; + [self setVerticalSelectionViewVisible:YES animated:YES]; + + if ([self.delegate respondsToSelector:@selector(barChartView:didSelectBarAtIndex:)]) + { + [self.delegate barChartView:self didSelectBarAtIndex:[self barViewIndexForPoint:touchPoint]]; + } +} + - (void)touchesEndedOrCancelledWithTouches:(NSSet *)touches { if (self.state == JBChartViewStateCollapsed) @@ -414,56 +442,12 @@ static UIColor *kJBBarChartViewDefaultBarColor = nil; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - if (self.state == JBChartViewStateCollapsed) - { - return; - } - - UITouch *touch = [touches anyObject]; - CGPoint touchPoint = [touch locationInView:self]; - UIView *barView = [self barViewForForPoint:touchPoint]; - if (barView == nil) - { - [self setVerticalSelectionViewVisible:NO animated:YES]; - return; - } - CGRect barViewFrame = barView.frame; - CGRect selectionViewFrame = self.verticalSelectionView.frame; - selectionViewFrame.origin.x = barViewFrame.origin.x; - self.verticalSelectionView.frame = selectionViewFrame; - [self setVerticalSelectionViewVisible:YES animated:YES]; - - if ([self.delegate respondsToSelector:@selector(barChartView:didSelectBarAtIndex:)]) - { - [self.delegate barChartView:self didSelectBarAtIndex:[self barViewIndexForPoint:touchPoint]]; - } + [self touchesBeganOrMovedWithTouches:touches]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - if (self.state == JBChartViewStateCollapsed) - { - return; - } - - UITouch *touch = [touches anyObject]; - CGPoint touchPoint = [touch locationInView:self]; - UIView *barView = [self barViewForForPoint:touchPoint]; - if (barView == nil) - { - [self setVerticalSelectionViewVisible:NO animated:YES]; - return; - } - CGRect barViewFrame = barView.frame; - CGRect selectionViewFrame = self.verticalSelectionView.frame; - selectionViewFrame.origin.x = barViewFrame.origin.x; - self.verticalSelectionView.frame = selectionViewFrame; - [self setVerticalSelectionViewVisible:YES animated:YES]; - - if ([self.delegate respondsToSelector:@selector(barChartView:didSelectBarAtIndex:)]) - { - [self.delegate barChartView:self didSelectBarAtIndex:[self barViewIndexForPoint:touchPoint]]; - } + [self touchesBeganOrMovedWithTouches:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event diff --git a/Classes/JBLineChartView.h b/Classes/JBLineChartView.h index c2be3430..a1c97fbb 100644 --- a/Classes/JBLineChartView.h +++ b/Classes/JBLineChartView.h @@ -115,7 +115,8 @@ /** * Returns the (vertical) selection color to be overlayed on the chart during touch events. - * The color is automically faded to transparent (vertically). + * The color is automically faded to transparent (vertically). The property showsVerticalSelection + * must be YES for the color to apply. * * Default: white color (faded to transparent). * @@ -125,4 +126,17 @@ */ - (UIColor *)verticalSelectionColorForLineChartView:(JBLineChartView *)lineChartView; +/** + * Returns the selection color to be overlayed on a line within the chart during touch events. + * The property showsLineSelection must be YES for the color to apply. + * + * Default: white color. + * + * @param lineChartView The line chart object requesting this information. + * @param lineIndex An index number identifying a line in the chart. + * + * @return The color to be used to highlight a line during chart selections. + */ +- (UIColor *)lineSelectionColorForLineChartView:(JBLineChartView *)lineChartView atLineIndex:(NSInteger)lineIndex; + @end diff --git a/Classes/JBLineChartView.m b/Classes/JBLineChartView.m index 3815fea8..58bbedbe 100644 --- a/Classes/JBLineChartView.m +++ b/Classes/JBLineChartView.m @@ -94,6 +94,7 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil; - (NSArray *)largestLineData; // largest collection of line data - (NSInteger)horizontalIndexForPoint:(CGPoint)point; - (NSInteger)lineIndexForTouch:(UITouch *)touch; +- (void)touchesBeganOrMovedWithTouches:(NSSet *)touches; - (void)touchesEndedOrCancelledWithTouches:(NSSet *)touches; // Setters @@ -409,6 +410,26 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil; return shortestIndex; } +- (void)touchesBeganOrMovedWithTouches:(NSSet *)touches +{ + if (self.state == JBChartViewStateCollapsed) + { + return; + } + + UITouch *touch = [touches anyObject]; + CGPoint touchPoint = [touch locationInView:self]; + + if ([self.delegate respondsToSelector:@selector(lineChartView:didSelectChartAtHorizontalIndex:atLineIndex:)]) + { + [self.delegate lineChartView:self didSelectChartAtHorizontalIndex:[self horizontalIndexForPoint:touchPoint] atLineIndex:[self lineIndexForTouch:touch]]; + } + + CGFloat xOffset = fmin(self.bounds.size.width - self.verticalSelectionView.frame.size.width, fmax(0, touchPoint.x - (ceil(self.verticalSelectionView.frame.size.width * 0.5)))); + self.verticalSelectionView.frame = CGRectMake(xOffset, self.verticalSelectionView.frame.origin.y, self.verticalSelectionView.frame.size.width, self.verticalSelectionView.frame.size.height); + [self setVerticalSelectionViewVisible:YES animated:YES]; +} + - (void)touchesEndedOrCancelledWithTouches:(NSSet *)touches { if (self.state == JBChartViewStateCollapsed) @@ -462,42 +483,12 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - if (self.state == JBChartViewStateCollapsed) - { - return; - } - - UITouch *touch = [touches anyObject]; - CGPoint touchPoint = [touch locationInView:self]; - - if ([self.delegate respondsToSelector:@selector(lineChartView:didSelectChartAtHorizontalIndex:atLineIndex:)]) - { - [self.delegate lineChartView:self didSelectChartAtHorizontalIndex:[self horizontalIndexForPoint:touchPoint] atLineIndex:[self lineIndexForTouch:touch]]; - } - - CGFloat xOffset = fmin(self.bounds.size.width - self.verticalSelectionView.frame.size.width, fmax(0, touchPoint.x - (ceil(self.verticalSelectionView.frame.size.width * 0.5)))); - self.verticalSelectionView.frame = CGRectMake(xOffset, self.verticalSelectionView.frame.origin.y, self.verticalSelectionView.frame.size.width, self.verticalSelectionView.frame.size.height); - [self setVerticalSelectionViewVisible:YES animated:YES]; + [self touchesBeganOrMovedWithTouches:touches]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - if (self.state == JBChartViewStateCollapsed) - { - return; - } - - UITouch *touch = [touches anyObject]; - CGPoint touchPoint = [touch locationInView:self]; - - if ([self.delegate respondsToSelector:@selector(lineChartView:didSelectChartAtHorizontalIndex:atLineIndex:)]) - { - [self.delegate lineChartView:self didSelectChartAtHorizontalIndex:[self horizontalIndexForPoint:touchPoint] atLineIndex:[self lineIndexForTouch:touch]]; - } - - CGFloat xOffset = fmin(self.bounds.size.width - self.verticalSelectionView.frame.size.width, fmax(0, touchPoint.x - (ceil(self.verticalSelectionView.frame.size.width * 0.5)))); - self.verticalSelectionView.frame = CGRectMake(xOffset, self.verticalSelectionView.frame.origin.y, self.verticalSelectionView.frame.size.width, self.verticalSelectionView.frame.size.height); - [self setVerticalSelectionViewVisible:YES animated:YES]; + [self touchesBeganOrMovedWithTouches:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event