17
17
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO (v ) ([[[UIDevice currentDevice ] systemVersion ] compare: v options: NSNumericSearch] != NSOrderedAscending)
18
18
#define DEFAULT_FONT_NAME @" HelveticaNeue-Light"
19
19
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
+
20
30
@interface BEMSimpleLineGraphView () {
21
31
// / The number of Points in the Graph
22
32
NSInteger numberOfPoints;
@@ -373,7 +383,7 @@ - (void)drawDots {
373
383
if (self.animationGraphEntranceTime != 0 || self.alwaysDisplayDots == YES ) {
374
384
BEMCircle *circleDot = [[BEMCircle alloc ] initWithFrame: CGRectMake (0 , 0 , self .sizePoint, self .sizePoint)];
375
385
circleDot.center = CGPointMake (positionOnXAxis, positionOnYAxis);
376
- circleDot.tag = i+100 ;
386
+ circleDot.tag = i+ DotFirstTag100 ;
377
387
circleDot.alpha = 0 ;
378
388
circleDot.absoluteValue = dotValue;
379
389
circleDot.Pointcolor = self.colorPoint ;
@@ -461,7 +471,7 @@ - (void)drawXAxis {
461
471
if (![self .dataSource respondsToSelector: @selector (lineGraph:labelOnXAxisForIndex: )] && ![self .dataSource respondsToSelector: @selector (labelOnXAxisForIndex: )]) return ;
462
472
463
473
for (UIView *subview in [self subviews ]) {
464
- if ([subview isKindOfClass: [UILabel class ]] && subview.tag == 1000 )
474
+ if ([subview isKindOfClass: [UILabel class ]] && subview.tag == DotLastTag1000 )
465
475
[subview removeFromSuperview ];
466
476
}
467
477
@@ -514,29 +524,29 @@ - (void)drawXAxis {
514
524
UILabel *firstLabel = [[UILabel alloc ] initWithFrame: CGRectMake (3 +self .YAxisLabelXOffset, self .frame.size.height-20 , viewWidth/2 , 20 )];
515
525
firstLabel.text = firstXLabel;
516
526
firstLabel.font = self.labelFont ;
517
- firstLabel.textAlignment = 0 ;
527
+ firstLabel.textAlignment = NSTextAlignmentLeft ;
518
528
firstLabel.textColor = self.colorXaxisLabel ;
519
529
firstLabel.backgroundColor = [UIColor clearColor ];
520
- firstLabel.tag = 1000 ;
530
+ firstLabel.tag = DotLastTag1000 ;
521
531
[self addSubview: firstLabel];
522
532
[xAxisValues addObject: firstXLabel];
523
533
[xAxisLabels addObject: firstLabel];
524
-
525
- NSNumber *xFirstAxisLabelCoordinate = [ NSNumber numberWithFloat: firstLabel.center.x- self .YAxisLabelXOffset] ;
534
+
535
+ NSNumber *xFirstAxisLabelCoordinate = @( firstLabel.center .x - self.YAxisLabelXOffset ) ;
526
536
[xAxisLabelPoints addObject: xFirstAxisLabelCoordinate];
527
537
528
538
UILabel *lastLabel = [[UILabel alloc ] initWithFrame: CGRectMake (self .frame.size.width/2 - 3 , self .frame.size.height-20 , self .frame.size.width/2 , 20 )];
529
539
lastLabel.text = lastXLabel;
530
540
lastLabel.font = self.labelFont ;
531
- lastLabel.textAlignment = 2 ;
541
+ lastLabel.textAlignment = NSTextAlignmentRight ;
532
542
lastLabel.textColor = self.colorXaxisLabel ;
533
543
lastLabel.backgroundColor = [UIColor clearColor ];
534
- lastLabel.tag = 1000 ;
544
+ lastLabel.tag = DotLastTag1000 ;
535
545
[self addSubview: lastLabel];
536
546
[xAxisValues addObject: lastXLabel];
537
547
[xAxisLabels addObject: lastLabel];
538
548
539
- NSNumber *xLastAxisLabelCoordinate = [ NSNumber numberWithFloat: lastLabel.center.x- self .YAxisLabelXOffset] ;
549
+ NSNumber *xLastAxisLabelCoordinate = @( lastLabel.center .x - self.YAxisLabelXOffset ) ;
540
550
[xAxisLabelPoints addObject: xLastAxisLabelCoordinate];
541
551
542
552
} else {
@@ -573,7 +583,7 @@ - (void)drawXAxis {
573
583
labelXAxis.textColor = self.colorXaxisLabel ;
574
584
labelXAxis.backgroundColor = [UIColor clearColor ];
575
585
[xAxisLabels addObject: labelXAxis];
576
- labelXAxis.tag = 1000 ;
586
+ labelXAxis.tag = DotLastTag1000 ;
577
587
578
588
// Add support multi-line, but this might overlap with the graph line if text have too many lines
579
589
labelXAxis.numberOfLines = 0 ;
@@ -613,16 +623,16 @@ - (void)drawXAxis {
613
623
614
624
- (void )drawYAxis {
615
625
for (UIView *subview in [self subviews ]) {
616
- if ([subview isKindOfClass: [UILabel class ]] && subview.tag == 2000 ) {
626
+ if ([subview isKindOfClass: [UILabel class ]] && subview.tag == LabelYAxisTag2000 ) {
617
627
[subview removeFromSuperview ];
618
628
}
619
- else if ([subview isKindOfClass: [UIView class ] ] && subview.tag == 2100 ) {
629
+ else if ([subview isKindOfClass: [UIView class ] ] && subview.tag == BackgroundYAxisTag2100 ) {
620
630
[subview removeFromSuperview ];
621
631
}
622
632
}
623
633
624
634
UIView *backgroundYaxis = [[UIView alloc ]initWithFrame:CGRectMake (0 , 0 , self .YAxisLabelXOffset, self .frame.size.height)];
625
- backgroundYaxis.tag = 2100 ;
635
+ backgroundYaxis.tag = BackgroundYAxisTag2100 ;
626
636
if (self.colorBackgroundYaxis == nil ) {
627
637
backgroundYaxis.backgroundColor = self.colorTop ;
628
638
} else backgroundYaxis.backgroundColor = self.colorBackgroundYaxis ;
@@ -663,16 +673,16 @@ - (void)drawYAxis {
663
673
labelYAxis.font = self.labelFont ;
664
674
labelYAxis.textColor = self.colorYaxisLabel ;
665
675
labelYAxis.backgroundColor = [UIColor clearColor ];
666
- labelYAxis.tag = 2000 ;
676
+ labelYAxis.tag = LabelYAxisTag2000 ;
667
677
labelYAxis.center = CGPointMake (self.YAxisLabelXOffset /2 , yAxisPosition);
668
678
[self addSubview: labelYAxis];
669
679
[yAxisLabels addObject: labelYAxis];
670
680
671
- NSNumber *yAxisLabelCoordinate = [ NSNumber numberWithFloat: labelYAxis.center.y] ;
681
+ NSNumber *yAxisLabelCoordinate = @( labelYAxis.center .y ) ;
672
682
[yAxisLabelPoints addObject: yAxisLabelCoordinate];
673
683
}
674
684
} else {
675
- CGFloat numberOfLabels;
685
+ NSInteger numberOfLabels;
676
686
if ([self .delegate respondsToSelector: @selector (numberOfYAxisLabelsOnLineGraph: )]) numberOfLabels = [self .delegate numberOfYAxisLabelsOnLineGraph: self ];
677
687
else numberOfLabels = 3 ;
678
688
@@ -691,25 +701,25 @@ - (void)drawYAxis {
691
701
labelYAxis.textAlignment = NSTextAlignmentRight;
692
702
labelYAxis.textColor = self.colorYaxisLabel ;
693
703
labelYAxis.backgroundColor = [UIColor clearColor ];
694
- labelYAxis.tag = 2000 ;
704
+ labelYAxis.tag = LabelYAxisTag2000 ;
695
705
696
706
[self addSubview: labelYAxis];
697
707
698
708
[yAxisLabels addObject: labelYAxis];
699
709
700
- NSNumber *yAxisLabelCoordinate = [ NSNumber numberWithFloat: labelYAxis.center.y] ;
710
+ NSNumber *yAxisLabelCoordinate = @( labelYAxis.center .y ) ;
701
711
[yAxisLabelPoints addObject: yAxisLabelCoordinate];
702
712
}
703
713
}
704
714
705
715
// Detect overlapped labels
706
- __block NSUInteger lastMatchIndex;
716
+ __block NSUInteger lastMatchIndex = 0 ;
707
717
NSMutableArray *overlapLabels = [NSMutableArray arrayWithCapacity: 0 ];
708
718
[yAxisLabels enumerateObjectsUsingBlock: ^(UILabel *label, NSUInteger idx, BOOL *stop) {
709
719
710
720
if (idx==0 ) lastMatchIndex = 0 ;
711
721
else { // Skip first one
712
- UILabel *prevLabel = [ yAxisLabels objectAtIndex: lastMatchIndex];
722
+ UILabel *prevLabel = yAxisLabels[ lastMatchIndex];
713
723
CGRect r = CGRectIntersection (prevLabel.frame , label.frame );
714
724
if (CGRectIsNull (r)) lastMatchIndex = idx;
715
725
else [overlapLabels addObject: label]; // overlapped
@@ -743,9 +753,9 @@ - (void)displayPermanentLabelForPoint:(BEMCircle *)circleDot {
743
753
self.enablePopUpReport = NO ;
744
754
self.xCenterLabel = circleDot.center .x ;
745
755
UILabel *permanentPopUpLabel = [[UILabel alloc ] init ];
746
- permanentPopUpLabel.textAlignment = 1 ;
756
+ permanentPopUpLabel.textAlignment = NSTextAlignmentCenter ;
747
757
permanentPopUpLabel.numberOfLines = 0 ;
748
- permanentPopUpLabel.text = [NSString stringWithFormat: @" %@ " , [ NSNumber numberWithInteger: circleDot.absoluteValue] ];
758
+ permanentPopUpLabel.text = [NSString stringWithFormat: @" %@ " , @(( NSInteger ) circleDot.absoluteValue) ];
749
759
permanentPopUpLabel.font = self.labelFont ;
750
760
permanentPopUpLabel.backgroundColor = [UIColor clearColor ];
751
761
[permanentPopUpLabel sizeToFit ];
@@ -756,7 +766,7 @@ - (void)displayPermanentLabelForPoint:(BEMCircle *)circleDot {
756
766
permanentPopUpView.backgroundColor = [UIColor whiteColor ];
757
767
permanentPopUpView.alpha = 0 ;
758
768
permanentPopUpView.layer .cornerRadius = 3 ;
759
- permanentPopUpView.tag = 3100 ;
769
+ permanentPopUpView.tag = PermanentPopUpViewTag3100 ;
760
770
permanentPopUpView.center = permanentPopUpLabel.center ;
761
771
762
772
if (permanentPopUpLabel.frame .origin .x <= 0 ) {
@@ -796,7 +806,7 @@ - (void)displayPermanentLabelForPoint:(BEMCircle *)circleDot {
796
806
797
807
- (BOOL )checkOverlapsForView : (UIView *)view {
798
808
for (UIView *viewForLabel in [self subviews ]) {
799
- if ([viewForLabel isKindOfClass: [UIView class ]] && viewForLabel.tag == 3100 ) {
809
+ if ([viewForLabel isKindOfClass: [UIView class ]] && viewForLabel.tag == PermanentPopUpViewTag3100 ) {
800
810
if ((viewForLabel.frame .origin .x + viewForLabel.frame .size .width ) >= view.frame .origin .x ) {
801
811
if (viewForLabel.frame .origin .y >= view.frame .origin .y && viewForLabel.frame .origin .y <= view.frame .origin .y + view.frame .size .height ) return YES ;
802
812
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 {
870
880
NSExpression *expression = [NSExpression expressionForFunction: @" min:" arguments: @[[NSExpression expressionForConstantValue: dataPoints]]];
871
881
NSNumber *value = [expression expressionValueWithObject: nil context: nil ];
872
882
return value;
873
- } else return 0 ;
883
+ } else return @ 0 ;
874
884
}
875
885
876
886
- (NSNumber *)calculateMaximumPointValue {
@@ -928,35 +938,35 @@ - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
928
938
closestDot.alpha = 0.8 ;
929
939
930
940
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 ) {
932
942
[self setUpPopUpLabelAbovePoint: closestDot];
933
943
}
934
944
935
- if (closestDot.tag > 99 && closestDot.tag < 1000 && [closestDot isMemberOfClass: [BEMCircle class ]]) {
945
+ if (closestDot.tag >= DotFirstTag100 && closestDot.tag < DotLastTag1000 && [closestDot isMemberOfClass: [BEMCircle class ]]) {
936
946
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 )];
938
948
939
949
} else if ([self .delegate respondsToSelector: @selector (didTouchGraphWithClosestIndex: )] && self.enableTouchReport == YES ) {
940
950
[self printDeprecationWarningForOldMethod: @" didTouchGraphWithClosestIndex:" andReplacementMethod: @" lineGraph:didTouchGraphWithClosestIndex:" ];
941
951
942
952
#pragma clang diagnostic push
943
953
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
944
- [self .delegate didTouchGraphWithClosestIndex: ((int )closestDot.tag - 100 )];
954
+ [self .delegate didTouchGraphWithClosestIndex: ((int )closestDot.tag - DotFirstTag100 )];
945
955
#pragma clang diagnostic pop
946
956
}
947
957
}
948
958
949
959
// ON RELEASE
950
960
if (recognizer.state == UIGestureRecognizerStateEnded) {
951
961
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 )];
953
963
954
964
} else if ([self .delegate respondsToSelector: @selector (didReleaseGraphWithClosestIndex: )]) {
955
965
[self printDeprecationWarningForOldMethod: @" didReleaseGraphWithClosestIndex:" andReplacementMethod: @" lineGraph:didReleaseTouchFromGraphWithClosestIndex:" ];
956
966
957
967
#pragma clang diagnostic push
958
968
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
959
- [self .delegate didReleaseGraphWithClosestIndex: (closestDot.tag - 100 )];
969
+ [self .delegate didReleaseGraphWithClosestIndex: (closestDot.tag - DotFirstTag100 )];
960
970
#pragma clang diagnostic pop
961
971
}
962
972
@@ -989,9 +999,9 @@ - (void)setUpPopUpLabelAbovePoint:(BEMCircle *)closestPoint {
989
999
self.popUpLabel .center = self.popUpView .center ;
990
1000
991
1001
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 ]];
993
1003
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 ]];
995
1005
if (self.enableYAxisLabel == YES && self.popUpView .frame .origin .x <= self.YAxisLabelXOffset ) {
996
1006
self.xCenterLabel = self.popUpView .frame .size .width /2 ;
997
1007
self.popUpView .center = CGPointMake (self.xCenterLabel + self.YAxisLabelXOffset + 1 , self.yCenterLabel );
@@ -1015,7 +1025,7 @@ - (void)setUpPopUpLabelAbovePoint:(BEMCircle *)closestPoint {
1015
1025
- (BEMCircle *)closestDotFromtouchInputLine : (UIView *)touchInputLine {
1016
1026
currentlyCloser = pow ((self.frame .size .width /(numberOfPoints-1 ))/2 , 2 );
1017
1027
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 ]]) {
1019
1029
if (self.alwaysDisplayDots == NO ) {
1020
1030
point.alpha = 0 ;
1021
1031
}
0 commit comments