Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit e2bf960

Browse files
committed
Merge pull request #97 from Boris-Em/pr/90
Pr/90 - Various syntax changes / code improvement
2 parents 2739cce + 859c806 commit e2bf960

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

Classes/BEMSimpleLineGraphView.m

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
1818
#define DEFAULT_FONT_NAME @"HelveticaNeue-Light"
1919

20+
21+
typedef NS_ENUM(NSInteger, BEMInternalTags)
22+
{
23+
DotFirstTag100 = 100,
24+
DotLastTag1000 = 1000,
25+
LabelYAxisTag2000 = 2000,
26+
BackgroundYAxisTag2100 = 2100,
27+
PermanentPopUpViewTag3100 = 3100,
28+
};
29+
2030
@interface BEMSimpleLineGraphView () {
2131
/// The number of Points in the Graph
2232
NSInteger numberOfPoints;
@@ -373,7 +383,7 @@ - (void)drawDots {
373383
if (self.animationGraphEntranceTime != 0 || self.alwaysDisplayDots == YES) {
374384
BEMCircle *circleDot = [[BEMCircle alloc] initWithFrame:CGRectMake(0, 0, self.sizePoint, self.sizePoint)];
375385
circleDot.center = CGPointMake(positionOnXAxis, positionOnYAxis);
376-
circleDot.tag = i+100;
386+
circleDot.tag = i+ DotFirstTag100;
377387
circleDot.alpha = 0;
378388
circleDot.absoluteValue = dotValue;
379389
circleDot.Pointcolor = self.colorPoint;
@@ -461,7 +471,7 @@ - (void)drawXAxis {
461471
if (![self.dataSource respondsToSelector:@selector(lineGraph:labelOnXAxisForIndex:)] && ![self.dataSource respondsToSelector:@selector(labelOnXAxisForIndex:)]) return;
462472

463473
for (UIView *subview in [self subviews]) {
464-
if ([subview isKindOfClass:[UILabel class]] && subview.tag == 1000)
474+
if ([subview isKindOfClass:[UILabel class]] && subview.tag == DotLastTag1000 )
465475
[subview removeFromSuperview];
466476
}
467477

@@ -514,29 +524,29 @@ - (void)drawXAxis {
514524
UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(3+self.YAxisLabelXOffset, self.frame.size.height-20, viewWidth/2, 20)];
515525
firstLabel.text = firstXLabel;
516526
firstLabel.font = self.labelFont;
517-
firstLabel.textAlignment = 0;
527+
firstLabel.textAlignment = NSTextAlignmentLeft;
518528
firstLabel.textColor = self.colorXaxisLabel;
519529
firstLabel.backgroundColor = [UIColor clearColor];
520-
firstLabel.tag = 1000;
530+
firstLabel.tag = DotLastTag1000;
521531
[self addSubview:firstLabel];
522532
[xAxisValues addObject:firstXLabel];
523533
[xAxisLabels addObject:firstLabel];
524-
525-
NSNumber *xFirstAxisLabelCoordinate = [NSNumber numberWithFloat:firstLabel.center.x-self.YAxisLabelXOffset];
534+
535+
NSNumber *xFirstAxisLabelCoordinate = @(firstLabel.center.x - self.YAxisLabelXOffset);
526536
[xAxisLabelPoints addObject:xFirstAxisLabelCoordinate];
527537

528538
UILabel *lastLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2 - 3, self.frame.size.height-20, self.frame.size.width/2, 20)];
529539
lastLabel.text = lastXLabel;
530540
lastLabel.font = self.labelFont;
531-
lastLabel.textAlignment = 2;
541+
lastLabel.textAlignment = NSTextAlignmentRight;
532542
lastLabel.textColor = self.colorXaxisLabel;
533543
lastLabel.backgroundColor = [UIColor clearColor];
534-
lastLabel.tag = 1000;
544+
lastLabel.tag = DotLastTag1000;
535545
[self addSubview:lastLabel];
536546
[xAxisValues addObject:lastXLabel];
537547
[xAxisLabels addObject:lastLabel];
538548

539-
NSNumber *xLastAxisLabelCoordinate = [NSNumber numberWithFloat:lastLabel.center.x-self.YAxisLabelXOffset];
549+
NSNumber *xLastAxisLabelCoordinate = @(lastLabel.center.x - self.YAxisLabelXOffset);
540550
[xAxisLabelPoints addObject:xLastAxisLabelCoordinate];
541551

542552
} else {
@@ -573,7 +583,7 @@ - (void)drawXAxis {
573583
labelXAxis.textColor = self.colorXaxisLabel;
574584
labelXAxis.backgroundColor = [UIColor clearColor];
575585
[xAxisLabels addObject:labelXAxis];
576-
labelXAxis.tag = 1000;
586+
labelXAxis.tag = DotLastTag1000;
577587

578588
// Add support multi-line, but this might overlap with the graph line if text have too many lines
579589
labelXAxis.numberOfLines = 0;
@@ -613,16 +623,16 @@ - (void)drawXAxis {
613623

614624
- (void)drawYAxis {
615625
for (UIView *subview in [self subviews]) {
616-
if ([subview isKindOfClass:[UILabel class]] && subview.tag == 2000) {
626+
if ([subview isKindOfClass:[UILabel class]] && subview.tag == LabelYAxisTag2000 ) {
617627
[subview removeFromSuperview];
618628
}
619-
else if ([subview isKindOfClass:[UIView class] ] && subview.tag == 2100) {
629+
else if ([subview isKindOfClass:[UIView class] ] && subview.tag == BackgroundYAxisTag2100 ) {
620630
[subview removeFromSuperview];
621631
}
622632
}
623633

624634
UIView *backgroundYaxis = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.YAxisLabelXOffset, self.frame.size.height)];
625-
backgroundYaxis.tag = 2100;
635+
backgroundYaxis.tag = BackgroundYAxisTag2100;
626636
if (self.colorBackgroundYaxis == nil) {
627637
backgroundYaxis.backgroundColor = self.colorTop;
628638
} else backgroundYaxis.backgroundColor = self.colorBackgroundYaxis;
@@ -663,16 +673,16 @@ - (void)drawYAxis {
663673
labelYAxis.font = self.labelFont;
664674
labelYAxis.textColor = self.colorYaxisLabel;
665675
labelYAxis.backgroundColor = [UIColor clearColor];
666-
labelYAxis.tag = 2000;
676+
labelYAxis.tag = LabelYAxisTag2000;
667677
labelYAxis.center = CGPointMake(self.YAxisLabelXOffset/2, yAxisPosition);
668678
[self addSubview:labelYAxis];
669679
[yAxisLabels addObject:labelYAxis];
670680

671-
NSNumber *yAxisLabelCoordinate = [NSNumber numberWithFloat:labelYAxis.center.y];
681+
NSNumber *yAxisLabelCoordinate = @(labelYAxis.center.y);
672682
[yAxisLabelPoints addObject:yAxisLabelCoordinate];
673683
}
674684
} else {
675-
CGFloat numberOfLabels;
685+
NSInteger numberOfLabels;
676686
if ([self.delegate respondsToSelector:@selector(numberOfYAxisLabelsOnLineGraph:)]) numberOfLabels = [self.delegate numberOfYAxisLabelsOnLineGraph:self];
677687
else numberOfLabels = 3;
678688

@@ -691,25 +701,25 @@ - (void)drawYAxis {
691701
labelYAxis.textAlignment = NSTextAlignmentRight;
692702
labelYAxis.textColor = self.colorYaxisLabel;
693703
labelYAxis.backgroundColor = [UIColor clearColor];
694-
labelYAxis.tag = 2000;
704+
labelYAxis.tag = LabelYAxisTag2000;
695705

696706
[self addSubview:labelYAxis];
697707

698708
[yAxisLabels addObject:labelYAxis];
699709

700-
NSNumber *yAxisLabelCoordinate = [NSNumber numberWithFloat:labelYAxis.center.y];
710+
NSNumber *yAxisLabelCoordinate = @(labelYAxis.center.y);
701711
[yAxisLabelPoints addObject:yAxisLabelCoordinate];
702712
}
703713
}
704714

705715
// Detect overlapped labels
706-
__block NSUInteger lastMatchIndex;
716+
__block NSUInteger lastMatchIndex = 0;
707717
NSMutableArray *overlapLabels = [NSMutableArray arrayWithCapacity:0];
708718
[yAxisLabels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
709719

710720
if (idx==0) lastMatchIndex = 0;
711721
else { // Skip first one
712-
UILabel *prevLabel = [yAxisLabels objectAtIndex:lastMatchIndex];
722+
UILabel *prevLabel = yAxisLabels[lastMatchIndex];
713723
CGRect r = CGRectIntersection(prevLabel.frame, label.frame);
714724
if (CGRectIsNull(r)) lastMatchIndex = idx;
715725
else [overlapLabels addObject:label]; // overlapped
@@ -743,9 +753,9 @@ - (void)displayPermanentLabelForPoint:(BEMCircle *)circleDot {
743753
self.enablePopUpReport = NO;
744754
self.xCenterLabel = circleDot.center.x;
745755
UILabel *permanentPopUpLabel = [[UILabel alloc] init];
746-
permanentPopUpLabel.textAlignment = 1;
756+
permanentPopUpLabel.textAlignment = NSTextAlignmentCenter;
747757
permanentPopUpLabel.numberOfLines = 0;
748-
permanentPopUpLabel.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInteger:circleDot.absoluteValue]];
758+
permanentPopUpLabel.text = [NSString stringWithFormat:@"%@", @((NSInteger) circleDot.absoluteValue)];
749759
permanentPopUpLabel.font = self.labelFont;
750760
permanentPopUpLabel.backgroundColor = [UIColor clearColor];
751761
[permanentPopUpLabel sizeToFit];
@@ -756,7 +766,7 @@ - (void)displayPermanentLabelForPoint:(BEMCircle *)circleDot {
756766
permanentPopUpView.backgroundColor = [UIColor whiteColor];
757767
permanentPopUpView.alpha = 0;
758768
permanentPopUpView.layer.cornerRadius = 3;
759-
permanentPopUpView.tag = 3100;
769+
permanentPopUpView.tag = PermanentPopUpViewTag3100;
760770
permanentPopUpView.center = permanentPopUpLabel.center;
761771

762772
if (permanentPopUpLabel.frame.origin.x <= 0) {
@@ -796,7 +806,7 @@ - (void)displayPermanentLabelForPoint:(BEMCircle *)circleDot {
796806

797807
- (BOOL)checkOverlapsForView:(UIView *)view {
798808
for (UIView *viewForLabel in [self subviews]) {
799-
if ([viewForLabel isKindOfClass:[UIView class]] && viewForLabel.tag == 3100) {
809+
if ([viewForLabel isKindOfClass:[UIView class]] && viewForLabel.tag == PermanentPopUpViewTag3100 ) {
800810
if ((viewForLabel.frame.origin.x + viewForLabel.frame.size.width) >= view.frame.origin.x) {
801811
if (viewForLabel.frame.origin.y >= view.frame.origin.y && viewForLabel.frame.origin.y <= view.frame.origin.y + view.frame.size.height) return YES;
802812
else if (viewForLabel.frame.origin.y + viewForLabel.frame.size.height >= view.frame.origin.y && viewForLabel.frame.origin.y + viewForLabel.frame.size.height <= view.frame.origin.y + view.frame.size.height) return YES;
@@ -870,7 +880,7 @@ - (NSNumber *)calculateMinimumPointValue {
870880
NSExpression *expression = [NSExpression expressionForFunction:@"min:" arguments:@[[NSExpression expressionForConstantValue:dataPoints]]];
871881
NSNumber *value = [expression expressionValueWithObject:nil context:nil];
872882
return value;
873-
} else return 0;
883+
} else return @0;
874884
}
875885

876886
- (NSNumber *)calculateMaximumPointValue {
@@ -928,35 +938,35 @@ - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
928938
closestDot.alpha = 0.8;
929939

930940

931-
if (self.enablePopUpReport == YES && closestDot.tag > 99 && closestDot.tag < 1000 && [closestDot isKindOfClass:[BEMCircle class]] && self.alwaysDisplayPopUpLabels == NO) {
941+
if (self.enablePopUpReport == YES && closestDot.tag >= DotFirstTag100 && closestDot.tag < DotLastTag1000 && [closestDot isKindOfClass:[BEMCircle class]] && self.alwaysDisplayPopUpLabels == NO) {
932942
[self setUpPopUpLabelAbovePoint:closestDot];
933943
}
934944

935-
if (closestDot.tag > 99 && closestDot.tag < 1000 && [closestDot isMemberOfClass:[BEMCircle class]]) {
945+
if (closestDot.tag >= DotFirstTag100 && closestDot.tag < DotLastTag1000 && [closestDot isMemberOfClass:[BEMCircle class]]) {
936946
if ([self.delegate respondsToSelector:@selector(lineGraph:didTouchGraphWithClosestIndex:)] && self.enableTouchReport == YES) {
937-
[self.delegate lineGraph:self didTouchGraphWithClosestIndex:((NSInteger)closestDot.tag - 100)];
947+
[self.delegate lineGraph:self didTouchGraphWithClosestIndex:((NSInteger)closestDot.tag - DotFirstTag100)];
938948

939949
} else if ([self.delegate respondsToSelector:@selector(didTouchGraphWithClosestIndex:)] && self.enableTouchReport == YES) {
940950
[self printDeprecationWarningForOldMethod:@"didTouchGraphWithClosestIndex:" andReplacementMethod:@"lineGraph:didTouchGraphWithClosestIndex:"];
941951

942952
#pragma clang diagnostic push
943953
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
944-
[self.delegate didTouchGraphWithClosestIndex:((int)closestDot.tag - 100)];
954+
[self.delegate didTouchGraphWithClosestIndex:((int)closestDot.tag - DotFirstTag100)];
945955
#pragma clang diagnostic pop
946956
}
947957
}
948958

949959
// ON RELEASE
950960
if (recognizer.state == UIGestureRecognizerStateEnded) {
951961
if ([self.delegate respondsToSelector:@selector(lineGraph:didReleaseTouchFromGraphWithClosestIndex:)]) {
952-
[self.delegate lineGraph:self didReleaseTouchFromGraphWithClosestIndex:(closestDot.tag - 100)];
962+
[self.delegate lineGraph:self didReleaseTouchFromGraphWithClosestIndex:(closestDot.tag - DotFirstTag100)];
953963

954964
} else if ([self.delegate respondsToSelector:@selector(didReleaseGraphWithClosestIndex:)]) {
955965
[self printDeprecationWarningForOldMethod:@"didReleaseGraphWithClosestIndex:" andReplacementMethod:@"lineGraph:didReleaseTouchFromGraphWithClosestIndex:"];
956966

957967
#pragma clang diagnostic push
958968
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
959-
[self.delegate didReleaseGraphWithClosestIndex:(closestDot.tag - 100)];
969+
[self.delegate didReleaseGraphWithClosestIndex:(closestDot.tag - DotFirstTag100)];
960970
#pragma clang diagnostic pop
961971
}
962972

@@ -989,9 +999,9 @@ - (void)setUpPopUpLabelAbovePoint:(BEMCircle *)closestPoint {
989999
self.popUpLabel.center = self.popUpView.center;
9901000

9911001
if ([self.delegate respondsToSelector:@selector(popUpSuffixForlineGraph:)])
992-
self.popUpLabel.text = [NSString stringWithFormat:@"%li%@", (long)[[dataPoints objectAtIndex:((NSInteger)closestDot.tag - 100)] integerValue], [self.delegate popUpSuffixForlineGraph:self]];
1002+
self.popUpLabel.text = [NSString stringWithFormat:@"%li%@", (long)[dataPoints[(NSInteger) closestDot.tag - DotFirstTag100] integerValue], [self.delegate popUpSuffixForlineGraph:self]];
9931003
else
994-
self.popUpLabel.text = [NSString stringWithFormat:@"%li", (long)[[dataPoints objectAtIndex:((NSInteger)closestDot.tag - 100)] integerValue]];
1004+
self.popUpLabel.text = [NSString stringWithFormat:@"%li", (long)[dataPoints[(NSInteger) closestDot.tag - DotFirstTag100] integerValue]];
9951005
if (self.enableYAxisLabel == YES && self.popUpView.frame.origin.x <= self.YAxisLabelXOffset) {
9961006
self.xCenterLabel = self.popUpView.frame.size.width/2;
9971007
self.popUpView.center = CGPointMake(self.xCenterLabel + self.YAxisLabelXOffset + 1, self.yCenterLabel);
@@ -1015,7 +1025,7 @@ - (void)setUpPopUpLabelAbovePoint:(BEMCircle *)closestPoint {
10151025
- (BEMCircle *)closestDotFromtouchInputLine:(UIView *)touchInputLine {
10161026
currentlyCloser = pow((self.frame.size.width/(numberOfPoints-1))/2, 2);
10171027
for (BEMCircle *point in self.subviews) {
1018-
if (point.tag > 99 && point.tag < 1000 && [point isMemberOfClass:[BEMCircle class]]) {
1028+
if (point.tag >= DotFirstTag100 && point.tag < DotLastTag1000 && [point isMemberOfClass:[BEMCircle class]]) {
10191029
if (self.alwaysDisplayDots == NO) {
10201030
point.alpha = 0;
10211031
}

0 commit comments

Comments
 (0)