Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Small refactoring

  • Loading branch information...
commit 730cc02bfd053d7678f8259a8b01a909bd87fb34 1 parent 1bad558
Sergey Lenkov authored April 01, 2012
3  Classes/YBGraphView.h
@@ -79,6 +79,9 @@
79 79
 	id delegate;
80 80
 	id dataSource;
81 81
     NSMutableArray *_customMarkers;
  82
+    
  83
+    float _stepX;
  84
+    float _stepY;
82 85
 }
83 86
 
84 87
 @property (nonatomic, retain) NSNumberFormatter *formatter;
260  Classes/YBGraphView.m
@@ -17,6 +17,8 @@ @interface YBGraphView (Private)
17 17
 
18 18
 - (void)drawLegendInRect:(NSRect)rect;
19 19
 - (void)drawCustomView:(NSView *)view atPoint:(NSPoint)point inRect:(NSRect)rect;
  20
+- (YBPointInfo *)infoForPoint:(NSPoint)point graphIndex:(NSInteger)graphIndex elementIndex:(NSInteger)elementIndex;
  21
+- (void)drawMarkers:(NSArray *)markers inRect:(NSRect)rect;
20 22
 
21 23
 @end
22 24
 
@@ -180,9 +182,9 @@ - (void)drawRect:(NSRect)rect {
180 182
 		offsetY = OFFSET_Y;
181 183
 	}
182 184
 	
183  
-	float minY = 0.0;
  185
+    float minY = 0.0;
184 186
 	float maxY = 0.0;
185  
-	
  187
+    
186 188
 	for (int i = 0; i < [graphs count]; i++) {
187 189
 		NSMutableArray *values = [graphs objectAtIndex:i];
188 190
 
@@ -216,10 +218,12 @@ - (void)drawRect:(NSRect)rect {
216 218
 	}
217 219
     
218 220
 	float step = (maxY - minY) / gridYCount;
219  
-	float stepY = (rect.size.height - (offsetY * 2)) / maxY;
  221
+    
  222
+    _stepX = (self.frame.size.width - (offsetX * 2)) / ([series count] - 1);
  223
+	_stepY = (rect.size.height - (offsetY * 2)) / maxY;
220 224
 	
221 225
 	if (!useMinValue) {
222  
-		stepY = (rect.size.height - (offsetY * 2)) / (maxY - minY);
  226
+		_stepY = (rect.size.height - (offsetY * 2)) / (maxY - minY);
223 227
 	}
224 228
 	
225 229
 	NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
@@ -228,10 +232,9 @@ - (void)drawRect:(NSRect)rect {
228 232
 	NSDictionary *attsDict = [NSDictionary dictionaryWithObjectsAndKeys:textColor, NSForegroundColorAttributeName, font, NSFontAttributeName, [NSNumber numberWithInt:NSNoUnderlineStyle], NSUnderlineStyleAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
229 233
 	
230 234
 	for (int i = 0; i <= gridYCount; i++) {		
231  
-		int y = (i * step) * stepY;
  235
+		int y = (i * step) * _stepY;
232 236
 		float value = i * step + minY;
233 237
 		
234  
-		
235 238
 		if (drawGridY) {
236 239
 			NSBezierPath *path = [NSBezierPath bezierPath];
237 240
 		
@@ -292,7 +295,7 @@ - (void)drawRect:(NSRect)rect {
292 295
 		maxStep = [series count];
293 296
 	}
294 297
 		
295  
-	float stepX = (self.frame.size.width - (offsetX * 2)) / ([series count] - 1);
  298
+	//_stepX = (self.frame.size.width - (offsetX * 2)) / ([series count] - 1);
296 299
 
297 300
 	paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
298 301
 	[paragraphStyle setAlignment:NSCenterTextAlignment];
@@ -344,7 +347,7 @@ - (void)drawRect:(NSRect)rect {
344 347
 		}
345 348
         
346 349
         for (int i = 1; i < maxStep - 1; i++) {
347  
-            int x = (i * step) * stepX;
  350
+            int x = (i * step) * _stepX;
348 351
             
349 352
             if (x > self.frame.size.width - (offsetX * 2)) {
350 353
                 x = self.frame.size.width - (offsetX * 2);
@@ -411,11 +414,11 @@ - (void)drawRect:(NSRect)rect {
411 414
 	
412 415
 	[paragraphStyle release];
413 416
 	
414  
-	stepX = (self.frame.size.width - 120) / ([series count] - 1);
  417
+	_stepX = (self.frame.size.width - 120) / ([series count] - 1);
415 418
 	    
416 419
     NSPoint lastPoint;
417 420
 	
418  
-    NSMutableArray *markers = [[NSMutableArray alloc] init];
  421
+    NSMutableArray *_markers = [[NSMutableArray alloc] init];
419 422
     
420 423
     for (int i = 0; i < [graphs count]; i++) {
421 424
 		NSMutableArray *values = [graphs objectAtIndex:i];
@@ -423,10 +426,10 @@ - (void)drawRect:(NSRect)rect {
423 426
 		
424 427
 		if ([values count] == 1) {
425 428
 			int x = (self.frame.size.width) / 2;
426  
-			int y = [[values objectAtIndex:0] floatValue] * stepY;
  429
+			int y = [[values objectAtIndex:0] floatValue] * _stepY;
427 430
 			
428 431
 			if (!useMinValue) {
429  
-				y = ([[values objectAtIndex:0] floatValue] - minY) * stepY;
  432
+				y = ([[values objectAtIndex:0] floatValue] - minY) * _stepY;
430 433
 			}
431 434
 			
432 435
 			NSPoint point = NSMakePoint(x, y);
@@ -447,7 +450,7 @@ - (void)drawRect:(NSRect)rect {
447 450
                     continue;
448 451
                 }
449 452
                      
450  
-				int x = j * stepX;
  453
+				int x = j * _stepX;
451 454
 				int y = 0;
452 455
 
453 456
 				if (useMinValue) {
@@ -457,9 +460,9 @@ - (void)drawRect:(NSRect)rect {
457 460
                         value = minValue;
458 461
                     }
459 462
                     
460  
-					y = value * stepY;
  463
+					y = value * _stepY;
461 464
 				} else {
462  
-                    y = ([[values objectAtIndex:j] floatValue] - minY) * stepY;
  465
+                    y = ([[values objectAtIndex:j] floatValue] - minY) * _stepY;
463 466
                 }
464 467
 			
465 468
 				NSBezierPath *path = [NSBezierPath bezierPath];
@@ -499,7 +502,7 @@ - (void)drawRect:(NSRect)rect {
499 502
                         [bullet drawAtPoint:startPoint];
500 503
                     }
501 504
                     
502  
-                    if (mousePoint.x > startPoint.x - (stepX / 2) && mousePoint.x < startPoint.x + (stepX / 2)) {
  505
+                    if (mousePoint.x > startPoint.x - (_stepX / 2) && mousePoint.x < startPoint.x + (_stepX / 2)) {
503 506
                         YBPointInfo *pointInfo = [[YBPointInfo alloc] init];
504 507
                         
505 508
                         pointInfo.x = startPoint.x;
@@ -511,14 +514,14 @@ - (void)drawRect:(NSRect)rect {
511 514
                             pointInfo.title = [formatter stringFromNumber:[[graphs objectAtIndex:i] objectAtIndex:j]];
512 515
                         }
513 516
                         
514  
-                        [markers addObject:pointInfo];
  517
+                        [_markers addObject:pointInfo];
515 518
                         [pointInfo release];
516 519
                     }
517 520
                     
518 521
                     continue;
519 522
                 }
520 523
                 
521  
-				x = (j + 1) * stepX;
  524
+				x = (j + 1) * _stepX;
522 525
 				y = 0;
523 526
 		
524 527
 				if (useMinValue) {
@@ -528,9 +531,9 @@ - (void)drawRect:(NSRect)rect {
528 531
                         value = minValue;
529 532
                     }
530 533
                     
531  
-                    y = value * stepY;
  534
+                    y = value * _stepY;
532 535
 				} else {
533  
-                    y = ([[values objectAtIndex:j + 1] floatValue] - minY) * stepY;
  536
+                    y = ([[values objectAtIndex:j + 1] floatValue] - minY) * _stepY;
534 537
                 }
535 538
 			
536 539
 				NSPoint endPoint = NSMakePoint(x + offsetX, y + offsetY);
@@ -557,57 +560,21 @@ - (void)drawRect:(NSRect)rect {
557 560
 				
558 561
                 BOOL isHighlighted = NO;
559 562
                 
560  
-                if (showMarkerNearPoint) {
561  
-                    if (mousePoint.x > startPoint.x - (stepX / 2) && mousePoint.x < startPoint.x + (stepX / 2) && 
562  
-                        mousePoint.y > startPoint.y - (stepY / 2) && mousePoint.y < startPoint.y + (stepY / 2)) {
563  
-                        YBPointInfo *pointInfo = [[YBPointInfo alloc] init];
564  
-                        
565  
-                        pointInfo.x = startPoint.x;
566  
-                        pointInfo.y = startPoint.y;
567  
-                        pointInfo.graph = i;
568  
-                        pointInfo.element = j;
569  
-                        
570  
-                        if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
571  
-                            pointInfo.title = [dataSource graphView:self markerTitleForGraph:i forElement:j];
572  
-                        } else {
573  
-                            pointInfo.title = [formatter stringFromNumber:[[graphs objectAtIndex:i] objectAtIndex:j]];
574  
-                        }
575  
-                        
576  
-                        [markers addObject:pointInfo];
577  
-                        [pointInfo release];
578  
-                        
579  
-                        isHighlighted = YES;
580  
-                    }
581  
-                } else {
582  
-                    if (mousePoint.x > startPoint.x - (stepX / 2) && mousePoint.x < startPoint.x + (stepX / 2)) {
583  
-                        YBPointInfo *pointInfo = [[YBPointInfo alloc] init];
584  
-
585  
-                        pointInfo.x = startPoint.x;
586  
-                        pointInfo.y = startPoint.y;
587  
-                        pointInfo.graph = i;
588  
-                        pointInfo.element = j;
589  
-                        
590  
-                        if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
591  
-                            pointInfo.title = [dataSource graphView:self markerTitleForGraph:i forElement:j];
592  
-                        } else {
593  
-                            pointInfo.title = [formatter stringFromNumber:[[graphs objectAtIndex:i] objectAtIndex:j]];
594  
-                        }
595  
-                    
596  
-                        [markers addObject:pointInfo];
597  
-                        [pointInfo release];
598  
-                        
599  
-                        isHighlighted = YES;
600  
-                    }
  563
+                YBPointInfo *pointInfo = [self infoForPoint:startPoint graphIndex:i elementIndex:j];
  564
+                
  565
+                if (pointInfo) {
  566
+                    [_markers addObject:pointInfo];
  567
+                    isHighlighted = YES;
601 568
                 }
602 569
                 
603 570
 				if (drawBullets) {
604  
-                    if (!highlightBullet) {
  571
+                    if (!highlightBullet || !enableMarker) {
605 572
                         isHighlighted = NO;
606 573
                     }
607 574
                     
608 575
 					bullet.color = [YBGraphView colorByIndex:i];
609 576
 					[bullet drawAtPoint:startPoint highlighted:isHighlighted];
610  
-				} else if (highlightBullet && isHighlighted) {
  577
+				} else if (highlightBullet && isHighlighted && enableMarker) {
611 578
                     bullet.color = [YBGraphView colorByIndex:i];
612 579
 					[bullet drawAtPoint:startPoint highlighted:NO];
613 580
                 }
@@ -657,67 +624,30 @@ - (void)drawRect:(NSRect)rect {
657 624
 		
658 625
         BOOL isHighlighted = NO;
659 626
         
660  
-        if (showMarkerNearPoint) {
661  
-            if (mousePoint.x > (lastPoint.x - (stepX / 2)) && mousePoint.x < (lastPoint.x + (stepX / 2)) && 
662  
-                mousePoint.y > (lastPoint.y - (stepY / 2)) && mousePoint.y < (lastPoint.y + (stepY / 2))) {
663  
-                YBPointInfo *pointInfo = [[YBPointInfo alloc] init];
664  
-                
665  
-                pointInfo.x = lastPoint.x;
666  
-                pointInfo.y = lastPoint.y;
667  
-                pointInfo.graph = i;
668  
-                pointInfo.element = [values count] - 1;
669  
-                
670  
-                if (![[[graphs objectAtIndex:i] objectAtIndex:[values count] - 1] isKindOfClass:[NSNull class]]) {
671  
-                    if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
672  
-                        pointInfo.title = [dataSource graphView:self markerTitleForGraph:i forElement:[values count] - 1];
673  
-                    } else {
674  
-                        pointInfo.title = [formatter stringFromNumber:[[graphs objectAtIndex:i] objectAtIndex:[values count] - 1]];
675  
-                    }
676  
-                    
677  
-                    [markers addObject:pointInfo];
678  
-                }
679  
-                
680  
-                [pointInfo release];
681  
-                
682  
-                isHighlighted = YES;
683  
-            }
684  
-        } else {
685  
-            if (mousePoint.x > (lastPoint.x - (stepX / 2)) && mousePoint.x < (lastPoint.x + (stepX / 2))) {
686  
-                YBPointInfo *pointInfo = [[YBPointInfo alloc] init];
687  
-			
688  
-                pointInfo.x = lastPoint.x;
689  
-                pointInfo.y = lastPoint.y;
690  
-                pointInfo.graph = i;
691  
-                pointInfo.element = [values count] - 1;
692  
-                
693  
-                if (![[[graphs objectAtIndex:i] objectAtIndex:[values count] - 1] isKindOfClass:[NSNull class]]) {
694  
-                    if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
695  
-                        pointInfo.title = [dataSource graphView:self markerTitleForGraph:i forElement:[values count] - 1];
696  
-                    } else {
697  
-                        pointInfo.title = [formatter stringFromNumber:[[graphs objectAtIndex:i] objectAtIndex:[values count] - 1]];
698  
-                    }
699  
-                
700  
-                    [markers addObject:pointInfo];
701  
-                }
702  
-                
703  
-                [pointInfo release];
704  
-                
705  
-                isHighlighted = YES;
706  
-            }
707  
-		}
  627
+        YBPointInfo *pointInfo = [self infoForPoint:lastPoint graphIndex:i elementIndex:[values count] - 1];
708 628
         
709  
-		if (drawBullets) {
710  
-			bullet.color = [YBGraphView colorByIndex:i];
711  
-			[bullet drawAtPoint:lastPoint highlighted:isHighlighted];
712  
-		}
  629
+        if (pointInfo) {
  630
+            [_markers addObject:pointInfo];
  631
+            isHighlighted = YES;
  632
+        }
  633
+        
  634
+        if (drawBullets) {
  635
+            if (!highlightBullet || !enableMarker) {
  636
+                isHighlighted = NO;
  637
+            }
  638
+            
  639
+            bullet.color = [YBGraphView colorByIndex:i];
  640
+            [bullet drawAtPoint:lastPoint highlighted:isHighlighted];
  641
+        } else if (highlightBullet && isHighlighted && enableMarker) {
  642
+            bullet.color = [YBGraphView colorByIndex:i];
  643
+            [bullet drawAtPoint:lastPoint highlighted:NO];
  644
+        }
713 645
 	}
714 646
 
715 647
 	if (drawLegend) {
716 648
 		[self drawLegendInRect:rect];
717 649
 	}
718 650
 	
719  
-	// draw info
720  
-	
721 651
 	if (drawInfo) {
722 652
 		NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
723 653
 		[paragraphStyle setAlignment:NSCenterTextAlignment];
@@ -728,33 +658,10 @@ - (void)drawRect:(NSRect)rect {
728 658
 		
729 659
 		[paragraphStyle release];
730 660
 	}
731  
-	
732  
-	// draw markers
733 661
 
734  
-	if (showMarker && !hideMarker && enableMarker) {
735  
-        for (NSView *view in _customMarkers) {
736  
-            [view removeFromSuperview];
737  
-        }
738  
-        
739  
-        [_customMarkers removeAllObjects];
740  
-         
741  
-        for (YBPointInfo *pointInfo in markers) {
742  
-            NSPoint point;
743  
-            point.x = pointInfo.x;
744  
-            point.y = pointInfo.y;
745  
-            
746  
-            if ([dataSource respondsToSelector:@selector(graphView: markerViewForGraph: forElement:)]) {
747  
-                NSView *customMarker = [dataSource graphView:self markerViewForGraph:pointInfo.graph forElement:pointInfo.element];
748  
-                
749  
-                [self drawCustomView:customMarker atPoint:point inRect:rect];
750  
-                [_customMarkers addObject:customMarker];
751  
-            } else {
752  
-                [marker drawAtPoint:point inRect:rect withTitle:pointInfo.title];
753  
-            }
754  
-        }
755  
-	}
  662
+	[self drawMarkers:_markers inRect:rect];
756 663
     
757  
-    [markers release];
  664
+    [_markers release];
758 665
     
759 666
     if (drawBottomMarker) {
760 667
         NSRectFill(NSMakeRect(100, 0, 120, 20));
@@ -771,6 +678,10 @@ - (void)drawRect:(NSRect)rect {
771 678
     }
772 679
 }
773 680
 
  681
+#pragma mark -
  682
+#pragma mark Private
  683
+#pragma mark -
  684
+
774 685
 - (void)drawLegendInRect:(NSRect)rect {
775 686
 	NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
776 687
 	[paragraphStyle setAlignment:NSLeftTextAlignment];
@@ -815,6 +726,71 @@ - (void)drawCustomView:(NSView *)customView atPoint:(NSPoint)point inRect:(NSRec
815 726
     [self addSubview:customView];
816 727
 }
817 728
 
  729
+- (YBPointInfo *)infoForPoint:(NSPoint)point graphIndex:(NSInteger)graphIndex elementIndex:(NSInteger)elementIndex {
  730
+    if (showMarkerNearPoint) {
  731
+        if (mousePoint.x > point.x - (_stepX / 2) && mousePoint.x < point.x + (_stepX / 2) && 
  732
+            mousePoint.y > point.y - (_stepY / 2) && mousePoint.y < point.y + (_stepY / 2)) {
  733
+            YBPointInfo *pointInfo = [[[YBPointInfo alloc] init] autorelease];
  734
+            
  735
+            pointInfo.x = point.x;
  736
+            pointInfo.y = point.y;
  737
+            pointInfo.graph = graphIndex;
  738
+            pointInfo.element = elementIndex;
  739
+            
  740
+            if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
  741
+                pointInfo.title = [dataSource graphView:self markerTitleForGraph:graphIndex forElement:elementIndex];
  742
+            } else {
  743
+                pointInfo.title = [formatter stringFromNumber:[[graphs objectAtIndex:graphIndex] objectAtIndex:elementIndex]];
  744
+            }
  745
+            
  746
+            return pointInfo;
  747
+        }
  748
+    } else {
  749
+        if (mousePoint.x > point.x - (_stepX / 2) && mousePoint.x < point.x + (_stepX / 2)) {
  750
+            YBPointInfo *pointInfo = [[[YBPointInfo alloc] init] autorelease];
  751
+            
  752
+            pointInfo.x = point.x;
  753
+            pointInfo.y = point.y;
  754
+            pointInfo.graph = graphIndex;
  755
+            pointInfo.element = elementIndex;
  756
+            
  757
+            if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
  758
+                pointInfo.title = [dataSource graphView:self markerTitleForGraph:graphIndex forElement:elementIndex];
  759
+            } else {
  760
+                pointInfo.title = [formatter stringFromNumber:[[graphs objectAtIndex:graphIndex] objectAtIndex:elementIndex]];
  761
+            }
  762
+            
  763
+            return pointInfo;
  764
+        }
  765
+    }
  766
+    
  767
+    return nil;
  768
+}
  769
+
  770
+- (void)drawMarkers:(NSArray *)markers inRect:(NSRect)rect {
  771
+    if (showMarker && !hideMarker && enableMarker) {
  772
+        for (NSView *view in _customMarkers) {
  773
+            [view removeFromSuperview];
  774
+        }
  775
+        
  776
+        [_customMarkers removeAllObjects];
  777
+        
  778
+        for (YBPointInfo *pointInfo in markers) {
  779
+            NSPoint point;
  780
+            point.x = pointInfo.x;
  781
+            point.y = pointInfo.y;
  782
+            
  783
+            if ([dataSource respondsToSelector:@selector(graphView: markerViewForGraph: forElement:)]) {
  784
+                NSView *customMarker = [dataSource graphView:self markerViewForGraph:pointInfo.graph forElement:pointInfo.element];
  785
+                
  786
+                [self drawCustomView:customMarker atPoint:point inRect:rect];
  787
+                [_customMarkers addObject:customMarker];
  788
+            } else {
  789
+                [marker drawAtPoint:point inRect:rect withTitle:pointInfo.title];
  790
+            }
  791
+        }
  792
+	}
  793
+}
818 794
 
819 795
 #pragma mark -
820 796
 #pragma mark Mouse Events

0 notes on commit 730cc02

Please sign in to comment.
Something went wrong with that request. Please try again.