@@ -37,6 +37,9 @@ @interface CPTLegend()
3737
3838-(void )recalculateLayout ;
3939-(void )removeLegendEntriesForPlot : (CPTPlot *)plot ;
40+
41+ -(void )legendEntryForInteractionPoint : (CGPoint)interactionPoint row : (NSUInteger *)row col : (NSUInteger *)col ;
42+
4043-(void )legendNeedsRedraw : (NSNotification *)notif ;
4144-(void )legendNeedsLayout : (NSNotification *)notif ;
4245-(void )legendNeedsReloadEntries : (NSNotification *)notif ;
@@ -1040,6 +1043,74 @@ -(void)legendNeedsReloadEntries:(NSNotification *)notif
10401043#pragma mark -
10411044#pragma mark Responder Chain and User interaction
10421045
1046+ // / @cond
1047+
1048+ -(void )legendEntryForInteractionPoint : (CGPoint)interactionPoint row : (NSUInteger *)row col : (NSUInteger *)col
1049+ {
1050+ // Convert the interaction point to the local coordinate system
1051+ CPTGraph *theGraph = self.graph ;
1052+
1053+ if ( theGraph ) {
1054+ interactionPoint = [self convertPoint: interactionPoint fromLayer: theGraph];
1055+ }
1056+ else {
1057+ for ( CPTPlot *plot in self.plots ) {
1058+ CPTGraph *plotGraph = plot.graph ;
1059+
1060+ if ( plotGraph ) {
1061+ interactionPoint = [self convertPoint: interactionPoint fromLayer: plotGraph];
1062+ break ;
1063+ }
1064+ }
1065+ }
1066+
1067+ // Update layout if needed
1068+ [self recalculateLayout ];
1069+
1070+ // Hit test the legend entries
1071+ CGFloat rMargin = self.rowMargin ;
1072+ CGFloat cMargin = self.columnMargin ;
1073+
1074+ CGFloat swatchWidth = self.swatchSize .width + self.titleOffset ;
1075+
1076+ CGFloat padHorizontal = self.entryPaddingLeft + self.entryPaddingRight ;
1077+ CGFloat padVertical = self.entryPaddingTop + self.entryPaddingBottom ;
1078+
1079+ // Rows
1080+ CGFloat position = CGRectGetMaxY (self.bounds ) - self.paddingTop ;
1081+
1082+ NSUInteger i = 0 ;
1083+
1084+ for ( NSNumber *height in self.rowHeightsThatFit ) {
1085+ CGFloat rowHeight = height.cgFloatValue + padVertical;
1086+ if ( (interactionPoint.y <= position) && (interactionPoint.y >= position - rowHeight) ) {
1087+ *row = i;
1088+ break ;
1089+ }
1090+
1091+ position -= rowHeight + rMargin;
1092+ i++;
1093+ }
1094+
1095+ // Columns
1096+ position = self.paddingLeft ;
1097+
1098+ i = 0 ;
1099+
1100+ for ( NSNumber *width in self.columnWidthsThatFit ) {
1101+ CGFloat colWidth = width.cgFloatValue + swatchWidth + padHorizontal;
1102+ if ( (interactionPoint.x >= position) && (interactionPoint.x <= position + colWidth) ) {
1103+ *col = i;
1104+ break ;
1105+ }
1106+
1107+ position += colWidth + cMargin;
1108+ i++;
1109+ }
1110+ }
1111+
1112+ // / @endcond
1113+
10431114// / @name User Interaction
10441115// / @{
10451116
@@ -1062,9 +1133,7 @@ -(void)legendNeedsReloadEntries:(NSNotification *)notif
10621133 **/
10631134-(BOOL )pointingDeviceDownEvent : (CPTNativeEvent *)event atPoint : (CGPoint)interactionPoint
10641135{
1065- NSArray *myPlots = self.plots ;
1066-
1067- if ( self.hidden || (myPlots.count == 0 ) ) {
1136+ if ( self.hidden || (self.plots .count == 0 ) ) {
10681137 return NO ;
10691138 }
10701139
@@ -1073,61 +1142,9 @@ -(BOOL)pointingDeviceDownEvent:(CPTNativeEvent *)event atPoint:(CGPoint)interact
10731142 [theDelegate respondsToSelector: @selector (legend:legendEntryForPlot:touchDownAtIndex:withEvent: )] ||
10741143 [theDelegate respondsToSelector: @selector (legend:legendEntryForPlot:wasSelectedAtIndex: )] ||
10751144 [theDelegate respondsToSelector: @selector (legend:legendEntryForPlot:wasSelectedAtIndex:withEvent: )] ) {
1076- // Convert the interaction point to the local coordinate system
1077- CPTGraph *theGraph = self.graph ;
1078- if ( theGraph ) {
1079- interactionPoint = [self convertPoint: interactionPoint fromLayer: theGraph];
1080- }
1081- else {
1082- for ( CPTPlot *plot in myPlots ) {
1083- CPTGraph *plotGraph = plot.graph ;
1084-
1085- if ( plotGraph ) {
1086- interactionPoint = [self convertPoint: interactionPoint fromLayer: plotGraph];
1087- break ;
1088- }
1089- }
1090- }
1091-
1092- // Update layout if needed
1093- [self recalculateLayout ];
1094-
1095- // Hit test the legend entries
1096- CGFloat rMargin = self.rowMargin ;
1097- CGFloat cMargin = self.columnMargin ;
1098-
1099- CGFloat swatchWidth = self.swatchSize .width + self.titleOffset ;
1100-
11011145 NSUInteger row = NSNotFound ;
11021146 NSUInteger col = NSNotFound ;
1103-
1104- // Rows
1105- CGFloat position = CGRectGetMaxY (self.bounds ) - self.paddingTop ;
1106- NSUInteger i = 0 ;
1107- for ( NSNumber *height in self.rowHeightsThatFit ) {
1108- CGFloat rowHeight = height.cgFloatValue ;
1109- if ( (interactionPoint.y <= position) && (interactionPoint.y >= position - rowHeight) ) {
1110- row = i;
1111- break ;
1112- }
1113-
1114- position -= rowHeight + rMargin;
1115- i++;
1116- }
1117-
1118- // Columns
1119- position = self.paddingLeft ;
1120- i = 0 ;
1121- for ( NSNumber *width in self.columnWidthsThatFit ) {
1122- CGFloat colWidth = width.cgFloatValue ;
1123- if ( (interactionPoint.x >= position) && (interactionPoint.x <= position + colWidth) ) {
1124- col = i;
1125- break ;
1126- }
1127-
1128- position += colWidth + swatchWidth + cMargin;
1129- i++;
1130- }
1147+ [self legendEntryForInteractionPoint: interactionPoint row: &row col: &col];
11311148
11321149 // Notify the delegate if we found a hit
11331150 if ( (row != NSNotFound ) && (col != NSNotFound ) ) {
@@ -1187,9 +1204,7 @@ -(BOOL)pointingDeviceUpEvent:(CPTNativeEvent *)event atPoint:(CGPoint)interactio
11871204
11881205 self.pointingDeviceDownEntry = nil ;
11891206
1190- NSArray *myPlots = self.plots ;
1191-
1192- if ( self.hidden || (myPlots.count == 0 ) ) {
1207+ if ( self.hidden || (self.plots .count == 0 ) ) {
11931208 return NO ;
11941209 }
11951210
@@ -1198,61 +1213,9 @@ -(BOOL)pointingDeviceUpEvent:(CPTNativeEvent *)event atPoint:(CGPoint)interactio
11981213 [theDelegate respondsToSelector: @selector (legend:legendEntryForPlot:touchUpAtIndex:withEvent: )] ||
11991214 [theDelegate respondsToSelector: @selector (legend:legendEntryForPlot:wasSelectedAtIndex: )] ||
12001215 [theDelegate respondsToSelector: @selector (legend:legendEntryForPlot:wasSelectedAtIndex:withEvent: )] ) {
1201- // Convert the interaction point to the local coordinate system
1202- CPTGraph *theGraph = self.graph ;
1203- if ( theGraph ) {
1204- interactionPoint = [self convertPoint: interactionPoint fromLayer: theGraph];
1205- }
1206- else {
1207- for ( CPTPlot *plot in myPlots ) {
1208- CPTGraph *plotGraph = plot.graph ;
1209-
1210- if ( plotGraph ) {
1211- interactionPoint = [self convertPoint: interactionPoint fromLayer: plotGraph];
1212- break ;
1213- }
1214- }
1215- }
1216-
1217- // Update layout if needed
1218- [self recalculateLayout ];
1219-
1220- // Hit test the legend entries
1221- CGFloat rMargin = self.rowMargin ;
1222- CGFloat cMargin = self.columnMargin ;
1223-
1224- CGFloat swatchWidth = self.swatchSize .width + self.titleOffset ;
1225-
12261216 NSUInteger row = NSNotFound ;
12271217 NSUInteger col = NSNotFound ;
1228-
1229- // Rows
1230- CGFloat position = CGRectGetMaxY (self.bounds ) - self.paddingTop ;
1231- NSUInteger i = 0 ;
1232- for ( NSNumber *height in self.rowHeightsThatFit ) {
1233- CGFloat rowHeight = height.cgFloatValue ;
1234- if ( (interactionPoint.y <= position) && (interactionPoint.y >= position - rowHeight) ) {
1235- row = i;
1236- break ;
1237- }
1238-
1239- position -= rowHeight + rMargin;
1240- i++;
1241- }
1242-
1243- // Columns
1244- position = self.paddingLeft ;
1245- i = 0 ;
1246- for ( NSNumber *width in self.columnWidthsThatFit ) {
1247- CGFloat colWidth = width.cgFloatValue ;
1248- if ( (interactionPoint.x >= position) && (interactionPoint.x <= position + colWidth) ) {
1249- col = i;
1250- break ;
1251- }
1252-
1253- position += colWidth + swatchWidth + cMargin;
1254- i++;
1255- }
1218+ [self legendEntryForInteractionPoint: interactionPoint row: &row col: &col];
12561219
12571220 // Notify the delegate if we found a hit
12581221 if ( (row != NSNotFound ) && (col != NSNotFound ) ) {
0 commit comments